PLEASE HELP!

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

    #1

    PLEASE HELP!

    #include<fstrea m>
    using namespace std;

    int main()
    {
    ifstream indata;
    ofstream outdata;

    indata.open("D: \\C++\\New Folder\\infile. txt");
    outdata.open("D :\\C++\\New Folder\\outfile .txt");

    int a=0;
    int i=0;
    int b=0;

    while(a<9)
    {
    indata>>i;
    b=i;
    outdata<<b<<" ";
    a=a+1;
    }
    indata.close();
    outdata.close() ;

    return 0;

    }

    This is in the infile.txt

    1
    2
    3
    4
    5
    6
    7
    8
    9

    The infile.txt and the outfile.txt are both in the same folder but when
    i run the program even after deleting the outfile.txt it shows this:
    0 0 0 0 0 0 0 0 0

    rather than this:
    1 2 3 4 5 6 7 8 9

    PLEASE HELP!

  • mlimber

    #2
    Re: PLEASE HELP!

    coinjo wrote:[color=blue]
    > #include<fstrea m>
    > using namespace std;
    >
    > int main()
    > {
    > ifstream indata;
    > ofstream outdata;
    >
    > indata.open("D: \\C++\\New Folder\\infile. txt");
    > outdata.open("D :\\C++\\New Folder\\outfile .txt");[/color]

    Prefer to open with the constructors:

    ifstream indata( "in.txt" );
    ofstream outdata( "out.txt" );

    Note that you didn't check if the open succeeded, and I'm guessing
    that's your problem (perhaps because of the space in the pathname).
    [color=blue]
    >
    > int a=0;
    > int i=0;
    > int b=0;[/color]

    Don't declare variables until you use them. i and b should be declared
    inside the loop. (Actually, you don't need both of them in the first
    place.)
    [color=blue]
    >
    > while(a<9)
    > {
    > indata>>i;
    > b=i;
    > outdata<<b<<" ";
    > a=a+1;[/color]

    "++a;" is more conventional. Even better would be a for loop rather
    than a while loop.
    [color=blue]
    > }[/color]

    You need to check for read failures (due to disk errors, end of file,
    etc.). Prefer the canonical form:

    while( indata >> i )
    {
    outdata << i << ' ';
    }

    You could put a check for the iteration cound in the while condition if
    the input file could have more than 9 pieces of data and you only want
    the first 9 of them:

    while( (indata >> i) && (a++ < 9) )
    {
    outdata << i << ' ';
    }
    [color=blue]
    > indata.close();
    > outdata.close() ;[/color]
    [snip]

    Unnecessary. The destructors do this automatically. You should
    generally only use close when you need to close the file before the end
    of scope.


    Cheers! --M

    Comment

    • Pep

      #3
      Re: PLEASE HELP!

      mlimber wrote:
      [color=blue]
      > coinjo wrote:[color=green]
      >> #include<fstrea m>
      >> using namespace std;
      >>
      >> int main()
      >> {
      >> ifstream indata;
      >> ofstream outdata;
      >>
      >> indata.open("D: \\C++\\New Folder\\infile. txt");
      >> outdata.open("D :\\C++\\New Folder\\outfile .txt");[/color]
      >
      > Prefer to open with the constructors:
      >
      > ifstream indata( "in.txt" );
      > ofstream outdata( "out.txt" );
      >
      > Note that you didn't check if the open succeeded, and I'm guessing
      > that's your problem (perhaps because of the space in the pathname).
      >[color=green]
      >>
      >> int a=0;
      >> int i=0;
      >> int b=0;[/color]
      >
      > Don't declare variables until you use them. i and b should be declared
      > inside the loop. (Actually, you don't need both of them in the first
      > place.)
      >[color=green]
      >>
      >> while(a<9)
      >> {
      >> indata>>i;
      >> b=i;
      >> outdata<<b<<" ";
      >> a=a+1;[/color]
      >
      > "++a;" is more conventional. Even better would be a for loop rather
      > than a while loop.
      >[color=green]
      >> }[/color]
      >
      > You need to check for read failures (due to disk errors, end of file,
      > etc.). Prefer the canonical form:
      >
      > while( indata >> i )
      > {
      > outdata << i << ' ';
      > }
      >
      > You could put a check for the iteration cound in the while condition if
      > the input file could have more than 9 pieces of data and you only want
      > the first 9 of them:
      >
      > while( (indata >> i) && (a++ < 9) )
      > {
      > outdata << i << ' ';
      > }
      >[color=green]
      >> indata.close();
      >> outdata.close() ;[/color]
      > [snip]
      >
      > Unnecessary. The destructors do this automatically. You should
      > generally only use close when you need to close the file before the end
      > of scope.
      >
      >
      > Cheers! --M[/color]

      I would agree with this. Initially I thought this might be a problem local
      to windows as I ran the program on linux and had no problems with it. Then
      I ran on windows with no problems until I deleted the input file.

      Ho hum.

      Comment

      Working...