I was about to start building a program, but then I realised I didn't know how to read files. In this case I would have the path of the file and want to echo it into console.
What is the comand?
I was about to start building a program, but then I realised I didn't know how to read files. In this case I would have the path of the file and want to echo it into console.
What is the comand?
I've managed to shorten what I need to know. Looking at this from the tutorial, I have most of what I need.
However, I want to output a paragraph, meaning more than one string. Perhaps using for loops?Code:#include <fstream> #include <iostream> using namespace std; int main() { char str[10]; //Creates an instance of ofstream, and opens example.txt ofstream a_file ( "example.txt" ); // Outputs to example.txt through a_file a_file<<"This text will now be inside of example.txt"; // Close the file stream explicitly a_file.close(); //Opens for reading the file ifstream b_file ( "example.txt" ); //Reads one string from the file b_file>> str; //Should output 'this' cout<< str <<"\n"; cin.get(); // wait for a keypress // b_file is closed implicitly here }
Sure, you can do something like.
Code:while (b_file >> str) { cout << str << endl; }
Thank you, I've gotten even closer now.
The only thing left to solve is that every word shows as a new line. How can I make it only start a new line when the file does?Code:#include <fstream> #include <iostream> using namespace std; int main() { char str[10]; //Opens for reading the file ifstream b_file ( "example" ); //Reads strings sequentialy while (b_file >> str) { cout << str << endl; } //Should output all contents cout<< str <<"\n"; cin.get(); // wait for a keypress // b_file is closed implicitly here }
getline - C++ Reference
Thats what you want most likely.
In my case, I do want exact replication.
Edit: Solved.
Last edited by Muscovy; 06-19-2009 at 09:06 PM.