0

I want to display all the text that is in the fille to the output, I use by using the code below, the code I got up and results posts are just a little out

#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
}

The above code simply displays the words "This" does not come out all into output.yang I want is all text in the file appear in the console ..

Martin
  • 3,396
  • 5
  • 41
  • 67
nasa
  • 13
  • 1
  • 1
  • 8

4 Answers4

1

The overloaded operator>> for char* will only read up to the first whitespace char (it's also extremely risky, if it tries to read a word longer then the buf length you'll end up with undefined behavior).

The following should do what you want in the most simple manner, as long as your compiler supports the rvalue stream overloads (if not you'll have to create a local ostream variable and then use the stream operator):

#include <fstream>
#include <iostream>

int main()
{
  std::ofstream("example.txt") << "This text will now be inside of example.txt";
  std::cout << std::ifstream("example.txt").rdbuf() << '\n';
}
user657267
  • 20,568
  • 5
  • 58
  • 77
0

try something like this

 #include <fstream>
 #include <iostream>

 using namespace std;

 int main(){
  string line;
  ofstream a_file ( "example.txt" );
  ifstream myfile ("filename.txt");
  if (myfile.is_open()) {
   while ( getline (myfile,line) ) {
      a_file << line << '\n';
   }
  myfile.close();
  a_file.close();
  } else 
      cout << "Unable to open file"; 
 }

Hope that helps

Zuko
  • 2,764
  • 30
  • 30
0

This is not the best way to read from a file. You probably need to use getline and read line by line. Note that you are using a buffer of fixed size, and you might cause an overflow. Do not do that.

This is an example that is similar to what you wish to achieve, not the best way to do things.

#include <fstream>
#include <iostream>

using namespace std;

int main() {
  string str;
  ofstream a_file("example.txt");
  a_file << "This text will now be inside of example.txt";
  a_file.close();
  ifstream b_file("example.txt");
  getline(b_file, str);
  b_file.close();
  cout << str << endl;
  return 0;
}
arhuaco
  • 1,718
  • 16
  • 19
0

This is a duplicate question of:

reading a line from ifstream into a string variable

As you know from text input/output with C++, cin only reads up to a newline or a space. If you want to read a whole line, use std::getline(b_file, str)

Community
  • 1
  • 1
0xpentix
  • 732
  • 9
  • 22