Want to read a txt file

Hi,
I want to read a txt file, but I want to read word by word. So can you please show what function would be best to read word by word from the txt file Thanks.
vcboy


Answer this question

Want to read a txt file

  • Eric Tsai

    Thanks, but I am writing MFC codes only. Do you know how to do it in MFC library
    vcboy

  • shawncm217

    Yeah, I used CStdioFile object and ReadString() as follow:
    CString str;
    CStdioFile Ftest;
    Ftest.Open("test.txt",CFile::modeRead|CFile::modeCreate);
    Ftest.ReadString(str);
    but the code stopped from running with ASSERTION FAILURE message while I debug. Any idea
    Thanks.
    vcboy

  • OlivierJ

    Then you can use the CStdioFile class and ReadString method. It will give you a CString that you can use the Tokenize method to extract words from.

  • PatrickGreen

    Would you mind sharing the assertion message


  • CalgaryDataGirl

    Ftest.Open("test.txt",CFile::modeRead|CFile::modeCreate);
     
    Shoudn't this be:
    Ftest.Open("test.txt"), CFile::modeRead);
     
    with your code it seems to me that you want to open a file readonly, but with CFile::modeCreate you truncate the file to 0 bytes (equals empty the file).
     
    Correct me if i'm wrong though.


  • JohnRM2

    You could use the TextReader and the ReadLine method to read a line. For each line read you can split it on the space character to get an array of words.

  • Want to read a txt file