Thread: file i/o problems

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    file i/o problems

    i cant figure out how to fix this!!!

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        ofstream legs;
        legs.open("legs.txt");
        legs<<"legs,legs,legs"<<endl;
        legs.close();
        system("pause");
        ifstream legs("legs.txt")
        return 0;
    };
    its suppose to make a file called legs and make the text in the file legs,legs,legs and then its suppose to open the previously created legs file!! but it doesnt

    and the log of the errors
    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\Makefile.win"
    Executing make...
    make.exe -f "C:\Dev-Cpp\Makefile.win" all
    g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

    main.cpp: In function `int main()':
    main.cpp:13: error: conflicting declaration 'std::ifstream legs'
    main.cpp:8: error: 'legs' has a previous declaration as `std:fstream legs'
    main.cpp:13: error: declaration of `std::ifstream legs'
    main.cpp:8: error: conflicts with previous declaration `std:fstream legs'

    make.exe: *** [main.o] Error 1

    Execution terminated

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you have declared legs to be an ofstream and then you redeclare it to be an ifstream.
    use a different variable name.
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    it was that easy wow ill try it 1 sec

    EDIT:well i didnt get any errors but how do i get the words legs,legs,legs to appear off the file ?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Something like this ?
    Code:
    int main() {
        ofstream legs;
        legs.open("legs.txt");
        legs<<"legs,legs,legs"<<endl;
        legs.close();
        
        ifstream ilegs("legs.txt");
        string s;
        ilegs >> s;
        cout << s << endl;
        return 0;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM