Results 1 to 2 of 2

Thread: What am i doing wrong?

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Question

    Hello i'm pratically a newbie to C++ i have only been programming in VB, but my problem is this: i am reading a book called "Thinking in C++" and they tell me this:

    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main() {
        ifstream in("CoryS.cpp"); //opening file for reading
        ofstream out("CoryS2.cpp"); //opening file for writing
        string s;
        while(getline(in, s)) //discards newline char
            out << s << "/n"; // ... must add it back
            }
    And it is suppose to copy the contents of one file into the other, and it does nothing i made the first file CoryS.cpp say "hello this is just a test" and the CoryS2.cpp a blank file with nothing in it....and it didn't work, anyoen know why/>
    if so please post!

    Thanks for listening!


  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    It works fine for me. Check if the files are in the same directory as the exe or else include the full path name.
    Also change /n with \n
    [code]
    //this works
    #include <string>
    #include <fstream>
    #include <iostream>
    using namespace std;

    int main() {
    ifstream in("CoryS.cpp"); //opening file for reading
    ofstream out("CoryS2.cpp"); //opening file for writing
    string s;
    while(getline(in, s)) //discards newline char
    out << s << "\n"; // ... must add it back
    return 1;
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width