PDA

Click to See Complete Forum and Search --> : What am i doing wrong?


struntz
Nov 12th, 2000, 08:00 PM
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:

#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! :D

Thanks for listening!

Vlatko
Nov 13th, 2000, 07:27 AM
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;
}