How to get past a blank line?

When my input file reaches a line that only contains a newline character, that same line keeps being read infinitely and I can read no further.

do
{
c = infile.peek();
infile.get(string, 15);
cout << string;
system("PAUSE");

if (strcmp("\n", string)==0)
infile.getline(string, 1);
else if(strcmp(" Frequencies --", string)==0)
{
infile.getline(string, 256);
outfile << string << endl;
}
else
infile.getline(string, 256);
} while (!infile.eof());
lnwlfx44 wrote:
How to get past a blank line?

-> infile.get();

And if you want to skip consecutive blank lines:

1
2
while (infile.peek()=='\n')
     infile.get();
Hmm, I tried infile.get(char c), but I'll try just "get". Thank you.

Edit: Nope, I'm still getting the same problem

if (infile.peek() == '\n')
{
infile.get();
infile.ignore(1, '\n');
}

Works... but only for the first time a line starts with '\n'.

Edit: Err sorry, make that it correctly disregards the first COUPLE blank lines. But nothing is different about the line it gets stuck on.


I hontestly think using that "ignore" is wrong, but it gets me past my first hurdle of a blank line.


Posting code for someone to see.

int main(int argc, char *argv[])
{
ifstream infile;
ofstream outfile;

char filename[64];
char string[256];
char c;
long linenumber=1;

cout << "Enter filename" << '\n';
cin >> filename;
cin.ignore(150, '\n');

infile.open( strcat(filename, ".OUT"), ios::in);
outfile.open( strcat(filename, ".DAT"), ios::out);

infile.unsetf(ios::skipws);

do
{
cout << linenumber << '\n';
infile.get(string, 15);
cout << string;
system("PAUSE");

if (infile.peek() == '\n')
{
infile.get();
//infile.ignore(1, '\n');
}
else if(strcmp(" Frequencies --", string)==0)
{
infile.getline(string, 256);
outfile << string << endl;
}
else
infile.getline(string, 256);

linenumber++;
} while (!infile.eof());

outfile.close();

cout << "Success";


system("PAUSE");
return EXIT_SUCCESS;
}
Last edited on
I was told to replace the body of my loop with

infile.getline(moo, 256);
if(strncmp(moo, " Frequencies --", 15)==0)
{
string s(moo);
string s2(string(s, 15, 256));
cout << "ding" << endl;
outfile << s2 << endl;
}

and it works.... where moo is the new name for the character array "string."
Topic archived. No new replies allowed.