Click to See Complete Forum and Search --> : How to read text file?
Yoh-hei
May 14th, 1999, 01:30 AM
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.
Jason Teagle
May 14th, 1999, 01:59 AM
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?
Yoh-hei
May 14th, 1999, 03:06 AM
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.
Jason Teagle
May 14th, 1999, 03:13 AM
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 jteagle@solartron.com and I'll gladly help you.
zephyr79
March 17th, 2003, 04:03 AM
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
Jason Teagle
March 17th, 2003, 07:53 AM
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?
zephyr79
March 17th, 2003, 09:10 AM
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
Jason Teagle
March 19th, 2003, 04:20 AM
Assuming the string with tabs is in a CString object called strLine:
CString strNextToken ;
strNextToken = strLine.SpanExcluding("\t");
The above line, if called repeatedly, will break off one word at a time for you.
Simon666
March 19th, 2003, 04:53 AM
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 (http://www.codeproject.com/file/importstringvalues.asp). I use it to read files with data separated with tabs. If you have any questions, feel free to ask.
zephyr79
March 19th, 2003, 11:23 PM
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. :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.