CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Posts
    25

    How do I read text files?

    Hi I already found a tutorial on this but it didnt work. But I am wondering if anyone can tell me how to read text files line by line and determine what they say. Also is it posibble to make a new enumerated type but have it also be a function or class? Or would that cause errors?
    Thanks ahead for helping.

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

    Re: How do I read text files?


  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: How do I read text files?

    very easy to do in QT ( a well developed C++ framework)
    Code:
    QFile myfile("text.txt");
    myfile.open(QFile::ReadOnly);
    QTextStream myreader(&myfile);
    QString  linereader = myreader.readline(); // reading one line at a time 
    Qstring  readall = myreader.readall() // read all texty into a single QString  same as std::string
    if you are new to C++ then stick with well established framework as QT or wxwidgets

    these frameworks make writing C++ code relatively easy and offer tons of easy to use API
    further they are very well documented.

  4. #4
    Join Date
    Apr 2008
    Posts
    725

    Re: How do I read text files?

    I'd say qt is much better documented than wx. I'd also say for reading files stl is simple enough

  5. #5
    Join Date
    Sep 2011
    Posts
    25

    Re: How do I read text files?

    I just realised this might be in the wrong section but I use visual C++ for compiling c++ and it makes errors for the way I tried to do it. So how would I do it with that? It has an error where I
    type:

    ifstream myfile;

    the error is "ifstream is unidentified"
    So how would I do this on visual c++

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How do I read text files?

    Quote Originally Posted by HavingPhun View Post
    ifstream myfile;

    the error is "ifstream is unidentified"
    So how would I do this on visual c++
    Code:
    #include <fstream>
    
    int main()
    {
      std::ifstream myfile;
    }
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Sep 2011
    Posts
    25

    Re: How do I read text files?

    Quote Originally Posted by monarch_dodra View Post
    Code:
    #include <fstream>
    
    int main()
    {
      std::ifstream myfile;
    }
    oh std::fstream thanks!

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