943,413 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 258038
  • C++ RSS
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
Jan 5th, 2005
0

Re: fstream Tutorial

Quote 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?
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Jan 6th, 2005
0

Re: fstream Tutorial

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)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnnyrico is offline Offline
4 posts
since Jan 2005
Jan 7th, 2005
0

Re: fstream Tutorial

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

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jan 7th, 2005
0

Re: fstream Tutorial

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.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnnyrico is offline Offline
4 posts
since Jan 2005
Jan 7th, 2005
0

Re: fstream Tutorial

Reputation Points: 10
Solved Threads: 0
Newbie Poster
gfunkllg is offline Offline
4 posts
since Dec 2004
Jan 13th, 2005
0

Re: fstream Tutorial

Quote 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.
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jan 20th, 2005
0

Re: fstream Tutorial

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnnyrico is offline Offline
4 posts
since Jan 2005
Jan 22nd, 2005
0

Re: fstream Tutorial

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?
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004
Jan 27th, 2005
0

Re: fstream Tutorial

Quote 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
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
May 29th, 2005
0

Re: fstream Tutorial

this is a revised version of the example program ..
it runs properly ...but still got a few bugs to be fixed.

code:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rghai6 is offline Offline
13 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: C++ Style
Next Thread in C++ Forum Timeline: write aprogramme





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC