Thread: FStream Question

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    15

    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. #2
    Registered User
    Join Date
    May 2003
    Posts
    148

    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. #3
    Registered User
    Join Date
    May 2003
    Posts
    15
    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?
    Last edited by CPP-Null; 05-25-2003 at 12:44 PM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    15
    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();
    }
    Last edited by CPP-Null; 05-25-2003 at 01:00 PM.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    148
    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. #6
    Registered User
    Join Date
    May 2003
    Posts
    15
    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();
    }
    Preservation lies in thought, if all is forgotten who is to preserve?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. fstream question!
    By FutureCoder in forum C++ Programming
    Replies: 1
    Last Post: 01-31-2003, 04:06 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM