Click here to Skip to main content
Click here to Skip to main content
Go to top

Importing multiple sets of string values from files

, 17 Jan 2001
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Peter Pearson

United Kingdom United Kingdom
University student in the south of England.
 
Interests: Music (Guitars), computers, swimming...

Comments and Discussions

 
Generalplease help me: i am getting square boxes PinmemberJagath Kiran2-Aug-06 0:49 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 18 Jan 2001
Article Copyright 2001 by Peter Pearson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid