Importing multiple sets of string values from files






4.68/5 (13 votes)
Jan 18, 2001

154421
An article on importing multiple sets of string values into a program from files (e.g. CSV files).
Introduction
This code shows how you can import string values from a text file into your program.
An example of this would be having a text file in CSV (Comma Separated Value) format, which has three different values (fields) on each line and having around 100 lines. Think of this as each line being a separate record, and each record consists of three values.
As it's relatively simple stuff, I haven't included a sample or code to download, but if people want it, I will add them.
The Process
The process that has to take place to do this operation is relatively simple, and it is made even easier by an undocumented MFC function AfxExtractSubString
. I'll talk more about this function in a minute, but it basically does a lot of the leg work of extracting values from a string by looking for a separator character.
The process that has to take place basically consists of the following steps:
- Open the file
- Find one line of that file at a time
- Extract each field from that one line, and place them in variables
- Do something with these values
- Go to the next line of the file and repeat from step 3
The AfxExtractSubString function
The AfxExtractSubString
function's declaration is this:
BOOL AfxExtractSubString(CString& rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep)
- The first parameter is the
CString
variable you want the extracted value to be placed into. - The second parameter is the full string you want to extract the values from. In this example, it will be a single line of the text file.
- The third parameter is the value you want to extract. For example, if you want to get the first value, you would enter
0
. If you want the third value, you would enter2
. - The fourth and final parameter is the separator that separates the values in the full string.
For the purpose of this article, it will be "," as we are importing a CSV format file, but it could equally be "\t" or any other character that does not appear in any of the actual text values.
The code we will use in this example is the following code:
CString strText = strLine; // A line of the file // Initialise the variables that will hold the values CString strItemName = ""; CString strPicPath = ""; CString strSoundPath = ""; // Extract the first value, and place it in the strItemName variable AfxExtractSubString(strItemName, strText, 0, ','); // Extract the second value, and place it in the strPicPath variable AfxExtractSubString(strPicPath, strText, 1, ','); // Extract the third value, and place it in the strSoundPath variable AfxExtractSubString(strSoundPath, strText, 2, ','); // Do something with these values in the variables
You don't need any extra header file included in order to be able to use this function, neither do you need any .lib files.
This function will find the last value of a string regardless of whether the string ends with the separator character or a new line character.
Reading the text file line by line
The code below shows how to open a text file and read it line by line:
// Open the text file we want CFile cfFile ("C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeRead); CArchive ar (&cfFile, CArchive::load); // Load its contents into a CArchive // Initialise the variable which holds each line's contents CString strLine = ""; if(!ar.ReadString(strLine)) // Read the first line of the CArchive into the variable return; // Failed, so quit out do // Repeat while there are lines in the file left to process { if(strLine.GetLength() == 0) // If the line is empty, skip it continue; // Do something with the line }while(ar.ReadString(strLine));
That's basically it. The code comments describe what's going on, here's the code in its entirety:
// Open the text file we want CFile cfFile ("C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeRead); CArchive ar (&cfFile, CArchive::load); // Load its contents into a CArchive // Initialise the variable which holds each line's contents CString strLine = ""; if(!ar.ReadString(strLine)) // Read the first line of the CArchive into the variable return; // Failed, so quit out do // Repeat while there are lines in the file left to process { if(strLine.GetLength() == 0) // If the line is empty, skip it continue; CString strText = strLine; // A line of the file // Initialise the variables that will hold the values CString strItemName = ""; CString strPicPath = ""; CString strSoundPath = ""; // Extract the first value, and place it in the strItemName variable AfxExtractSubString(strItemName, strText, 0, ','); // Extract the second value, and place it in the strPicPath variable AfxExtractSubString(strPicPath, strText, 1, ','); // Extract the third value, and place it in the strSoundPath variable AfxExtractSubString(strSoundPath, strText, 2, ','); // Do something with these values in the variables }while(ar.ReadString(strLine));
That's it, I'm afraid. This is the easiest way I've found of extracting multiple values from CSV files and importing them into a program.