Home > Software engineering >  MFC to read a text file with data
MFC to read a text file with data

Time:10-07


As shown in figure, I use CStdioFile file, readstring to read text data, the first three rows can read it out, but since the fourth row couldn't read the data, how to remove the Spaces in front of the???

CodePudding user response:

To go up to your code space can use cstrings trim function, if each row are fixed begin a space, that is read from the beginning the second character directly

CodePudding user response:

Directly using the original files in the C language, speaking, reading and writing function,
CStdioFile this class is not for all of the text format can be read by line, encapsulation effect is not very good,

CodePudding user response:

http://bbs.csdn.net/topics/360055953

CodePudding user response:

Your CStdioFile file how did you write the code to read and write?

CodePudding user response:

reference 1st floor sunnyloves response:
to your code to the blank space can be used on cstrings trim function, if each row are fixed begin a space, then directly from the second character just started reading

Thank you for your reply, my code is as follows, the purpose is to obtain the second column of the data (from the third row), and then connect with all good! If you want to start reading from the second characters, how to read? Consult ~ ~ ~
 CStdioFile file; 
File. The Open (sCompleteTmp CStdioFile: : modeRead);
Int k=0;
For (k=0; File. The ReadString (FileContent); K++)
{
If (k & gt;
=2){
A [k]=FileContent. Left (FileContent. Find (', '));
//the FileContent=FileContent. Mid (FileContent. Find (', ') + 1);
B=FileContent [k]. Right (FileContent. Find (', '));
//MessageBox [k] (b);
\ [k - 1) +=b [k] + _T (", ");
}
}

CodePudding user response:

The
reference 4 floor VisualEleven reply:
your CStdioFile file how did you write the code to read and write?

Thank you for your reply, my code is as follows, the purpose is to obtain the second column of the data (from the third row), then a comma is used up!
 CStdioFile file; 
File. The Open (sCompleteTmp CStdioFile: : modeRead);
Int k=0;
//FileLine=0;
For (k=0; File. The ReadString (FileContent); K++)
{

If (k & gt;
=2){
A [k]=FileContent. Left (FileContent. Find (', '));
B=FileContent [k]. Right (FileContent. Find (', '));
\ [k - 1) +=b [k] + _T (", ");
}
}

CodePudding user response:

reference 5 floor maoxige reply:
Quote: refer to 1st floor sunnyloves response:

To go up to your code space can use cstrings trim function, if each row are fixed begin a space, then directly from the second character just started reading

Thank you for your reply, my code is as follows, the purpose is to obtain the second column of the data (from the third row), and then connect with all good! If you want to start reading from the second characters, how to read? Consult ~ ~ ~
 CStdioFile file; 
File. The Open (sCompleteTmp CStdioFile: : modeRead);
Int k=0;
For (k=0; File. The ReadString (FileContent); K++)
{
If (k & gt;
=2){
A [k]=FileContent. Left (FileContent. Find (', '));
//the FileContent=FileContent. Mid (FileContent. Find (', ') + 1);
B=FileContent [k]. Right (FileContent. Find (', '));
//MessageBox [k] (b);
\ [k - 1) +=b [k] + _T (", ");
}
}

You sure each row are \ n line feed this first second from which line 3, the beginning is a space or TAB?

CodePudding user response:

You look debugging, should be read in, a trim can remove Spaces

CodePudding user response:

File each time read directly read a whole line and then read the entire line. LeftTrim (); RightTrim (); Direct the Trim () likely to not to drop space

CodePudding user response:

references 9 f SWWLLX response:
file each read directly read a whole line and then read the entire line. LeftTrim (); RightTrim (); Direct the Trim () likely to out space

Hello, go to space I know, but the question now starts I can't read data in the fourth row, don't know why?

CodePudding user response:

refer to the eighth floor xianglitian response:
you look debugging, should be read in, take out a blank trim can

Step by step, I'm debugging, debugging, using messagebox output, is read only the first three lines of data, the fourth row began to read not to come out, whether CStdioFile readstring to encounter the beginning is a space data will not be able to read?

CodePudding user response:

refer to 7th floor sunnyloves response:
Quote: refer to fifth floor maoxige response:

Quote: refer to 1st floor sunnyloves response:

To go up to your code space can use cstrings trim function, if each row are fixed begin a space, then directly from the second character just started reading

Thank you for your reply, my code is as follows, the purpose is to obtain the second column of the data (from the third row), and then connect with all good! If you want to start reading from the second characters, how to read? Consult ~ ~ ~
 CStdioFile file; 
File. The Open (sCompleteTmp CStdioFile: : modeRead);
Int k=0;
For (k=0; File. The ReadString (FileContent); K++)
{
If (k & gt;
=2){
A [k]=FileContent. Left (FileContent. Find (', '));
//the FileContent=FileContent. Mid (FileContent. Find (', ') + 1);
B=FileContent [k]. Right (FileContent. Find (', '));
//MessageBox [k] (b);
\ [k - 1) +=b [k] + _T (", ");
}
}

You sure each row are \ n line feed this first second from which line 3, the beginning is a space or TAB?

Is a newline, ordinary line, the third row is a screenshot of the third line in my questions, namely, "" 000056", "since the fourth line is a space in the beginning, not the TAB! Problem is I couldn't attend the data since the fourth row, I debug, the first three rows of data can be read

CodePudding user response:

 
ReadString (FileContent);//ignore line 1
ReadString (FileContent);//ignore line 2
ReadString (FileContent);//ignore line 3
ReadString (FileContent);//in the debug to see what is FileContent content
Cstrings sFirst sSecond;
AfxExtractSubString (FileContent sFirst, 0, ' ');//is a space in the single quotation marks, to get the first number
AfxExtractSubString (FileContent sSecond, 1, ', ');//get the number 2

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related