-
file i/o help
I know the basic file i/o structur, but I can't work out how to make it display more than one string on the page?
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";
// b_file is closed implicitly here
}
This is the code from cprog and it displays 'this'. But how do you display the whole thing
-
one quick and easy way would be to:
Code:
string ouput = "This text will now be inside of example.txt";
a_file << output;
-
The problem was to read a string, not output a string.
The simplest method to get an entire line is to use std::getline():
Code:
std::string input;
std::ifstream b_file("example.txt");
std::getline(b_file, input); //get a whole line of text from the file
-