What do you think
does?Code:infile.close();
Printable View
Opening a file with an fstream in your program doesn't open it up on your screen. You're confused about what the term "open" means in this context.
If you want to open a file in another program (for example, open a text file in Notepad), then there are ways of doing that described in the FAQ. But that's not what you do with an fstream.
then you should maybe try to cout the file contents?
If you want to display the contents of the file in the console window, you have to write code to output it to the console window. Opening a file in your program just means that the stream has the file ready for you to read from. You can use any of the many function available for reading from streams.
The one you probably want is getline. Try looking up the getline function and figure out how to read from a stream. Then try outputting the line you read. Once that works, you'll want to use a loop to read each line and output them all to the screen.
There are other methods, but I imagine you are trying to learn how to program and those other methods won't help you much in that regard.
Oh brother. Doesn't anyone actually study anymore? :confused:
Ever heard of a book? In ancient times past we used these to learn things. Incredible, but true.
What book was this then?
Soma
There are a lot of resources out there. My first C programming book was found at a second-hand book store for less than $10. Then there are public libraries and even online tutorials. The point is, if you really want to program you are going to have to work very hard to get good at it, and that means lots of study, research, and practice. Good luck. ;)
Sebastiani I got to ask. the code in your signature. whats it do?
.....
I learned everything I need to know about C++ from tutorials on the web, this message board, and Stroustrup's book.
Think of it this way:
Code:std::ifstream reader; //can read stuff
reader.open("File.file");
if(!reader) { /*error*/ }
//NOW. What shall we do with this file? Let's READ data from the
//beginning, until the first "newline" character.
std::string buffer;
std::getline(reader,buffer,'\n'); // '\n' is default, actually
std::cout << "The first line goes like this:\n" << buffer << std::endl;
//Now the stream is poised at the second line, if only you read it..... etc...
>> but how do I read it from the stream I tried, but it didn't work!
Show your attempt and describe how it doesn't work. If there are compiler errors, post them. If it compiles but doesn't do what you expect, explain what you expect and what it does.