File I/O

This is a discussion on File I/O within the C++ Programming forums, part of the General Programming Boards category; Code: case 's': ofstream save_file ("mudsave.txt"); save_file<<"Test Test Test"; save_file.close(); ifstream load_file ("mudsave.txt"); cout<<load_file; cin.get(); break; This is just a ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    File I/O

    Code:
    	case 's':
    	ofstream save_file ("mudsave.txt");
    	save_file<<"Test Test Test";
    	save_file.close();
    	ifstream load_file ("mudsave.txt");
    	cout<<load_file;
    	cin.get();
    	break;
    This is just a small part of my code where I am, of course, trying to read and write a file. These are the errors I get when I try to compile it:

    Code:
    /home/account/cpp/mud.cpp: In function `int main(int, char**)':
    /home/account/cpp/mud.cpp:218: error: jump to case label
    /home/account/cpp/mud.cpp:212: error:   crosses initialization of `
       std::ifstream load_file'
    /home/account/cpp/mud.cpp:209: error:   crosses initialization of `
       std::ofstream save_file'
    /home/account/cpp/mud.cpp:221: error: jump to case label
    /home/account/cpp/mud.cpp:212: error:   crosses initialization of `
       std::ifstream load_file'
    /home/account/cpp/mud.cpp:209: error:   crosses initialization of `
       std::ofstream save_file'
    /home/account/cpp/mud.cpp:250: error: jump to case label
    /home/account/cpp/mud.cpp:212: error:   crosses initialization of `
       std::ifstream load_file'
    /home/account/cpp/mud.cpp:209: error:   crosses initialization of `
       std::ofstream save_file'
    /home/account/cpp/mud.cpp:255: error: jump to case label
    /home/account/cpp/mud.cpp:212: error:   crosses initialization of `
       std::ifstream load_file'
    /home/account/cpp/mud.cpp:209: error:   crosses initialization of `
       std::ofstream save_file'
    /home/account/cpp/mud.cpp:218: warning: destructor needed for `std::ifstream
       load_file'
    /home/account/cpp/mud.cpp:218: warning: where case label appears here
    /home/account/cpp/mud.cpp:218: warning: (enclose actions of previous case
       statements requiring destructors in their own scope.)
    /home/account/cpp/mud.cpp:221: warning: destructor needed for `std::ifstream
       load_file'
    /home/account/cpp/mud.cpp:221: warning: where case label appears here
    /home/account/cpp/mud.cpp:250: warning: destructor needed for `std::ifstream
       load_file'
    /home/account/cpp/mud.cpp:250: warning: where case label appears here
    /home/account/cpp/mud.cpp:255: warning: destructor needed for `std::ifstream
       load_file'
    /home/account/cpp/mud.cpp:255: warning: where case label appears here
    My computer is awesome.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Place the code between the case and the break in a code block (i.e. by using braces).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    	case 's':{
    	ofstream save_file ("mudsave.txt");
    	save_file<<"Test Test Test";
    	save_file.close();
    	ifstream load_file ("mudsave.txt");
    	load_file>>str;
    	cout<<str<<"\n";
    	cin.get();
    	break;
    	}
    It kind of works now.....I can't believe I forgot the brackets, it is quite late. Instead of outputing everything in the file...like I want it to it just ouputs "Test" once.
    My computer is awesome.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    You shouldn't need those brackets. Your error is caused because the << operator on an ostream, by default, stops on whitespace.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Open the file in your text editor, it probably contains the string "Test Test Test". You only see one "Test" because you are only reading one word into the string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Well ya, I need to know how to read the entire file into the string.

    Edit:Look at my second post. I changed the code.
    My computer is awesome.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    You shouldn't need those brackets.
    My guess is that cerin is declaring variables within a switch selection structure, and in such a case placing the code under a case label in a code block limits the variable scope as expected.

    Your error is caused because the << operator on an ostream, by default, stops on whitespace.
    As far as I know, this isnt true. operator>> used as an input operator stops on whitespace, but operator<< used as an output operator does not.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    You shouldn't need those brackets.
    My guess is that cerin is declaring variables within a switch selection structure, and in such a case placing the code under a case label in a code block limits the variable scope as expected.
    I am afraid that I haven't declared it in the switch statement.
    My computer is awesome.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Well ya, I need to know how to read the entire file into the string.
    In this case, you can use getline().
    Code:
    getline(load_file, str);
    I am afraid that I haven't declared it in the switch statement.
    You have, since you're constructing the i/o streams.
    Last edited by laserlight; 01-27-2006 at 11:00 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    /home/account/cpp/mud.cpp: In function `int main(int, char**)':
    /home/account/cpp/mud.cpp:214: error: invalid conversion from `void*' to `
       char**'
    /home/account/cpp/mud.cpp:214: error: cannot convert `char*' to `size_t*' for
       argument `2' to `__ssize_t getline(char**, size_t*, FILE*)'
    I changed it to getline and now I get this error.
    My computer is awesome.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Post your updated code. It should be the smallest and simplest code that demonstrates this error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    	case 's':{
    	ofstream save_file ("mudsave.txt");
    	save_file<<"Test Test Test";
    	save_file.close();
    	ifstream load_file ("mudsave.txt");
    	getline(load_file, str);
    	cout<<str<<"\n";
    	cin.get();
    	break;
    	}
    My computer is awesome.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    laserlight: you're probably right on that - I was reading and typing fast, and it's late. As for the brackets, I'd suspect you're right, but since we haven't seen that code, it's hard to judge. I know I've NEVER used brackets like that - just seeing it made my eyes twitch.

    cerin: for getline(), it looks like you've got str declared as a char*, not a std::string (just from the error message). If not, let us see a bit more of the code, and we could help more.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I changed a few things, and now it works. Thanks for your help everyone.
    My computer is awesome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

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