Reputation: 1
I've been struggling with this bug for a while. I'm using windows 10 and code::blocks 16.01 MinGW.
I want to compare c to an end line character.
One program on my system runs successfully, just to skip the header line of a file:
while(c!='\n')
{
c = fgetc(traverse);
if(c == EOF)
return(1);
}
where traverse was opened using
fopen("traverse.dat", "r");
however, my other programs:
FILE * infile;
/* Open a large CSV */
infile = fopen("Getty_Megatem.csv", "r");
if(infile == NULL)
{
printf("no file");
return(EXIT_FAILURE);
}
char c = 0;
int i = 0;
/* Opens file successfully */
printf("File opened\n");
/* Count commas in first line */
while(c != '\n');
{
c = fgetc(infile);
if(c == ',')
i++;
if(c == EOF)
return(EXIT_FAILURE);
}
printf("Number of commas: %i\n", i);
fclose(infile);
return(EXIT_SUCCESS);
and
ifstream infile;
char c;
string mystr;
infile.open("ostring.dat");
// Skip a line
while (c!= '\n')
infile >> c;
getline(infile, mystr);
and (the one I REALLY want to work)
ifstream emdata;
string firstline;
char c = 0;
int i = 0;
vector<double> vdata;
// redundancy
vdata.reserve(10000);
// There are ~ 300 doubles per line
emdata.open("Getty_Megatem.csv");
// Skip first line
getline(emdata, firstline);
while(c != '\n' && c != EOF)
{
emdata >> vdata[i] >> c;
cout << vdata[i] << ",";
i++;
if(i == 999)
{
cout << "\n\ni>9999";
break;
}
}
emdata.close();
return 0;
are unsuccessful, they compile and execute then read the stream forever - or until my maximum iterations 9999 is reached. All these files contain new lines.
Upvotes: 0
Views: 66
Reputation: 433
As pointed in the comments, the first one won't work because it have a ; right after the while(). The problem with the other two is that the >> operator don't read '\n' and spaces, they are treated as separators. That's why the last two won't work.
To fix the 1st just remove the ; after the while statement
//while(c != '\n');
while(c != '\n')
{
c = fgetc(infile);
if(c == ',')
i++;
if(c == EOF)
return(EXIT_FAILURE);
}
And here are my suggestions for the others:
2nd - forget the while, if you want to skip a line, get a line and do nothing with it.
ifstream infile;
char c;
string mystr;
infile.open("ostring.dat");
// Skip a line
getline(infile, mystr);
/*while (c!= '\n')
infile >> c;*/
getline(infile, mystr);
3rd - get the line and use stringstream to get the inputs you need
ifstream emdata;
string firstline;
char c = 0;
int i = 0;
vector<double> vdata;
// redundancy
vdata.reserve(10000);
// There are ~ 300 doubles per line
emdata.open("Getty_Megatem.csv");
// Skip first line
getline(emdata, firstline);
//get second line
getline(emdata, firstline);
// replace commas with spaces
std::replace(firstline.begin(),firstline.end(),',',' ');
stringstream ss(firstline);//
while(ss.rdbuf()->in_avail()!=0)//loop until buffer is empty
{
ss >> vdata[i];
cout << vdata[i] << ",";
i++;
if(i == 999)
{
cout << "\n\ni>9999";
break;
}
}
emdata.close();
return 0;
Upvotes: 0
Reputation: 10038
You are using formatted input to get a character when you should be using unformatted input:
char c;
if (cin.get( c )) ...
or
int c;
c = cin.get();
if (c != cin.eof()) ...
The >>
operator removes whitespace, including newlines.
Upvotes: 1