-
January 26th, 2011, 06:41 AM
#1
Finding Text File Line Length and Reading into Array in MFC C++
Hi,
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
{
while(fileName.ReadString(strLine))
{
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
}
-
January 26th, 2011, 07:59 AM
#2
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:
Tokenize()
http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
-
April 1st, 2011, 09:46 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|