Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / MFC
Article

Importing multiple sets of string values from files

Rate me:
Please Sign up or sign in to vote.
4.68/5 (14 votes)
17 Jan 2001 150.7K   47   35
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:

  1. Open the file
  2. Find one line of that file at a time
  3. Extract each field from that one line, and place them in variables
  4. Do something with these values
  5. 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; // 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
University student in the south of England.

Interests: Music (Guitars), computers, swimming...

Comments and Discussions

 
GeneralMy vote of 5 Pin
wolfgang wayland19-Jan-12 6:15
wolfgang wayland19-Jan-12 6:15 
GeneralVc++ 6.0 Pin
__alias21-Sep-06 6:37
__alias21-Sep-06 6:37 
Questionremove square box? Pin
clarap3-Aug-06 17:55
clarap3-Aug-06 17:55 
Generalplease help me: i am getting square boxes Pin
Jagath Kiran2-Aug-06 0:49
Jagath Kiran2-Aug-06 0:49 
QuestionAfxExtractSubString(); Pin
kalibus13-Jan-06 2:08
kalibus13-Jan-06 2:08 
GeneralAfxExtractSubString source Pin
KenDang18-Aug-05 5:59
KenDang18-Aug-05 5:59 
Generalstring variable Pin
Micealgleeson16-Feb-05 2:51
Micealgleeson16-Feb-05 2:51 
Question4 C# ? Pin
Anonymous8-Oct-03 3:59
Anonymous8-Oct-03 3:59 
GeneralGood Stuff! Pin
manojb29-Aug-03 5:48
manojb29-Aug-03 5:48 
QuestionCan someone Help Pin
edy3917-Jan-03 16:37
edy3917-Jan-03 16:37 
AnswerRe: Can someone Help Pin
Synetech30-Jul-03 12:44
Synetech30-Jul-03 12:44 
GeneralRe: Can someone Help Pin
st0per31-Jul-04 6:50
st0per31-Jul-04 6:50 
AnswerRe: Can someone Help Pin
tretretrtert14-Jul-06 0:35
tretretrtert14-Jul-06 0:35 
AnswerRe: Can someone Help Pin
tretretrtert14-Jul-06 0:40
tretretrtert14-Jul-06 0:40 
GeneralRe: Can someone Help Pin
tretretrtert14-Jul-06 0:41
tretretrtert14-Jul-06 0:41 
GeneralRe: Can someone Help Pin
tretretrtert14-Jul-06 0:41
tretretrtert14-Jul-06 0:41 
AnswerRe: Can someone Help Pin
tretretrtert14-Jul-06 0:43
tretretrtert14-Jul-06 0:43 
AnswerRe: Can someone Help Pin
tretretrtert14-Jul-06 0:49
tretretrtert14-Jul-06 0:49 
AnswerRe: Can someone Help Pin
clarap2-Aug-06 17:25
clarap2-Aug-06 17:25 
GeneralCannot work Pin
yixin16-Jan-03 21:27
yixin16-Jan-03 21:27 
GeneralQuoted strings Pin
raykos9-Jan-03 5:24
raykos9-Jan-03 5:24 
GeneralCArchive doesn't work on text Pin
5-Jun-02 8:43
suss5-Jun-02 8:43 
GeneralRe: CArchive doesn't work on text Pin
Synetech30-Jul-03 12:47
Synetech30-Jul-03 12:47 
GeneralRe: CArchive doesn't work on text Pin
Bzepp10-Apr-07 6:42
Bzepp10-Apr-07 6:42 
QuestionHow do I find strings in files? Pin
17-Apr-01 5:00
suss17-Apr-01 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.