CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Jan 2011
    Durham, UK

    Finding Text File Line Length and Reading into Array in MFC C++


    I'm a newbie to C++ and MFC environments, so I'm sorry if this question is really obvious to answer but I've been searching forums and can't find a solution.

    Basically, I have a programme that reads a CSV file containing lines of data including a title for each line and a series of (x, y) coordinates. However, each line is of a different length (note the first entry is the name of the data line, the second, fourth etc. are x-coordinates and third, fifth etc. are y-coordinates) e.g.:

    Data 1,0.000,0.000,0.250,1.125,0.500,2.250,0.750,1.125,1.000,0.000
    Data 2,0.000,0.000,0.500,1.000,1.000,0.000

    I want my code to read in the file, assign the first entry to an array of names of data, and then assign the coordinates to two arrays, one for the x-coordinates and one for the y-coordinates. I've written the code below to open the file and read the data from a text file where I know the length of the line is of 11 items (i.e. "Data 1" line above) but obviously this causes problems when having lines of shorter or longer length. I've assumed the key to this problem (hence the question title!) is being able to find the length of the line of the input file, but I haven't been able to figure it out myself.

    Thanks for reading my post, and I hope you're able to offer me some assistance.

    // arrays to store data
    CArray<CString, CString> arrayDataName;
    CArray<double, double> arrayDataX;
    CArray<double, double> arrayDataY;

    // variables for use in reading CSV file
    CStdioFile fileName;
    CString strLine, strValue;
    int strPos, prevComma, nextComma, nValues;

    if(fileName.Open("C:\\Data.csv", CFile::modeRead)) // open file
    prevComma = -1; // enables initial loop to begin at zero

    // read CSV file of saved data
    for(nValues = 0; nValues < 12; nValues++)
    strValue = _T(""); // reset string

    // find position of end of line or next comma
    if(nValues == 11) nextComma = strLine.GetLength();
    else nextComma = strLine.Find(_T(","), prevComma + 1);
    for(strPos = prevComma + 1; strPos < nextComma; strPos++) strValue += strLine.GetAt(strPos); // read data

    // assign data to correct array
    if(nValues == 0) arrayDataName.Add(strValue);
    else if(nValues % 2 == TRUE) arrayDataX.Add(atof(strValue));
    else if(nValues % 2 == FALSE) arrayDataY.Add(atof(strValue));

    prevComma = nextComma; // set current end point as next start point
    fileName.Close(); // close file

  2. #2
    Join Date
    Aug 2000
    West Virginia

    Re: Finding Text File Line Length and Reading into Array in MFC C++

    Instead of hardwiring 12 (or any other number), loop until
    no more commas are found (making sure to still count the
    last field). You might consider using the CString member function:


  3. #3
    Join Date
    Jan 2011
    Durham, UK

    Re: Finding Text File Line Length and Reading into Array in MFC C++

    Yep, that worked perfectly, tokenize() is the way to go!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.