# FStream Question

This is a discussion on FStream Question within the C++ Programming forums, part of the General Programming Boards category; After I create a random text file called text.txt with Code: std::fstream fin, fout; fout.open("c:\text.txt", std:ios::out); //ignore the smily its ...

1. ## FStream Question

After I create a random text file called text.txt with
Code:
std::fstream fin, fout;
fout.open("c:\text.txt", std:ios::out); //ignore the smily its an ':'':'o';
if(!fout.is_open()) return 0;

//I wrote 10 lines of strings to the file with:
while (int i = 0; i <= 10; i++) {
fout << "This is line #" << i++ << std::endl;
fout.close();

//now I open the file to get the string of characters for each line
//and print them to the screen
fin.open("c:\text.txt", std::ios::in);
if(!fin.is_open()) return 0;
std::string str;
while (!fin.eof()) {
std::getline(fin, str, '\n'); //why is it FN? and '\n' for params???
std::cout << str << std::endl; //prints str line.
fin.close(); //close fstream.
system ("PAUSE");
}
My question is, if I create a text file manually - and simply want to get the lines of strings in the file how do I go about this? Simply use fin.open("c:\text.txt", std::ios::in);? and then instantiate a string to retrieve all data untill the end of the file? ::

::EDIT:: - also why do I need to use standard before each operator? i.e. std::cout, std::string str?

2. ## Re: FStream Question

The whole file content in one string?
Code:
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
ifstream file("test.txt");

stringstream stream;
stream << file.rdbuf();

string fileContent = stream.str();
}
Originally posted by CPP-Null i
::EDIT:: - also why do I need to use standard before each operator? i.e. std::cout, std::string str? [/B]
No,try using namespace std; or using std::cout ,etc.

3. Where can I learn more about this topic? - Exspecially on the StringStream class? - also is it possible to divide the contents up in a file and store it at different variables? Like say I declare a = 5 in a text file. I read it in, and do c = a + 1. Will that yeild six?

what does file.rdbuf() do?

4. Im running into a problem - its not printing the string filecontent. =( - I tried cout << filecontent; didn't seem to print.

Code:
using namespace std;
int main(){
ifstream file1("c:\test.txt");
stringstream stream;
stream << file1.rdbuf();
string filecontent = stream.str();
getline(file1, filecontent, '\n');
cout << filecontent;
system("PAUSE");
file1.close();
}

5. Take the getline out.After rdbuf the filepointer sits at end of file.You already had the whole content in the string,dont override it again.

6. I removed getline(), but the text being stored in the string thats read from the buffer isn't being printed to the string. All I get is the System ("PAUSE") message, not the text stored in the text file.

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main(){
ifstream file1("c:\test.txt");
stringstream stream;
stream << file1.rdbuf();
string filecontent = stream.str();
cout << filecontent; //isn't this needed to print the filecontents?
system("PAUSE");
file1.close();
}

Popular pages Recent additions