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

    How to read text file?

    Hello:
    I study use VC++ 6.0 MFC ScrollView Class read a
    text file .How to open the text file and read it use
    VC++ program.I think it different with C program.
    Can VC++ only read binary file?
    How to start?
    Would you help me ?
    Thank you!

    Yoh-Hei.


  2. #2
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: How to read text file?

    To read a text file, construct a CStdioFile object. Call its Open() method with the filename and CFile::modeRead|CFile::typeText as the attributes. Then call its ReadString() method until it returns FALSE (end of file). Then call its Close() method. Like this:

    ---
    BOOL bReadOK ;
    CStdioFile filInput ;
    CString strText ;

    if (filInput.Open("c:\\text\\somefile.txt",
    CFile::modeRead|CFile::typeText) )
    {
    while (TRUE)
    {
    bReadOK = filInput.ReadString(strText);
    if (!bReadOK)
    break ; // EOF.

    // Do something with the string read here...
    }

    filClose();
    }


    ---

    How's that?


    --
    Jason Teagle
    [email protected]

  3. #3
    Join Date
    Apr 1999
    Posts
    79

    Re: How to read text file?

    Hello:
    Mr. Jason Teagle Thank you very much!
    I'm testing your advice.But I first time use the CStdioFile.
    It'S need study (Exp:How to control the read one data of
    one time,endln...)
    If are there examples about use this on internet?
    I use VC++ 6.0 MFC ScrollView class.
    Thank you again!

    Yoh-Hei.




  4. #4
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: How to read text file?

    I'm not quite sure what you mean, but if it helps, each call to ReadString() reads just one line of the text file at a time - up to the <CR><LF> at the end of the line.

    If you want more help with CStdioFile, CString and text files, e-mail me at [email protected] and I'll gladly help you.


    --
    Jason Teagle
    [email protected]

  5. #5
    Join Date
    Feb 2003
    Posts
    35
    Hi, Jason Teagle

    I'm using readstring command to read in data from textfile too.But for my case my data are separated by tab . But Readstring only allows me to read in the data before tab and ignore the rest.
    Eg.

    fmap famp1 famp2.

    It can only managed to read in famp.

    What shld i do? thanks

  6. #6
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710
    I'm not sure what has gone wrong for you, but I just ran a quick test to confirm what I believed - ReadString() should and does (for me) read in the whole line, tabs and all - all pieces of data from that one line in the file appear.

    I attach a GIF showing my VC environment, proving it should work - notice that the tabs show as little blocks in "strLine" in the variable window.

    Can you perhaps create a completely new project and test JUST that part (open a file and read a line), and if it still fails, show us the code to open the file and read it?
    Attached Images Attached Images

  7. #7
    Join Date
    Feb 2003
    Posts
    35
    Hi, i've got that part working already.. Thanks..
    But the tab are represented by a small block. Can i know how to
    extract out the data that are separated by the tab.
    eg for your case
    This|is|a|test // | represent tab
    i need the word "this" to go to 1 edit box
    "is" to another, etc.

    Thank U for ur precious help

  8. #8
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710
    Assuming the string with tabs is in a CString object called strLine:

    Code:
    CString   strNextToken ;
    
    strNextToken = strLine.SpanExcluding("\t");
    The above line, if called repeatedly, will break off one word at a time for you.

  9. #9
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628
    Originally posted by zephyr79
    Hi, i've got that part working already.. Thanks..
    But the tab are represented by a small block. Can i know how to
    extract out the data that are separated by the tab.
    eg for your case
    This|is|a|test // | represent tab
    i need the word "this" to go to 1 edit box
    "is" to another, etc.

    Thank U for ur precious help
    Use AfxExtractSubString as explained over here. I use it to read files with data separated with tabs. If you have any questions, feel free to ask.
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

  10. #10
    Join Date
    Feb 2003
    Posts
    35
    Hi, thanks.
    i've found another method is to use Find and ReverseFind method. might not be very efficient but it does works too. 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