Thread: fstream reading

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    fstream reading

    Hello, i want to make a program that saves input as a txt, and then can read it back, and save certain portions as strings. i can already do the input, but how do you read the information? what do setg and setp do?
    Please help me!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why not post a simple sample of your attempt?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    Ok, heres my best shot:
    Code:
    #include <cstring>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
      ifstream teacher;
      open.teacher ("Tinfo.txt");
      string name;
      cout<<"Enter your Name\n";
      cin>>name;
      teacher<<name;
      cin.get();
      
      //at this point i have no clue what to do.
      }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps something along this line.
    Code:
    #include <string>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
       static const char filename[] = "Tinfo.txt";
    
       ofstream out(filename);
       if ( out )
       {
          cout << "Enter your Name\n";
          string  outgoing_text;
          cin  >> outgoing_text;
          out  << outgoing_text;
          out.close();
       }
    
       ifstream in(filename);
       if ( in )
       {
          string  incoming_text;
          in   >> incoming_text;
          cout << "incoming_text = " << incoming_text << '\n';
       }
    }
    
    /* my input/output
    Enter your Name
    Dave
    incoming_text = Dave
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    #include <string>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
       string name;  
       ofstream out ("Tinfo.txt");
       cout << "Enter your Name\n";
       getline( cin, name);
       teacher << name;
       cin.get();
       out.close();
       ifstream in ("Tinfo.txt");
       getline( in, name);
       cout << "Your name is: " << name << endl;
      //in stream is closed explicitly here
       return 1;
    }
    Okay well there is a small example.
    1) ifstream reads FROM the file
    2) ofstream places thing in the file
    3) it is better to use getline with namesjust incase your name has spaces
    4) You will also want to place a file existence check for ifstream it is a bad Idea to read from a file that does not exist

    I hope that that may assist you in some way
    [EDIT]
    ARG someone beat me to post

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> what do setg and setp do?
    I don't think you should be using setg and setp (actually seekp and seekg).

    Your first step would be to attempt to compile the partial program and get it working, then slowly add one piece at a time. The current code is close except for a couple problems. First, it should be <string>, not <cstring>. Second, you have an ifstream called teacher. The ifstream class reads data already in a file. Is that what you want to do? If so, then the code later that outputs name to the teacher stream is wrong. If you really do want to output name to the teacher file stream, then you want an ofstream, not ifstream. Finally, open.teacher probably doesn't compile, it should be teacher.open.

    Once you get those things straightened out, you need to explain to yourself and us more clearly and more detailed what you want to do next in your program. Do you want to read from that file after you wrote to it? Do you want to do something with the name string like search or something? What do you specifically want to do?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    i got it! thank you for your help. by the way, the point is for a user to be able to save settings even after the program turns off
    Last edited by cppdungeon; 09-23-2006 at 01:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Reading from a file using fstream.
    By sbho in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2007, 11:14 AM
  3. I need some help reading in data with fstream
    By Geo-Fry in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2003, 08:25 AM
  4. Does reading file with fstream not work on xp?
    By pinkcheese in forum C++ Programming
    Replies: 13
    Last Post: 04-11-2002, 08:39 AM
  5. Reading Files with fstream
    By pinkcheese in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2002, 11:10 PM