| | |
fstream Tutorial
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
•
•
Originally Posted by johnnyrico
Hey, Thx for the tutorial, but i was wondering how to search for text, not records.
Ex, i want to find all the records that have the word The in them how to do that?
•
•
Join Date: Jan 2005
Posts: 4
Reputation:
Solved Threads: 0
i got a problem with the write to file function, what i want to do is write whatever is in the variable data into the file.
C++ Syntax (Toggle Plain Text)
void write () { fstream fin; fin.open("files/titles.txt",ios::out|ios::app); char* data; cout<<"Enter the data you want to insert inside the file\n"; cin>>data; fin.write(data,int); fin.close(); }
•
•
•
•
Originally Posted by johnnyrico
i got a problem with the write to file function, what i want to do is write whatever is in the variable data into the file.
C++ Syntax (Toggle Plain Text)
void write () { fstream fin; fin.open("files/titles.txt",ios::out|ios::app); char* data; cout<<"Enter the data you want to insert inside the file\n"; cin>>data; fin.write(data,int); fin.close(); }
Better yet just allocate the memory statically (normally) like char data[100].
the write function should be called as file_handle.write(data,size of data);
C++ Syntax (Toggle Plain Text)
void write () { fstream fin; fin.open("files/titles.txt",ios::out|ios::app); char data[100]; cout<<"Enter the data you want to insert inside the file\n"; cin>>data; fin.write(data,100); //better instead of 100 you can use use strlen(data) fin.close(); }
•
•
Join Date: Jan 2005
Posts: 4
Reputation:
Solved Threads: 0
ok tnks for the help in the above section but i got another piece of a code here that i am wondering why it is not reading the whole line.
now my problem is that if the line that i put for the game variable is lets say world of warcraft so it doesn't go to the game varialbe but only the word "world" goes into it, and the words of and warcraft go into publisher and developer, respectivly. so my question is how do i make the whole sentence like "world of warcraft" go into one variable
C++ Syntax (Toggle Plain Text)
ofstream file; file.open("gamepc.txt"); //open a file cout<<"please insert the name of the game: "<<endl; cin>>name; file<<"name of the game: "<<name<<endl; //write to it cout<<"please insert the game publisher: "<<endl; cin>>publisher; file<<"publisher: "<<publisher<<endl; cout<<"please insert the game developer: "<<endl; cin>>developer; file<<"developer: "<<developer<<endl; cout<<"please insert the game genre: "<<endl; cin>>genre; file<<"genre: "<<genre<<endl; cout<<"please insert the game release date: "<<endl; cin>>date; file.close();
•
•
Join Date: Dec 2004
Posts: 4
Reputation:
Solved Threads: 0
did u check
www.christianfinancegroup.com
www.christianfinancegroup.com
•
•
•
•
Originally Posted by johnnyrico
ok tnks for the help in the above section but i got another piece of a code here that i am wondering why it is not reading the whole line.
now my problem is that if the line that i put for the game variable is lets say world of warcraft so it doesn't go to the game varialbe but only the word "world" goes into it, and the words of and warcraft go into publisher and developer, respectivly. so my question is how do i make the whole sentence like "world of warcraft" go into one variableC++ Syntax (Toggle Plain Text)
ofstream file; file.open("gamepc.txt"); //open a file cout<<"please insert the name of the game: "<<endl; cin>>name; file<<"name of the game: "<<name<<endl; //write to it cout<<"please insert the game publisher: "<<endl; cin>>publisher; file<<"publisher: "<<publisher<<endl; cout<<"please insert the game developer: "<<endl; cin>>developer; file<<"developer: "<<developer<<endl; cout<<"please insert the game genre: "<<endl; cin>>genre; file<<"genre: "<<genre<<endl; cout<<"please insert the game release date: "<<endl; cin>>date; file.close();
file.getline(char_buffer,size);
and also:
file.getline(char_buffer,size,terminating_char);
By char buffer I mean any char array
and by teminating character I mean any character and the befault one is '\n' which stands for new line.Helps?
•
•
Join Date: Jan 2005
Posts: 4
Reputation:
Solved Threads: 0
k lets say i got
struct movies{
int year;
char title[256];
char director[256];
char genere[256]
char actors[256];
};
movies entery;
how do i search inside the structure for a specifit title, year actors ect? lets say i type like 20 records inside a file using one strcutre. then i want to search the name lord of the rings as the title in all of those records and then if it finds it it ouputs all the titles with this name and then allows the user to choose one and edit it. how to do it?
struct movies{
int year;
char title[256];
char director[256];
char genere[256]
char actors[256];
};
movies entery;
how do i search inside the structure for a specifit title, year actors ect? lets say i type like 20 records inside a file using one strcutre. then i want to search the name lord of the rings as the title in all of those records and then if it finds it it ouputs all the titles with this name and then allows the user to choose one and edit it. how to do it?
•
•
•
•
Originally Posted by iamboredguy
Hey firenet
You had mentioned a way to delete a record from the file (using sequential access). I tried to do it with random access (i.e seekg and seekp). The program can't delete the last record. What do I do?
Hey post the code, I will be able to better pinpoint the problem
•
•
Join Date: May 2005
Posts: 13
Reputation:
Solved Threads: 0
this is a revised version of the example program ..
it runs properly ...but still got a few bugs to be fixed.
code:
it runs properly ...but still got a few bugs to be fixed.
code:
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> struct contact { char name[10]; int age; }; struct address { char city[10]; char country[10]; }; class DtbRec { private: contact cnt; address adr; public: void getdata(); void dispdata(); }; /* I will give you a bit of work, make the funtions getdata() and dispdata() It's easy and not worth for me to bother with now :-} */ void DtbRec::getdata() //get user input { cout <<"Enter info(Name,age,city,country)"<<endl; cin>>cnt.name; cin>>cnt.age; cin>>adr.city; cin>>adr.country; } void DtbRec::dispdata() //display to screen { cout<<cnt.name<<endl; cout<<cnt.age<<endl; cout<<adr.city<<endl; cout<<adr.country<<endl; } //This program is not tested, have fun fixing errors, if any //I am a taos programmer so dont expect anything major //Typing mistakes are not errors //This was done off the cuff and not even compiled once int main() { DtbRec xRec; //temp rec fstream fl_h; //file handle char ch; int num; fl_h.open("database.txt",ios::in|ios::out|ios::binary|ios::trunc); do { cout<<"\n\nFstream Dtb\n" <<"\n1.Add records" <<"\n2.View records" <<"\n3.Modify records" <<"\n4.Exit" <<"\n\tEnter Choice:"; cin>>ch; if(ch == '1') //we are dealing with chars not ints { //Adding a rec fl_h.seekp(0,ios::end); //will disscuss this later,this sets the file write pointer to //the end of a file. xRec.getdata(); //Get some data from the user fl_h.write((char*)&xRec,sizeof(DtbRec)); fl_h.seekg(0,ios::end); num=fl_h.tellg(); int rec; rec=num/(sizeof(DtbRec)); cout<<"No of recs="<<rec; } else if(ch == '2') { //View recs fl_h.seekg(0,ios::beg); //will disscuss this later,this sets the file read pointer to the //begining of a file. int n = 0; fl_h.seekg(0,ios::beg); while(!fl_h.eof()) //will disscuss this later,it check if the file's end has been reached { n++; fl_h.read((char*)&xRec,sizeof(DtbRec)); cout<<"\nRecord No["<<n<<"]\n"; xRec.dispdata(); //Show the user all the data present } } else if(ch == '3') { //Modify me colors ;-) cout<<"Enter the record no(starts at 0):"; cin>>num; fl_h.seekg(num*sizeof(DtbRec),ios::beg); //move the read pointer to where the rec is fl_h.read((char*)&xRec,sizeof(DtbRec)); //read it xRec.dispdata(); //Show the info xRec.getdata(); //Let the user change the info fl_h.seekp(num*sizeof(DtbRec),ios::beg); //move the write ponter this time fl_h.write((char*)&xRec,sizeof(DtbRec)); //overwrite with new info //yahoo,modification done.I have seen too many people who just //cant get modification .It's so simiple I just cant get why they just cant //get it ;-) } }while(ch != '4'); fl_h.close(); //close the file cout<<"\nEnd of Program"; return 0; }
Last edited by Ancient Dragon; May 1st, 2008 at 11:10 pm. Reason: add code tags
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: C++ Style
- Next Thread: write aprogramme
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





