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 enter 2
.
- 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;
CString strItemName = "";
CString strPicPath = "";
CString strSoundPath = "";
AfxExtractSubString(strItemName, strText, 0, ',');
AfxExtractSubString(strPicPath, strText, 1, ',');
AfxExtractSubString(strSoundPath, strText, 2, ',');
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:
CFile cfFile ("C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeRead);
CArchive ar (&cfFile, CArchive::load);
CString strLine = "";
if(!ar.ReadString(strLine))
return;
do
{
if(strLine.GetLength() == 0)
continue;
}while(ar.ReadString(strLine));
That's basically it. The code comments describe what's going on, here's the code in its entirety:
CFile cfFile ("C:\\TextFile.txt", CFile::modeNoTruncate | CFile::modeRead);
CArchive ar (&cfFile, CArchive::load);
CString strLine = "";
if(!ar.ReadString(strLine))
return;
do
{
if(strLine.GetLength() == 0)
continue;
CString strText = strLine;
CString strItemName = "";
CString strPicPath = "";
CString strSoundPath = "";
AfxExtractSubString(strItemName, strText, 0, ',');
AfxExtractSubString(strPicPath, strText, 1, ',');
AfxExtractSubString(strSoundPath, strText, 2, ',');
}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.
University student in the south of England.
Interests: Music (Guitars), computers, swimming...