Web Analytics Made Easy -
StatCounter C++ File I/O Problem - CodingForum

Announcement

Collapse
No announcement yet.

C++ File I/O Problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C++ File I/O Problem

    Hey everyone. Here's my problem - I am writing a basic program to deal with file input/ouput. I have two files, test.txt and test2.txt. test.txt is read to variable i, then variable i is output to test2.txt. This works fine. But then, I want to output a different variable, j, to test.txt. This is where the problem occurs. When the program is run, it does not write the variable to the file. I'm posting my code below, if someone could just point me in the right direction as to what I'm doing wrong/what is going wrong I would greatly appreciate it. Also, let me know if you need me to explain the problem any better. Thanks in advance.

    Code:
    int main(int argc, char * argv[]) {
    
      fstream test;
      fstream test2;
    
      int i=0,j=13;
    
      test.open("test.txt",ios::in);
      test2.open("test2.txt");
    
      test>>i;
      test2<<i;
    
      test.close();
      test.open("test.txt",ios::out);
      test<<j;
    
      test.close();
      test2.close();
    	
      return 0;
    }

  • #2
    I'm not sure what to say, this code seems to work fine.
    I started with test1.txt file containing 2, and test2.txt being blank
    After running the code below, test1.txt contained 13, and test2.txt contained 2.
    Running that program again without changing the files gave the following, test1.txt contained 13, test2.txt contained 13. As expected.

    The only changes I made to the code were
    test2.open("test2.txt", ios:: out);
    Added newline characters to the end of the files ('cause I'm anal).

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    int main(int argc, char * argv[])
    {
    
      fstream test1;
      fstream test2;
    
      int i=0;
      int j=13;
    
      test1.open("test1.txt",ios::in);
      test2.open("test2.txt", ios::out);
    
      test1>>i;
      test2<< i << endl;
    
      test1.close();
      test1.open("test1.txt",ios::out);
      test1<< j << endl;
    
      test1.close();
      test2.close();
    
      return 0;
    }

    Comment


    • #3
      Originally posted by Gox View Post
      I'm not sure what to say, this code seems to work fine.
      I started with test1.txt file containing 2, and test2.txt being blank
      After running the code below, test1.txt contained 13, and test2.txt contained 2.
      Running that program again without changing the files gave the following, test1.txt contained 13, test2.txt contained 13. As expected.

      The only changes I made to the code were
      test2.open("test2.txt", ios:: out);
      Added newline characters to the end of the files ('cause I'm anal).
      Ok I modified my code according to yours and it still gave me the same problem. I later figured out (by pure chance) that the program seems to work if I have a blank line following the 2 in test1.txt. When I do this and then run the program, test1.txt contains 13, but test2.txt is still blank. So just out of curiosity, I tried running my program in a different compiler. Originally I was using Cygwin. When I tried it with Visual Studio .NET 2003, the program worked flawlessly provided that I have a blank line following the 2 in test1.txt. I am really curious as to why Cygwin does not work like this. Any ideas? I guess from now on I will just have to use Visual Studio. Thanks for the help.

      Comment


      • #4
        I'm not sure what compiler Cygwin uses, but for reference I used the g++ compiler. Unsure of the version however.

        Comment


        • #5
          Originally posted by Gox View Post
          I'm not sure what compiler Cygwin uses, but for reference I used the g++ compiler. Unsure of the version however.
          Ya I used the g++ compiler through Cygwin. Strange huh. Oh well, Visual Studio works fine I guess so I will use that for now. Thanks for the help.

          Comment


          • #6
            instead of saying test.open("test.txt",ios::in);, you could say test.open(argv[1],ios::in);
            Then when you run it from the command line, say:
            ./name.exe test.txt
            it will open it. didn't know if you knew that or not (i actually just learned about that and thought it was pretty cool).

            Comment

            Working...
            X