Reputation: 496
I need to write a program that reads numbers from a file, removes the commas, and writes the numbers to a new file. I have managed to do most for the work, but I don't know how to add the spaces between the numbers in the new file. This is my program
int main()
{
ifstream numbersDs; // input: numbers data stream
ofstream nubersNoComma;
projDit(); // Project details
// Check if the files exist.
numbersDs.open(inFile);
nubersNoComma.open(outFile);
char c;
numbersDs >> c;
while (!numbersDs.eof())
{
while ((c != ',') && (!numbersDs.eof())) \\ another .eof to avoid inf loop
{
cout << c ;
nubersNoComma << c;
numbersDs >> c;
}
cout << c ;
numbersDs >> c;
}
nubersNoComma.close();
numbersDs.close();
}
this is the input:
148,540 5345 34,456 2 1,002
this is the output:
14854053453445621002
Upvotes: 2
Views: 167
Reputation: 25154
This code should help you, as "Dan Hook" mentioned, you have to further consider skipping white spaces when using stringstreams, fstreams.
while (!numbersDs.eof())
{
while ((c != ',') && (!numbersDs.eof()))
{
cout << c ;
nubersNoComma << c;
numbersDs>>std::noskipws >> c;
}
nubersNoComma << " ";
cout << c ;
numbersDs >> c;
}
Upvotes: 0
Reputation: 7088
ifstream
is eating the whitespace. Add the following:
numbersDs >> std::noskipws;
Upvotes: 2