File I/0

This is a discussion on File I/0 within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble with some file i/o coding.. I'm not sure why i can't get it to work. Code: #include ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    US
    Posts
    2

    File I/0

    I'm having trouble with some file i/o coding.. I'm not sure why i can't get it to work.

    Code:
    #include <fstream.h>
    #include <iostream>
    #include <string.h>
    #include <cstdlib>
    #include <stdlib.h>
    
    int main()
    {
      int aa, bb, cc, a, b, c;
      aa=1, bb=2, cc=3, a=3, b=1, c=2;
    
      ofstream abc;
      abc.open ( "example.txt" );
    
      abc << aa << " " << bb << " " << cc << endl;
      abc << "This is a test" << endl;
      abc.close();
    
      ifstream cba ( "example.txt" );
      if ( !abc.is_open() )
      { cout << "Nuttin" << endl; }
      else
      {  cba >> a >> " " >> b >> " " >> c;
      cout << a << " " << b << " " << c;  }
      cba.close();
      return 0;
    }

    sorry its a little long, but i'm lost with this whole file i/o stuff. none of the tutorials or information about it is helping me any.

    I have been unable to compile this so that it goes into the else statement under ifstream. and that is as far as i have been able to narrow the problem down.. Thanks if anyone helps..

  2. #2
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Code:
    #include <fstream>
    #include <iostream>
    
    int main()
    {
      int aa, bb, cc, a, b, c;
      aa=1, bb=2, cc=3, a=3, b=1, c=2;
      fstream abc;
    
      abc.open ( "example.txt" ,fstream::out);
    
      abc << aa << " " << bb << " " << cc << endl;
      abc << "This is a test" << endl;
      abc.close();
    
      abc.open("example.txt" ,fstream::in);
      if ( !abc.is_open() )
      { cout << "Nuttin" << endl;return 0; }
    
      abc>>a>>b>>c;
      cout<<a<<" "<<b<<" "<<c<<endl;;
      abc.close();
      return 0;
    }
    is this what you need?

    blow me ... ...

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    ifstream cba ( "example.txt" );
    if ( !abc.is_open() )

    you open cba, then you test abc which you closed earlier

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    US
    Posts
    2
    I have fixed my problem for the File i/o but i have a new one. When i moved my code to a different computer it stopped writing the information to the text file. I have no clue what could be stopping it with the exeption of the change of computer.. cause it was working.

    I went from a windows '98 to a windows 2000 professional and it quit working. But I had also gone from a windows '98 to a windows XP and it still worked. I havent changed the code any and i tested Hermitsky's code here and even that wont write to the file, while it had on the other two computers.

    The text file and code file are in the same directory too, the same as when it was working.

    Thanks for the help so far, I hope to figure out this problem soon..
    Last edited by beryc; 04-18-2005 at 11:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21