Darn files!! lol

This is a discussion on Darn files!! lol within the C++ Programming forums, part of the General Programming Boards category; I'm using Visual C++ 6.0 and I got this program but it won't work. Program: Code: #include <iostream> #include <fstream> ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    16

    Question Darn files!! lol

    I'm using Visual C++ 6.0 and I got this program but it won't work.

    Program:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	fstream file;
    	
    
    	int x;
    	int j=12;
    
    //get from file
    file.open("data.txt",ios::in);
    
    file >> x;
    
    file.close();
    
    	//put into file
    		file.open("data.txt",ios::out);
    
    		file << j;
    
    		file.close();
    	
    
    	cout << x << endl;
    	cout << j << endl;
    
    	return 0;
    }
    Inside the file i have one number but when i try to write a different number into the same file I get nothing. The original number is erase and i'm left with nothing. Any suggestion...
    Thankx

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    ios::out will erase the content of the file if there's any
    ios::app will seek to end-of-file before each write
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,200
    Also ALWAYS check to make sure the file is open. I tested your code and when you tried to reopen the file for writing it wouldnt open, so ALWAYS add code to check if the file open successfully.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    16
    When i use the "ios:ut" it erase the value within the file and nothing else. When i use the "ios::app" the number remain the same and no other value was added into the file. Any other ideas....
    < I need to read and write to the same file>!!

    Thankx

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Again I am not sure let me know if I am wrong.

    For fstream if i remember correctly you can combine the parameters with a | so it can do both reading and writing to the file?

    Code:
    file.open("data.txt",ios::in | ios::out);
    Last edited by manofsteel972; 03-03-2004 at 01:23 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    16
    Yeah, I try that before and again after mentioning file.open("data.txt",ios::in| ios:ut); and that didn't work either.Is there any other way??
    Thanx

  7. #7
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    I can get them to work sepearately just not together.

    I am as puzzled as you are. I can get it to work if i only use one or the other but not both. You could define and ifstream for input and an ofstream for output that works.

    Code:
    ifstream file_in;
    ofstream file_out;
    
    file_in.open("data.txt",ios::in);
    
    
    file_out.open("data.txt",ios::app);
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
      fstream file;
    
      int x;
      int j=12;
    
      //  open for reading
      file.open("data.txt",ios::in);
      if (!file)
        cout << "could not open file" << endl;
    
      file >> x;
    
      file.close();
      
      // once stream object is disconnected,
      // no further access is allowed unless
      // is reset
      file.clear();    
      
      // open file for writing, append to end of file
      file.open("data.txt", ios::out | ios::app);
      if (!file)
        cout << "could not open file" << endl;
    
      file << j;
    
      file.close();	
    
      cout << x << endl;
      cout << j << endl;
    
      return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  5. Lol!
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2002, 11:29 AM

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