

I would like to throw out some extra information about files. The above diagrams are two bits of important information. One is the general accepted heiarchy for the file streams. The second is one of the ASCII character sets. Knowing these numbers are important for dealing with files.
Consider the following file as an input example:
1,"Indionapolis, IN",$22.33,A1C
2,"Phoenix, AZ",$12.53,A3C
3,"Los Angeles, CA",$26.03,A5C
4,"Huston, TX",$2.83,A3C
5,"Fargo, MN",$42.33,A1C
Try out the following source code; this is the start of an idea to read in such a file. What are the limitations? What are the strengths? How can this work better?
Created with colorer-take5 library. Type 'cpp'
#include <iostream.h>
#include <fstream.h>
int main( )
{
char filename[255];
char tmpStr[300];
char qr;
int tmpint=0;
int x;
cout << "\n\n\nCSV FILE TESTER\n\n\n";
cout << "Enter the name for the CSV File: ";
cin >> filename;
if ( strlen( filename ) == 0 ) {
cerr << "Blank filename" << endl;
exit( 1 );
}
ifstream inFile( filename, ios::in );
qr = ' ';
while ( !inFile.eof() )
{
inFile.getline(tmpStr, 299);
x = 0;
while ( x < strlen(tmpStr) ){
qr = tmpStr[x];
if (qr == ',')
cout << " \n";
else if (qr == '"')
{
cout << '"';
x++;
qr = tmpStr[x];
while ( ( !inFile.eof() ) && ( qr != '"' ) )
{
qr = tmpStr[x];
cout << qr;
x++;
}
cout << '"' << endl;
}
else
cout << qr;
x++;
}
cout << endl;
}
cin.ignore();
return 0;
}
No comments:
Post a Comment