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!
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!
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.*
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. }
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.*
Okay well there is a small example.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; }
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
>> 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?
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.