Reading delimited files...
-
As you probably noticed from this post I am a very new newbie to VC++. Here is my problem. I need to read in a test file that might have a format like this... string, int, int. I know in java you can use a string tokenizer to read in each value separatly and by line. I can't seem to figure this out in C++. This is what I have been doing. CFile myFile("test.txt", CFile::modeWrite); BYTE buffer[4096]; CString input; input = myFile.Read(buffer, 4096); Once I get it into a string object I can use the method SpanExcluding to get a string from the beginning to the first character I specify not to read. I can't seem to get past it thought. I know my approach is probably all wrong so I don't care if you tear it to shreads. I frequently visit this site and know you guys know what you;re doing so thanks for any advice in advance. Joshua
-
As you probably noticed from this post I am a very new newbie to VC++. Here is my problem. I need to read in a test file that might have a format like this... string, int, int. I know in java you can use a string tokenizer to read in each value separatly and by line. I can't seem to figure this out in C++. This is what I have been doing. CFile myFile("test.txt", CFile::modeWrite); BYTE buffer[4096]; CString input; input = myFile.Read(buffer, 4096); Once I get it into a string object I can use the method SpanExcluding to get a string from the beginning to the first character I specify not to read. I can't seem to get past it thought. I know my approach is probably all wrong so I don't care if you tear it to shreads. I frequently visit this site and know you guys know what you;re doing so thanks for any advice in advance. Joshua
Joshua, use CStdioFile instead of CFile; it lets you read in a line at a time. For grabbing the tokens, you can use the CRT function strtok(), or the undocumented MFC function AfxExtractSubString(). You use the function thus: // assuming you have a CString sLine that holds the next line... CString sStringToken, sIntToken1, sIntToken2; AfxExtractSubString ( sStringToken, sLine, 0, ',' ); AfxExtractSubString ( sIntToken1, sLine, 1, ',' ); AfxExtractSubString ( sIntToken2, sLine, 2, ',' ); The prototype for AfxExtractSubString() is: BOOL AfxExtractSubString(CString& rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '\n'); where rString is where the token is placed, lpszFullString is the string to search, iSubString is the token to pull out (a 0-based count), and chSep is the delimiter character.
-
As you probably noticed from this post I am a very new newbie to VC++. Here is my problem. I need to read in a test file that might have a format like this... string, int, int. I know in java you can use a string tokenizer to read in each value separatly and by line. I can't seem to figure this out in C++. This is what I have been doing. CFile myFile("test.txt", CFile::modeWrite); BYTE buffer[4096]; CString input; input = myFile.Read(buffer, 4096); Once I get it into a string object I can use the method SpanExcluding to get a string from the beginning to the first character I specify not to read. I can't seem to get past it thought. I know my approach is probably all wrong so I don't care if you tear it to shreads. I frequently visit this site and know you guys know what you;re doing so thanks for any advice in advance. Joshua
To add to what Michael has given you... Text file are usually much easier to handle if you use CStdioFile instead of CFile. CStdioFile allows you to easly read each line in a loop. For example... CStdioFile f; // construct a CStdioFile object char buffer[256]; // define a temp buffer while (f.ReadString(szBuffer, 255)) // tokenize string and process Roger C.
-
Joshua, use CStdioFile instead of CFile; it lets you read in a line at a time. For grabbing the tokens, you can use the CRT function strtok(), or the undocumented MFC function AfxExtractSubString(). You use the function thus: // assuming you have a CString sLine that holds the next line... CString sStringToken, sIntToken1, sIntToken2; AfxExtractSubString ( sStringToken, sLine, 0, ',' ); AfxExtractSubString ( sIntToken1, sLine, 1, ',' ); AfxExtractSubString ( sIntToken2, sLine, 2, ',' ); The prototype for AfxExtractSubString() is: BOOL AfxExtractSubString(CString& rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '\n'); where rString is where the token is placed, lpszFullString is the string to search, iSubString is the token to pull out (a 0-based count), and chSep is the delimiter character.
-
To add to what Michael has given you... Text file are usually much easier to handle if you use CStdioFile instead of CFile. CStdioFile allows you to easly read each line in a loop. For example... CStdioFile f; // construct a CStdioFile object char buffer[256]; // define a temp buffer while (f.ReadString(szBuffer, 255)) // tokenize string and process Roger C.