Skipping '\n' & ' 'Characters In A File
I have an assignment that I`m working on that requires me to read characters from a file and input them into an array. The only problem that I`m having thus far is being able to skip over the '\n' and ' ' (space) characters. Can anyone please help me with this? The function in my program that is giving me trouble is as follows:
Code:
void insert(char A[])
{
char nb;
int r=0;
infile.open("lab05.dat", ios::in);
if(!infile)
{
cerr << "Can not find the file\n";
exit(1);
}
while(!infile.eof())
{
infile.get(nb);
while(nb=='\n')
{
infile.ignore();
infile.get(nb);
A[r]=nb;
cout << A[r];
r++;
}
if(nb==' ')
{
infile.get(nb);
A[r]=nb;
cout << A[r];
r++;
}
else
{
A[r]=nb;
cout << A[r];
r++;
}
}
}
Re: Skipping '\n' & ' 'Characters In A File
Looking at just this piece of code, the problems are:
Code:
infile.get(nb); // get a character from the file
while(nb=='\n') // loop while the current char is \n
{
infile.ignore(); // ignore the next character in the file, the one after the \n
infile.get(nb); // get the next character
A[r]=nb; // store it, even if it's a \n or space
cout << A[r];
r++;
}