CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2008
    Posts
    1

    How to open a file in C++?

    in vb net i use this code: My.Computer.FileSystem.ReadAllText("C:\test.txt")
    How about in C++? thanks!

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How to open a file in C++?

    Google is faster than opening a topic.

  3. #3
    Join Date
    Oct 2008
    Posts
    51

    Re: How to open a file in C++?

    Hi,
    Look at the example below and make your own file reading.

    I hope this help!





    // basic file operations
    #include <iostream>
    #include <fstream>
    using namespace std;

    int main () {
    ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    myfile.close();
    return 0;
    }

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to open a file in C++?

    It's worth noting that the below is *exactly* equivalent to the above in all respects:

    Code:
    // basic file operations
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
      ofstream myfile("example.txt");
      myfile << "Writing this to a file.\n";
      return 0;
    }
    There's never a need for an explicit close() if the ofstream object is going out of scope, and likewise you can open the file as soon as you construct the object.

    ifstreams can be used in much the same way for input.

  5. #5
    Join Date
    Sep 2008
    Posts
    16

    Re: How to open a file in C++?

    Many people still use this, [I use this too]

    Code:
    FILE*f=fopen("filename.txt");
    if they happen to know not about fstream

  6. #6
    Join Date
    Oct 2008
    Posts
    116

    Re: How to open a file in C++?

    using FILE* is so ok.
    fstream ( ifstream, ofstream ) is nice too.
    however if you just work with text let using CStidioFile ( but it's only for MFC )
    My English is very bad. So tell me if Something I talked make u confuse.
    My Ebook Store: www.coding.vn/book.php

  7. #7
    Join Date
    Oct 2008
    Posts
    8

    Re: How to open a file in C++?

    Yes, I agree, I would look also at CFile if you work with MFC, it provides multiple to deal with files

  8. #8
    Join Date
    Oct 2008
    Posts
    8

    Re: How to open a file in C++?

    I just rewatched some articles, and thinking about using Readfile(fHan,xxxxxx). Hope this could help you as some extracted fluid for the program.

  9. #9
    Join Date
    Aug 2008
    Posts
    112

    Re: How to open a file in C++?

    I prefer C#'s File class or FileInfo as well as Directory or DirectoryInfo for files and directory tasks. You can write one byte, or the whole content to a particular file. I think I just made a little test yesterday about the speed of fsteam for reading content line by line of a file and discovered that it worked worse than C#'s ReadLine function. fstream also fails on several cases if the files was encoded differently. About MFC, it has the reputation of slow interface design and speed as well as Standard compatiblity problem.
    hi,,,

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to open a file in C++?

    Quote Originally Posted by Khiem View Post
    fstream also fails on several cases if the files was encoded differently.
    In general, you should not expect standard IO mechanisms to understand non-ASCII encodings. There are some exceptions, and some partial exceptions: getline() will do the right thing on a UTF8 file, for instance, is the \n character is the same in UTF8 as it is in ASCII.

    Either do the decoding yourself, or get a library specifically designed for the task to do so.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured