Like Tree3Likes
  • 1 Post By grumpy
  • 1 Post By jimblumberg
  • 1 Post By grumpy

File I/O example from book won't work

This is a discussion on File I/O example from book won't work within the C++ Programming forums, part of the General Programming Boards category; Hey guys, the following code snippet is from my textbook (section about file I/O): Code: // datacopy.cpp #include <cstdlib> #include ...

  1. #1
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197

    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 dennis.cpp; 12-28-2011 at 05:11 PM. Reason: Additional remarks + typo correction
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    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.
    dennis.cpp likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    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
    dennis.cpp likes this.

  5. #5
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

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

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    @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
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

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

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