Thread: File I/O example from book won't work

  1. #1
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259

    File I/O example from book won't work

    Hey guys,

    the following code snippet is from my textbook (section about file I/O):

    Code:
    // datacopy.cpp
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
    
        ifstream source;
        string sFilename;
        cout << "Source file: ";
        cin >> sFilename;
    
        source.open(sFilename.c_str(), ios::binary|ios::in);
        if (!source) {
            cerr << sFilename << " cannot be opened.\n";
            exit(-1);
        }
    
        cout << "Target file: ";
        string tFilename;
    
        ofstream target(tFilename.c_str(), ios::binary|ios::out);
        if (!target) {
            cerr << tFilename << " cannot be opened.\n";
            exit(-1);
        }
    
        char ch;
        while (source.get(ch)) {
            target.put(ch);
        }
    
        source.close();
        target.close();
    
    }
    I typed it exactly like it is given in my textbook, but the program wouldn't even let me type in the target's file name.

    I tried it with several different source file names, like test_file.txt, test_file, testfile, etc. Seems after typing in the source file name there is something remaining in the input buffer (the '\n'?), right?

    Is this an error in the code snippet I should e-mail the author? Or am I wrong somewhere and don't see it myself?

    EDIT: Here's an example output, just for clarification:

    [dennis@marx k2]$ ./datacopy
    Source file: datacopytest
    Target file: cannot be opened.
    [dennis@marx k2]$
    Last edited by spaghetticode; 12-28-2011 at 05:11 PM. Reason: Additional remarks + typo correction
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Windows 10 Home

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That code won't compile due to a typo on line 17.

    At no point has your code read a string in for the target file name.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259
    Typo's only here, not in my code (I copy + pasted it from my editor, but I translated German names and screen text lines to English so you guys get a better understanding).
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Windows 10 Home

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    3,714
    You may want to print, return distinct messages for your open failures. Example:
    Code:
    if(!source)
    {
       std::cerr << "Failed to open source file";
       return(2);
    }
    
    if(!target)
    {  
       std::cerr << "Failed to open target";
       return(3);
    }
    Jim

  5. #5
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259
    Quote Originally Posted by jimblumberg View Post
    You may want to print, return distinct messages for your open failures.
    'kay, did that. The error message in output is that of the target file. So I'm right about the input buffer thing?
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Windows 10 Home

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    As I noted in my previous post, the code is NEVER reading in a name for the target file.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    3,714
    Looks okay, as long as the input file exists, if the file does not exist then the open will fail. You need to get the output file name from the user then it should work correctly.

    Jim

  8. #8
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259
    @grumpy - sorry, I missed that part of your answer. Of course you're right. Stupid me (again!). Thanks.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Windows 10 Home

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Example from book - time(NULL) doesn't work
    By FernandoBasso in forum C Programming
    Replies: 6
    Last Post: 10-30-2011, 05:59 PM
  2. A book example I can't get to work...
    By tabl3six in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2011, 02:17 PM
  3. Better tutorials(Book would work too)
    By Mordacai in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2007, 02:16 PM
  4. Newbie trying to work w/ KR book!?!
    By Slimcracker in forum C Programming
    Replies: 3
    Last Post: 05-01-2003, 02:04 PM
  5. program from book wont work
    By cemock in forum C Programming
    Replies: 2
    Last Post: 03-06-2003, 08:58 AM