Thread View: alt.comp.lang.learn.c-c++
4 messages
4 total messages
Started by Wayne McDermott
Sun, 23 Sep 2001 07:43
Summing a comma delinted file
Author: Wayne McDermott
Date: Sun, 23 Sep 2001 07:43
Date: Sun, 23 Sep 2001 07:43
75 lines
3370 bytes
3370 bytes
I have a file that consists of consumption values by month for stock. The
file begins with the material number for the item followed by a comma then
the consumption for July97, Aug97 etc. Each line represents one item and is
terminated by the endline character. If there was no consumption for the
month then there will be two commas, for instance the first item is
represented as
462,2,,5,7,8,13,,,5,1
481,,12,5,8,32,9,81,,,5,4
where 462 is the stock number for the first item, 481 is the stock number
for the second etc.
I am attempting to write code that will read the input file, sum the
consumption till it reaches the endl and write the sum to outfile.dat. At
this stage I don't mind that the stock number itself will also be added to
the sum, I can't get it to step past the first number anyway. Here is the
code I have so far
#include <fstream>
int main()
{
using namespace std;
ifstream instream;
ofstream outstream;
instream.open("tblConsumptionByPeriod.txt");
if (instream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
outstream.open("outfile.dat");
if (outstream.fail())
{
cout << "Output file opening failed.\n";
exit(1);
}
int sum = 0;
double next;
instream >> next;
while (!instream.eof()) //While we haven't read to the end of the file
{
while (instream >> next);
{
instream >> next;
do
{
sum = sum + next;
cout << sum << '\n';
outstream << sum << '\n';
instream >> next;
}
while (next != '\n'); //Do till we reach the end of the line
}
}
instream.close();
outstream.close();
return 0;
}
Stepping through the code indicates that the instream >> next command is not
actually moving to the next double and it has me stumped. I probably have
problems to overcome after that but all it does at the moment is endlessly
add 462 to itself and write that to outfile.dat.
Any hints greatly appreciated, I have tried using get and trapping the
commas but that doesn't appear to work either.
TIA
Wayne
Re: Summing a comma delinted file
Author: Chris ( Val )
Date: Sun, 23 Sep 2001 10:22
Date: Sun, 23 Sep 2001 10:22
139 lines
5402 bytes
5402 bytes
"Wayne McDermott" <elhombre@bigpond.com> wrote in message
news:5vgr7.27572$c5.208712@newsfeeds.bigpond.com...
| I have a file that consists of consumption values by month for stock. The
| file begins with the material number for the item followed by a comma then
| the consumption for July97, Aug97 etc. Each line represents one item and is
| terminated by the endline character. If there was no consumption for the
| month then there will be two commas, for instance the first item is
| represented as
|
| 462,2,,5,7,8,13,,,5,1
| 481,,12,5,8,32,9,81,,,5,4
|
| where 462 is the stock number for the first item, 481 is the stock number
| for the second etc.
|
| I am attempting to write code that will read the input file, sum the
| consumption till it reaches the endl and write the sum to outfile.dat. At
| this stage I don't mind that the stock number itself will also be added to
| the sum, I can't get it to step past the first number anyway. Here is the
| code I have so far
|
| #include <fstream>
| int main()
| {
| using namespace std;
|
| ifstream instream;
| ofstream outstream;
|
| instream.open("tblConsumptionByPeriod.txt");
| if (instream.fail())
| {
| cout << "Input file opening failed.\n";
| exit(1);
| }
| outstream.open("outfile.dat");
| if (outstream.fail())
| {
| cout << "Output file opening failed.\n";
| exit(1);
| }
| int sum = 0;
| double next;
|
|
| instream >> next;
| while (!instream.eof()) //While we haven't read to the end of the file
| {
| while (instream >> next);
| {
| instream >> next;
| do
| {
| sum = sum + next;
| cout << sum << '\n';
| outstream << sum << '\n';
| instream >> next;
| }
| while (next != '\n'); //Do till we reach the end of the line
| }
| }
| instream.close();
| outstream.close();
|
| return 0;
| }
|
| Stepping through the code indicates that the instream >> next command is not
| actually moving to the next double and it has me stumped. I probably have
| problems to overcome after that but all it does at the moment is endlessly
| add 462 to itself and write that to outfile.dat.
|
| Any hints greatly appreciated, I have tried using get and trapping the
| commas but that doesn't appear to work either.
|
| TIA
| Wayne
Hi Wayne, when using the bitwise operator to perform extraction, it will
terminate it's reading upon any whitespace it encounters and then move
to the next appropriate block of data. You can use getline( ); and read in the
data into strings then convert that data to double later. The getline( ); will allow
you to specify a delimiter. See below:
This solution is not complete, just gives an idea on how you could do it.
BTW, it would be much easier if you only had one delimiter between
each field, the way it stands you'll also have to work out the number of
times you encountered a delimiter and maybe set up a boolean flag of
some sort for comparison purposes.
Below is a quick and dirty example using a string array, but it's up to you
to figure out the rest.
# include <iostream>
# include <fstream>
# include <string>
# include <cstdlib>
using namespace std; // Generic...
int main( )
{
const int MAX = 15;
string Record[MAX] = {"EMPTY"};
int Count = 0;
// No need to close the files, this is handled automatically
ifstream instream("tblConsumptionByPeriod.txt");
ofstream outstream("outfile.dat");
if (!instream)
{
cout << "Input file opening failed.\n";
EXIT_FAILURE;
}
if (!outstream)
{
cout << "Output file opening failed.\n";
EXIT_FAILURE;
}
while (!instream.eof( ))
{
getline( instream,Record[Count], ',' );
Count++;
}
for(int i = 0; i < MAX; i++)
{
cout << Record[i] << endl;
}
return 0;
}
HTH
Chris Val
Re: Summing a comma delinted file
Author: Rich Churcher
Date: Sun, 23 Sep 2001 22:48
Date: Sun, 23 Sep 2001 22:48
49 lines
2278 bytes
2278 bytes
"Chris \( Val \)" <chrisvalNOSPAM@NOSPAMbigpond.com.au> writes:
> Hi Wayne, when using the bitwise operator to perform extraction,
Er, that's the stream extraction operator :o)
> it will terminate it's reading upon any whitespace it encounters and
> then move to the next appropriate block of data.
...unless noskipws is used.
[ snip ]
> if (!instream)
> {
> cout << "Input file opening failed.\n";
> EXIT_FAILURE;
Don't you mean,
exit( EXIT_FAILURE );
?
> }
>
> if (!outstream)
> {
> cout << "Output file opening failed.\n";
> EXIT_FAILURE;
> }
>
> while (!instream.eof( ))
No, don't do that. eof() only returns true after there has been an
attempt to read past the end of file. Instead, you can test for
success or failure of the input operation:
while( getline( instream, Record[ Count ], ',' ) )
{
// ...
[ examples not compiled or tested ]
--
Cheers, // First time posting? READ THIS:
Rich. // http://snurse-l.org/acllc-c++/faq.html
Re: Summing a comma delinted file
Author: Chris ( Val )
Date: Mon, 24 Sep 2001 00:49
Date: Mon, 24 Sep 2001 00:49
57 lines
2584 bytes
2584 bytes
"Rich Churcher" <churcher@ihug.com.au> wrote in message news:m3sndd34om.fsf@ihug.com.au...
| "Chris \( Val \)" <chrisvalNOSPAM@NOSPAMbigpond.com.au> writes:
|
| > Hi Wayne, when using the bitwise operator to perform extraction,
|
| Er, that's the stream extraction operator :o)
|
|
| > it will terminate it's reading upon any whitespace it encounters and
| > then move to the next appropriate block of data.
|
| ...unless noskipws is used.
'OK'
| [ snip ]
| > if (!instream)
| > {
| > cout << "Input file opening failed.\n";
| > EXIT_FAILURE;
|
| Don't you mean,
|
| exit( EXIT_FAILURE );
|
| ?
OOP's...
| > }
| >
| > if (!outstream)
| > {
| > cout << "Output file opening failed.\n";
| > EXIT_FAILURE;
| > }
| >
| > while (!instream.eof( ))
|
| No, don't do that. eof() only returns true after there has been an
| attempt to read past the end of file. Instead, you can test for
| success or failure of the input operation:
|
| while( getline( instream, Record[ Count ], ',' ) )
| {
| // ...
Thank's Rich, that looks much better.
Chris Val
| [ examples not compiled or tested ]
|
| --
| Cheers, // First time posting? READ THIS:
| Rich. // http://snurse-l.org/acllc-c++/faq.html
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads