fstream Tutorial

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: fstream Tutorial

 
0
  #31
Jan 5th, 2005
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?
Open file, read a line, if this line contains "The" substring - that's a record you want, get to the next line, if again... until end of file. Simple, right?
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 4
Reputation: johnnyrico is an unknown quantity at this point 
Solved Threads: 0
johnnyrico johnnyrico is offline Offline
Newbie Poster

Re: fstream Tutorial

 
0
  #32
Jan 6th, 2005
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.

  1. void write ()
  2. {
  3. fstream fin;
  4. fin.open("files/titles.txt",ios::out|ios::app);
  5. char* data;
  6. cout<<"Enter the data you want to insert inside the file\n";
  7. cin>>data;
  8.  
  9. fin.write(data,int);
  10.  
  11. fin.close();
  12.  
  13. }
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: fstream Tutorial

 
0
  #33
Jan 7th, 2005
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.

  1. void write ()
  2. {
  3. fstream fin;
  4. fin.open("files/titles.txt",ios::out|ios::app);
  5. char* data;
  6. cout<<"Enter the data you want to insert inside the file\n";
  7. cin>>data;
  8.  
  9. fin.write(data,int);
  10.  
  11. fin.close();
  12.  
  13. }
There are a few problems with your code.The char *data, is just a pointer.You need to allocate some memory to it before you call cin or it will cause the program to crash.

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);

  1. void write ()
  2. {
  3. fstream fin;
  4. fin.open("files/titles.txt",ios::out|ios::app);
  5. char data[100];
  6. cout<<"Enter the data you want to insert inside the file\n";
  7. cin>>data;
  8.  
  9. fin.write(data,100); //better instead of 100 you can use use strlen(data)
  10.  
  11. fin.close();
  12.  
  13. }
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 4
Reputation: johnnyrico is an unknown quantity at this point 
Solved Threads: 0
johnnyrico johnnyrico is offline Offline
Newbie Poster

Re: fstream Tutorial

 
0
  #34
Jan 7th, 2005
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.
  1. ofstream file;
  2.  
  3. file.open("gamepc.txt"); //open a file
  4. cout<<"please insert the name of the game: "<<endl;
  5. cin>>name;
  6. file<<"name of the game: "<<name<<endl; //write to it
  7. cout<<"please insert the game publisher: "<<endl;
  8. cin>>publisher;
  9. file<<"publisher: "<<publisher<<endl;
  10. cout<<"please insert the game developer: "<<endl;
  11. cin>>developer;
  12. file<<"developer: "<<developer<<endl;
  13. cout<<"please insert the game genre: "<<endl;
  14. cin>>genre;
  15. file<<"genre: "<<genre<<endl;
  16. cout<<"please insert the game release date: "<<endl;
  17. cin>>date;
  18. file.close();
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4
Reputation: gfunkllg is an unknown quantity at this point 
Solved Threads: 0
gfunkllg gfunkllg is offline Offline
Newbie Poster

Re: fstream Tutorial

 
0
  #35
Jan 7th, 2005
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: fstream Tutorial

 
0
  #36
Jan 13th, 2005
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.
  1. ofstream file;
  2.  
  3. file.open("gamepc.txt"); //open a file
  4. cout<<"please insert the name of the game: "<<endl;
  5. cin>>name;
  6. file<<"name of the game: "<<name<<endl; //write to it
  7. cout<<"please insert the game publisher: "<<endl;
  8. cin>>publisher;
  9. file<<"publisher: "<<publisher<<endl;
  10. cout<<"please insert the game developer: "<<endl;
  11. cin>>developer;
  12. file<<"developer: "<<developer<<endl;
  13. cout<<"please insert the game genre: "<<endl;
  14. cin>>genre;
  15. file<<"genre: "<<genre<<endl;
  16. cout<<"please insert the game release date: "<<endl;
  17. cin>>date;
  18. file.close();
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
Reading a whole line is quite simple.Just use:

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?
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 4
Reputation: johnnyrico is an unknown quantity at this point 
Solved Threads: 0
johnnyrico johnnyrico is offline Offline
Newbie Poster

Re: fstream Tutorial

 
0
  #37
Jan 20th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 23
Reputation: iamboredguy is an unknown quantity at this point 
Solved Threads: 0
iamboredguy's Avatar
iamboredguy iamboredguy is offline Offline
Newbie Poster

Re: fstream Tutorial

 
0
  #38
Jan 22nd, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: fstream Tutorial

 
0
  #39
Jan 27th, 2005
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
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 13
Reputation: rghai6 is an unknown quantity at this point 
Solved Threads: 0
rghai6 rghai6 is offline Offline
Newbie Poster

Re: fstream Tutorial

 
0
  #40
May 29th, 2005
this is a revised version of the example program ..
it runs properly ...but still got a few bugs to be fixed.

code:
  1. #include <iostream.h>
  2. #include <fstream.h>
  3.  
  4. struct contact
  5. {
  6. char name[10];
  7. int age;
  8. };
  9.  
  10. struct address
  11. {
  12. char city[10];
  13. char country[10];
  14. };
  15.  
  16. class DtbRec
  17. {
  18. private:
  19. contact cnt;
  20. address adr;
  21.  
  22. public:
  23. void getdata();
  24. void dispdata();
  25. };
  26.  
  27. /*
  28.   I will give you a bit of work, make the funtions getdata() and dispdata()
  29.   It's easy and not worth for me to bother with now :-}
  30. */
  31.  
  32. void DtbRec::getdata() //get user input
  33. {
  34. cout <<"Enter info(Name,age,city,country)"<<endl;
  35. cin>>cnt.name;
  36. cin>>cnt.age;
  37. cin>>adr.city;
  38. cin>>adr.country;
  39.  
  40. }
  41.  
  42. void DtbRec::dispdata() //display to screen
  43. {
  44. cout<<cnt.name<<endl;
  45. cout<<cnt.age<<endl;
  46. cout<<adr.city<<endl;
  47. cout<<adr.country<<endl;
  48. }
  49.  
  50. //This program is not tested, have fun fixing errors, if any
  51. //I am a taos programmer so dont expect anything major
  52. //Typing mistakes are not errors
  53. //This was done off the cuff and not even compiled once
  54.  
  55. int main()
  56. {
  57. DtbRec xRec; //temp rec
  58. fstream fl_h; //file handle
  59. char ch;
  60. int num;
  61.  
  62. fl_h.open("database.txt",ios::in|ios::out|ios::binary|ios::trunc);
  63.  
  64. do
  65. {
  66. cout<<"\n\nFstream Dtb\n"
  67. <<"\n1.Add records"
  68. <<"\n2.View records"
  69. <<"\n3.Modify records"
  70. <<"\n4.Exit"
  71.  
  72. <<"\n\tEnter Choice:";
  73.  
  74. cin>>ch;
  75.  
  76. if(ch == '1') //we are dealing with chars not ints
  77. {
  78. //Adding a rec
  79.  
  80. fl_h.seekp(0,ios::end); //will disscuss this later,this sets the file write pointer to
  81. //the end of a file.
  82. xRec.getdata(); //Get some data from the user
  83.  
  84. fl_h.write((char*)&xRec,sizeof(DtbRec));
  85.  
  86. fl_h.seekg(0,ios::end);
  87. num=fl_h.tellg();
  88. int rec;
  89. rec=num/(sizeof(DtbRec));
  90. cout<<"No of recs="<<rec;
  91.  
  92. }
  93.  
  94. else if(ch == '2')
  95. {
  96. //View recs
  97.  
  98. fl_h.seekg(0,ios::beg); //will disscuss this later,this sets the file read pointer to the
  99. //begining of a file.
  100. int n = 0;
  101. fl_h.seekg(0,ios::beg);
  102. while(!fl_h.eof()) //will disscuss this later,it check if the file's end has been reached
  103. {
  104. n++;
  105. fl_h.read((char*)&xRec,sizeof(DtbRec));
  106.  
  107. cout<<"\nRecord No["<<n<<"]\n";
  108. xRec.dispdata(); //Show the user all the data present
  109. }
  110. }
  111.  
  112. else if(ch == '3')
  113. {
  114. //Modify me colors ;-)
  115.  
  116. cout<<"Enter the record no(starts at 0):";
  117. cin>>num;
  118.  
  119. fl_h.seekg(num*sizeof(DtbRec),ios::beg); //move the read pointer to where the rec is
  120. fl_h.read((char*)&xRec,sizeof(DtbRec)); //read it
  121. xRec.dispdata(); //Show the info
  122. xRec.getdata(); //Let the user change the info
  123.  
  124. fl_h.seekp(num*sizeof(DtbRec),ios::beg); //move the write ponter this time
  125. fl_h.write((char*)&xRec,sizeof(DtbRec)); //overwrite with new info
  126.  
  127. //yahoo,modification done.I have seen too many people who just
  128. //cant get modification .It's so simiple I just cant get why they just cant
  129. //get it ;-)
  130. }
  131.  
  132. }while(ch != '4');
  133.  
  134. fl_h.close(); //close the file
  135. cout<<"\nEnd of Program";
  136. return 0;
  137. }
Last edited by Ancient Dragon; May 1st, 2008 at 11:10 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC