Thursday, July 9, 2009

Need help with io in C++, have read much programmed little?

I am somewhat familiar with input/output in programming, but am having problems using file io in C++. This is my skeleton type program:





#include %26lt;stdio.h%26gt;


#include %26lt;fstream.h%26gt;


int main(int argc, char *argv[])


{ ifstream filein; ofstream fileout; char ch;


filein.open("input.txt", ios::in);


fileout.open("output.txt", ios::out);


for(filein.get(ch); ch!=EOF; filein.get(ch));


{


fileout.put(ch);


}


filein.close();


fileout.close();


return 0;


}





i created a text file input.txt and output.txt, input has "asd" in it and output is empty, i want it to write the asd to output.txt





using EOF it is infinite loop, and using NULL it terminates doing nothing, I have read a lot about c/c++ but this is my first stab at programming in it and it is taking me forever to get all syntax and small stuff takin care of. Please tell me what I am doing wrong. Thank you :)

Need help with io in C++, have read much programmed little?
http://www.cplusplus.com/doc/tutorial/fi...





Please get a better book. The above code is terrible. And outdated.





Just open up two filestreams and transfer one line to another. Use getline to take in entire lines. Store them in strings. As in %26lt;string%26gt; strings.





EDIT: I'll give you the code. Please get a newer book so you understand what I did.





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;





int main()


{


string str;


ifstream infile("input.txt");


ofstream outfile("output.txt");


if (infile.is_open())


{


while (!infile.eof() )


{


getline(infile, str);


outfile %26lt;%26lt; str %26lt;%26lt; endl;


}


infile.close();


outfile.close();


}





else cout %26lt;%26lt; "Unable to open file";





return 0;


}
Reply:use ifstream::in and ifstream::out instead!

pear

No comments:

Post a Comment