for loop?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LT

    for loop?

    Good day!
    I'm trying to read in values from input file (in_file)
    and add them to another file (out_file)
    Repeat the same process but opening a different file...
    I'm trying to use a combination of for loop (2 run, does not work) and while
    loops.

    Any help on this will be great...
    This is probably a very, very simple question and I apologize for that.

    I declared the files as follow

    ifstream in_file;
    ofstream out_file;
    out_file.open(" outputNumbers.t xt", ios::out);

    //I'll like to use a loop for
    this...

    for (int i=1; i<5; i++)
    {
    out_file <<"\n **running run number "<<i;
    out_file <<"\n ----------------------";
    if (i==1)
    {
    in_file.open("i n_data", ios::in); //Opening my first input
    file

    while (!in_file.eof() ) // while the file is not empty
    {
    in_file >> number;
    out_file<<"\nNu mber from the in_data file is "<<number;
    }
    in_file.close() ; // closing the file, so that I can open a
    different file
    }

    if (i==2) // on my second run, this should happen, BUT it isn't
    {
    in_file.open("i n_bonus", ios::in);

    while (!in_file.eof() )
    {
    out_file<<"\nOp ening in_Bonus file";
    in_file >>number;
    out_file <<"\nNumber from the in_bonus file is "<<number;
    }
    in_file.close() ;
    }
    }
    }


    -LT


  • Rolf Magnus

    #2
    Re: for loop?

    LT wrote:
    [color=blue]
    > Good day!
    > I'm trying to read in values from input file (in_file)
    > and add them to another file (out_file)
    > Repeat the same process but opening a different file...
    > I'm trying to use a combination of for loop (2 run, does not work) and
    > while loops.[/color]

    Define "does not work".
    [color=blue]
    >
    > Any help on this will be great...
    > This is probably a very, very simple question and I apologize for
    > that.
    >
    > I declared the files as follow[/color]

    Please always post _full_ compilable code, i.e. a minimal, but complete
    program (i.e. including a main function and all header includes). There
    may be things wrong in parts you snipped out, or others may want to try
    your code to find out what's wrong with it.
    [color=blue]
    >
    > ifstream in_file;
    > ofstream out_file;
    > out_file.open(" outputNumbers.t xt", ios::out);[/color]

    ofstreams are by default opended as ios::out. That's why they are
    ofstreams ;-)
    [color=blue]
    > //I'll like to use a loop
    > for
    > this...
    >
    > for (int i=1; i<5; i++)
    > {
    > out_file <<"\n **running run number "<<i;
    > out_file <<"\n ----------------------";
    > if (i==1)
    > {
    > in_file.open("i n_data", ios::in); //Opening my first
    > input
    > file
    >
    > while (!in_file.eof() ) // while the file is not
    > empty[/color]

    eof() doesn't mean empty. It means the end of the file was reached. And
    you're not using it correctly, since eof() will only return true
    _after_ you tried to read beyond the end of the file. So your loop will
    do one iteration too much. Also, you don't catch any real errors. Your
    program will go to an endless loop if there is any error reading the
    file. Replace your loop with:

    while (in_file >> number)
    out_file<<"\nNu mber from the in_data file is"<<number;

    if (!in_file.eof() )
    sd::cerr << "Error reading file\n";
    [color=blue]
    > {
    > in_file >> number;
    > out_file<<"\nNu mber from the in_data file is
    > "<<number;
    > }
    > in_file.close() ; // closing the file, so that I can open
    > a
    > different file[/color]

    Closing a file will _not_ reset its status bits. I.e. if you want to
    reuse the stream for another file, your stream will still be in fail
    state. Try adding an:

    in_file.clear() ;
    [color=blue]
    > }
    >
    > if (i==2) // on my second run, this should happen, BUT it
    > isn't
    > {
    > in_file.open("i n_bonus", ios::in);
    >
    > while (!in_file.eof() )
    > {
    > out_file<<"\nOp ening in_Bonus file";
    > in_file >>number;
    > out_file <<"\nNumber from the in_bonus file is
    > "<<number;
    > }
    > in_file.close() ;
    > }
    > }
    > }[/color]

    Btw, your outer loop isn't very useful and makes the code harder to
    understand.

    Comment

    • Charles LaCour

      #3
      Re: for loop?


      "LT" <TrYiNg2PrOtEcT @mYpRiVaCy.com> wrote in message
      news:Nr2dnYPCLP 4HOkei4p2dnA@co mcast.com...[color=blue]
      > Good day!
      > I'm trying to read in values from input file (in_file)
      > and add them to another file (out_file)
      > Repeat the same process but opening a different file...
      > I'm trying to use a combination of for loop (2 run, does not work) and[/color]
      while[color=blue]
      > loops.
      >
      > Any help on this will be great...
      > This is probably a very, very simple question and I apologize for that.
      >
      > I declared the files as follow
      >
      > ifstream in_file;
      > ofstream out_file;
      > out_file.open(" outputNumbers.t xt", ios::out);
      >
      > //I'll like to use a loop for
      > this...
      >
      > for (int i=1; i<5; i++)
      > {
      > out_file <<"\n **running run number "<<i;
      > out_file <<"\n ----------------------";
      > if (i==1)
      > {
      > in_file.open("i n_data", ios::in); //Opening my first input
      > file
      >
      > while (!in_file.eof() ) // while the file is not empty
      > {
      > in_file >> number;
      > out_file<<"\nNu mber from the in_data file is "<<number;
      > }
      > in_file.close() ; // closing the file, so that I can open a
      > different file
      > }
      >
      > if (i==2) // on my second run, this should happen, BUT it[/color]
      isn't[color=blue]
      > {
      > in_file.open("i n_bonus", ios::in);
      >
      > while (!in_file.eof() )
      > {
      > out_file<<"\nOp ening in_Bonus file";
      > in_file >>number;
      > out_file <<"\nNumber from the in_bonus file is "<<number;
      > }
      > in_file.close() ;
      > }
      > }
      > }
      >
      >
      > -LT
      >
      >[/color]

      #include <iostream.h>
      #include <fstream.h>

      int main()
      {
      char *inDataFile[] = { "inData01.t xt", "inData02.t xt", "inData03.t xt",
      "inData04.t xt", "inData05.t xt", "inData06.t xt" };
      ifstream src; // Source file object
      ofstream dst; // Destination file object
      int i, number;
      int fileCount = sizeof (inDataFile) / sizeof (inDataFile[ 0 ]);

      // Open the output file. If it won't open, return with failure.
      dst.open( "outData.tx t", ios::out );
      if ( !dst )
      return -1;

      for ( i = 0; i < fileCount; ++i )
      {
      src.open( inDataFile[ i ], ios::in | ios::nocreate );
      if ( src.fail() )
      {
      continue;
      }

      // Read each number from the source file
      // and send it to the destination file.
      src >> number;
      while ( !src.fail() )
      {
      dst << number << endl;
      src >> number;
      }

      src.close();
      }

      dst.close();

      return 0;
      }


      Comment

      • Unforgiven

        #4
        Re: for loop?

        Charles LaCour wrote:[color=blue]
        > #include <iostream.h>
        > #include <fstream.h>[/color]

        #include <iostream>
        #include <fstream>
        using namespace std;

        --
        Unforgiven

        "Most people make generalisations "
        Freek de Jonge

        Comment

        • LT

          #5
          Re: for loop?

          Thank you all for all the Great info,
          next time I'll post the entire code, sorry about that, I figured less is
          better...

          Also, I now know what I was doing wrong, a combination of things including
          not knowing the " in_file.clear() ; "
          to clear the stream...

          Thank you all,
          LT
          "Charles LaCour" <YouDontKnowMe@ phantom.net> wrote in message
          news:toBCb.1997 $i55.1317@fed1r ead06...[color=blue]
          >
          > "LT" <TrYiNg2PrOtEcT @mYpRiVaCy.com> wrote in message
          > news:Nr2dnYPCLP 4HOkei4p2dnA@co mcast.com...[color=green]
          > > Good day!
          > > I'm trying to read in values from input file (in_file)
          > > and add them to another file (out_file)
          > > Repeat the same process but opening a different file...
          > > I'm trying to use a combination of for loop (2 run, does not work) and[/color]
          > while[color=green]
          > > loops.
          > >
          > > Any help on this will be great...
          > > This is probably a very, very simple question and I apologize for that.
          > >
          > > I declared the files as follow
          > >
          > > ifstream in_file;
          > > ofstream out_file;
          > > out_file.open(" outputNumbers.t xt", ios::out);
          > >
          > > //I'll like to use a loop[/color][/color]
          for[color=blue][color=green]
          > > this...
          > >
          > > for (int i=1; i<5; i++)
          > > {
          > > out_file <<"\n **running run number "<<i;
          > > out_file <<"\n ----------------------";
          > > if (i==1)
          > > {
          > > in_file.open("i n_data", ios::in); //Opening my first[/color][/color]
          input[color=blue][color=green]
          > > file
          > >
          > > while (!in_file.eof() ) // while the file is not empty
          > > {
          > > in_file >> number;
          > > out_file<<"\nNu mber from the in_data file is "<<number;
          > > }
          > > in_file.close() ; // closing the file, so that I can open a
          > > different file
          > > }
          > >
          > > if (i==2) // on my second run, this should happen, BUT it[/color]
          > isn't[color=green]
          > > {
          > > in_file.open("i n_bonus", ios::in);
          > >
          > > while (!in_file.eof() )
          > > {
          > > out_file<<"\nOp ening in_Bonus file";
          > > in_file >>number;
          > > out_file <<"\nNumber from the in_bonus file is[/color][/color]
          "<<number;[color=blue][color=green]
          > > }
          > > in_file.close() ;
          > > }
          > > }
          > > }
          > >
          > >
          > > -LT
          > >
          > >[/color]
          >
          > #include <iostream.h>
          > #include <fstream.h>
          >
          > int main()
          > {
          > char *inDataFile[] = { "inData01.t xt", "inData02.t xt", "inData03.t xt",
          > "inData04.t xt", "inData05.t xt",[/color]
          "inData06.t xt" };[color=blue]
          > ifstream src; // Source file object
          > ofstream dst; // Destination file object
          > int i, number;
          > int fileCount = sizeof (inDataFile) / sizeof (inDataFile[ 0 ]);
          >
          > // Open the output file. If it won't open, return with failure.
          > dst.open( "outData.tx t", ios::out );
          > if ( !dst )
          > return -1;
          >
          > for ( i = 0; i < fileCount; ++i )
          > {
          > src.open( inDataFile[ i ], ios::in | ios::nocreate );
          > if ( src.fail() )
          > {
          > continue;
          > }
          >
          > // Read each number from the source file
          > // and send it to the destination file.
          > src >> number;
          > while ( !src.fail() )
          > {
          > dst << number << endl;
          > src >> number;
          > }
          >
          > src.close();
          > }
          >
          > dst.close();
          >
          > return 0;
          > }
          >
          >[/color]


          Comment

          Working...