Hi all,
I'm a beginner to C++, about 4 days into learning it.
I've used Matlab for the last 3 years, and it has made me lazy with all its built in functions and type-non-fussy variables. So I'm learning C++ :)
I've made a program to read in a .csv file, the user selects the headers they want extracted and written to another file. e.g. if the original csv has 57 headers, the user might only be interested in columns 2,3,4,6,8,10,27,
I've managed to extract the data from the chosen columns and display them in the cmd window.
I'm storing it as a string. so if the input csv is...
A, B, C, D, E, F,
11, 21, 31, 41, 51, 61,
12, 22, 32, 42, 52, 62,
and the user selected columns 1,3,5, the output is ...
11, 31, 51,
12, 32, 52,
A the point where I'm using
cout << DataStr;
I'm also using
myfile << DataStr
but it's not working.
Here's the section of code below. I've commented out OutputFile as it wasn't working. I copied the example from
http://www.cplusplus.com/doc/tutorial/files/
but even this doesn't work... it compiles fine and runs no probs so i'm stuck as to where the problem is... :(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
//ofstream OutputFile; //open output file
//OutputFile.open ("DAPout.txt"); //create output file
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
cout<<"Output file created?";
system("PAUSE");
read.open (argv[1]);
if (read.is_open()) { //if it's open
getline (read,line); //skip the first line, they are headers
getline (read,line); //skip the second line, they are units
while ( read.good() ) //while it's still open
{
getline (read,line); //read the data from 'read' obj, store in 'line'
DataStr = DataFromLine(line, ColsToExport, HEADER_LENGTH, DelimChar, NumToFetch);
cout<<"Concat Str " <<DataStr<<"\n";
//OutputFile << DataStr; //write extracted data
}
} else {
cout<<"Failed to read input file \n"; //quit if can't read an input file
return EXIT_SUCCESS;
}
read.close(); //close the inputfile
//OutputFile.close(); //close the output file
|