🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #574

Re: Summing a comma delinted file

#574
From: Chris ( Val )
Date: Sun, 23 Sep 2001 10:22
139 lines
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
Technical Details
Message-ID: <DJir7.27761$c5.209698@newsfeeds.bigpond.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.uchicago.edu!newsswitch.lcs.mit.edu!sunqbc.risq.qc.ca!news.maxwell.syr.edu!intgwpad.nntp.telstra.net!newsfeeds.bigpond.com!not-for-mail
References: <5vgr7.27572$c5.208712@newsfeeds.bigpond.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 103c8d,a8ecdb8ba824bdd3
X-Google-Attributes: gid103c8d,public
X-Google-Thread: 114592,a8ecdb8ba824bdd3
X-Google-Attributes: gid114592,public
X-Google-ArrivalTime: 2001-09-23 03:24:35 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.uchicago.edu!newsswitch.lcs.mit.edu!sunqbc.risq.qc.ca!news.maxwell.syr.edu!intgwpad.nntp.telstra.net!newsfeeds.bigpond.com!not-for-mail
From: "Chris \( Val \)" <chrisvalNOSPAM@NOSPAMbigpond.com.au>
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cppbuilder.language
References: <5vgr7.27572$c5.208712@newsfeeds.bigpond.com>
Subject: Re: Summing a comma delinted file
Lines: 142
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
Message-ID: <DJir7.27761$c5.209698@newsfeeds.bigpond.com>
Date: Sun, 23 Sep 2001 20:22:50 +1000
NNTP-Posting-Host: 203.54.59.214
X-Trace: newsfeeds.bigpond.com 1001240547 203.54.59.214 (Sun, 23 Sep 2001 20:22:27 EST)
NNTP-Posting-Date: Sun, 23 Sep 2001 20:22:27 EST
Organization: Telstra BigPond Internet Services (http://www.bigpond.com)