assignment help [fstream, on modifying a record in a file]

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: assignment help [fstream, on modifying a record in a file]

 
0
  #11
Apr 2nd, 2006
>What im stucked here is to modify a record. I really need some answers on how i can do this.

Erm, well I've done a litte code to help how you might modify a file... but I'm using vectors and std::strings instead, anywhoo...have a look

First save this file in your c: directory

records.txt
  1. Movie title:The matrix
  2. Movie star 1:Keanu Reeves
  3. Movies star 2:Lawrence Fishbourne
  4. Movie producer:Joel Silver
  5. Movie director:Wachowski Brothers
  6. Production Company:Warner Brothers
  7. Number of Copies in Stock:25
  8. Total Copies in store:1

Here's the code...
  1. //pedantic.cpp
  2. //A program to demonstrate how to ammend a text file
  3. //Released into the public domain on the 02/04/06
  4. //All rights reserved
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <fstream>
  9. #include <vector>
  10.  
  11. int main()
  12. {
  13. std::vector <std::string> bullcrap; //create a vector of strings
  14. std::vector <std::string> more_bullcrap; //create a vector of strings
  15.  
  16. bullcrap.push_back("Movie title:");
  17. bullcrap.push_back("Movie star 1:");
  18. bullcrap.push_back("Movie star 2:");
  19. bullcrap.push_back("Movie producer:");
  20. bullcrap.push_back("Movie director:");
  21. bullcrap.push_back("Production company:");
  22. bullcrap.push_back("Number of Copies in Stock:");
  23. bullcrap.push_back("Total Copies in store:");
  24.  
  25. std::ifstream read("c:/records.txt");
  26.  
  27. std::string tee_he;
  28.  
  29. while(getline(read,tee_he,'\n'))//read in file line by line
  30. {
  31. std::cout<<tee_he<<std::endl;
  32. more_bullcrap.push_back(tee_he);
  33. }
  34. read.close();
  35.  
  36. std::cout<<"\n\n\nWhat do ya wanna change,gimme a number chump:\n"<<std::endl;
  37.  
  38. for(int i=0; i<more_bullcrap.size(); i++)
  39. {
  40. std::cout<<" "<<i+1<<")"<<more_bullcrap[i]<<std::endl;
  41. }
  42. int choice;
  43. std::cin>>choice;
  44.  
  45. std::cout<<"You have chosen "<<bullcrap[choice-1];
  46. std::cout<<"\nWhat do you wanna change it to:"<<std::endl;
  47.  
  48. std::cin.ignore();
  49. std::string burp;
  50. std::getline(std::cin, burp, '\n');
  51.  
  52. more_bullcrap[choice-1].erase(); //delete entire line chosen by user
  53. more_bullcrap[choice-1]=burp; //replace line with new value
  54.  
  55. std::cout<<"\n\n\n";
  56. std::cout<<"Your file has been ammended\nThanQ";
  57.  
  58. remove("c:/records.txt");//delete file
  59.  
  60. std::ofstream write("c:/records.txt");//write to file
  61. for(int i=0; i<more_bullcrap.size(); i++)
  62. {
  63. if (i==choice-1)
  64. {
  65. write<<bullcrap[i]<<more_bullcrap[i]<<std::endl;
  66. }
  67. else
  68. {
  69. write<<more_bullcrap[i]<<std::endl;
  70. }
  71.  
  72. }
  73. write.close();
  74.  
  75. std::cin.get();
  76. return 0;
  77.  
  78.  
  79. }

And a sample run...
  1. Movie title:The matrix
  2. Movie star 1:Keanu Reeves
  3. Movies star 2:Lawrence Fishbourne
  4. Movie producer:Joel Silver
  5. Movie director:Wachowski Brothers
  6. Production Company:Warner Brothers
  7. Number of Copies in Stock:25
  8. Total Copies in store:1
  9.  
  10.  
  11.  
  12. What do ya wanna change,gimme a number chump:
  13.  
  14. 1)Movie title:The matrix
  15. 2)Movie star 1:Keanu Reeves
  16. 3)Movies star 2:Lawrence Fishbourne
  17. 4)Movie producer:Joel Silver
  18. 5)Movie director:Wachowski Brothers
  19. 6)Production Company:Warner Brothers
  20. 7)Number of Copies in Stock:25
  21. 8)Total Copies in store:1
  22. 1
  23. You have chosen Movie title:
  24. What do you wanna change it to:
  25. The matrix Revolutions
  26.  
  27.  
  28.  
  29. Your file has been ammended
  30. ThanQ

Hmm?
*Voted best profile in the world*
Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: assignment help [fstream, on modifying a record in a file]

 
0
  #12
Apr 2nd, 2006
Yup, that's the read the file, change the data, overwrite old file technque. I think he wants the change the file contents without overwriting the entire file version, though.
Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: assignment help [fstream, on modifying a record in a file]

 
0
  #13
Apr 3rd, 2006
Originally Posted by Lerner
Yup, that's the read the file, change the data, overwrite old file technque. I think he wants the change the file contents without overwriting the entire file version, though.
I agree that doing it my way is inefficient, especially if the file is tens of thousands of pages long. And Probably the use of std::strings and std::vectors just confused him? I hate using c-style strings

The only other way I can see how to do this without going into the complicated proceedure you outlined in your previous post is to:-

Open the original file
Read in the file line by line holding one line at a time in memory
If that line is to remain unchanged write it to a temporary file
If that line is to be changed ammend that line whilst still in memory and then write it to the temporary file.
Carry on till you have read all of the original file.
Then remove the original file and rename the temporary file with the name of the original file.

(which is basically just the method you had described earlier)

This way, instead of reading the whole file into memory you are only reading one line at a time into memory. ???

If it's any more complicated than that, I think you should consult your teacher. This assignment shouldn't really be any more complicated than this.


Most of that stuff I've done with std::strings can be done using c-style strings. I'll post up a link in a bit if you're intent on using c-style strings.
*Voted best profile in the world*
Quick reply to this message  
Join Date: Mar 2006
Posts: 6
Reputation: weehoong is an unknown quantity at this point 
Solved Threads: 0
weehoong weehoong is offline Offline
Newbie Poster

Re: assignment help [fstream, on modifying a record in a file]

 
0
  #14
Apr 3rd, 2006
Hey there guys!

I've solved my own problem here. After more and more (which i've already done so much... sigh)research, i've finally found out a way to do this. it might not be the more efficient way to go around the problem but it works and im planing to stick around with it unless i've got time to play around wit it.

Special thanks to iamthwee and Lerner for replying to my problems! its great to learn from u guys. Really appreaciates it!

anyway, here is my code out for anyone else who might have got the same problem as i do and are searching the forum for answer. Hope it helps them!
  1.  
  2. void modifyVideoList()
  3. {
  4. videoDataTemp temp; //videodataTEMP to temp (temporary storage)
  5. fstream file4; //fstream is file4
  6. videoData fields; //videoData is fields (original)
  7.  
  8. char titleTemp [30]; //temporary storage for search function
  9. int flag=0;
  10. int num=0; //counter to count how many record it has go thru
  11. int counter=0;
  12.  
  13. cout<<"\t\n\nEnter title of video to search: \n";
  14. cin.getline(titleTemp,30);
  15.  
  16. file4.open("videolist.dat", ios::out | ios::in | ios::binary);
  17. file4.seekg(0, ios::beg);
  18. file4.read( reinterpret_cast<char*>(&fields), sizeof(fields) );
  19.  
  20. //while not end of file
  21. while ( !file4.eof())
  22. {
  23.  
  24. //if string compare original title is equals to temporary
  25. //return true
  26. if(strcmp(fields.title,titleTemp)==0)
  27. {
  28. num = num+1; //num plus 1
  29. //cout<<num<<endl;
  30.  
  31. ///////display previos title
  32.  
  33. cout<< "\nDisplaying '" << fields.title << "' Information\n";
  34. cout<< "Movie Title: " << fields.title <<endl;
  35. cout<< "Movie Star 1: " << fields.star1 <<endl;
  36. cout<< "Movie Star 2: " << fields.star2 <<endl;
  37. cout<< "Movie Producer: " << fields.producer <<endl;
  38. cout<< "Movie Director: " << fields.director <<endl;
  39. cout<< "Production Company: " << fields.productionCo <<endl;
  40. cout<< "Number of Available Copies In Stock: " << fields.availableCopies <<endl;
  41. cout<< "Total Copies in Store: " << fields.copiesInStock <<endl;
  42.  
  43. cout<< "\n\n";
  44.  
  45. //////////edit into temporary storage
  46.  
  47. cout<<"Video Title: ";
  48. cin.getline(temp.TEMPtitle, 30);
  49.  
  50. cout<<"Movie Star 1: ";
  51. cin.getline(temp.TEMPstar1, 30);
  52.  
  53. cout<<"Movie Star 2: ";
  54. cin.getline(temp.TEMPstar2, 30);
  55.  
  56. cout<<"Movie Producer: ";
  57. cin.getline(temp.TEMPproducer, 30);
  58.  
  59. cout<<"Movie Director: ";
  60. cin.getline(temp.TEMPdirector, 30);
  61.  
  62. cout<<"Production Company: ";
  63. cin.getline(temp.TEMPproductionCo, 30);
  64.  
  65. cout<<"Number of Available Copies in Stock: ";
  66. cin>> temp.TEMPavailableCopies;
  67.  
  68. cout<<"Total Copies in Store: ";
  69. cin>> temp.TEMPcopiesInStock;
  70.  
  71. //seek position of the original file and replace using write
  72. file4.seekp(num*sizeof(fields),ios::beg); //move the write ponter this time
  73. file4.write( reinterpret_cast<char*>(&temp), sizeof(fields)); //<-- Edited Version
  74. break;
  75.  
  76. }
  77.  
  78. num = counter++;
  79.  
  80. file4.read( reinterpret_cast<char*>(&fields), sizeof(fields) );
  81.  
  82. }
  83.  
  84. if(flag==1)
  85. {
  86. cout<<"Video title not found!"<<endl;
  87. fflush(stdin);
  88. }
  89.  
  90. cout << endl;
  91. file4.close();
  92. system("Pause");
  93. system("Cls");
  94.  
  95. }

Resources i've gone thru for anyone looking for answers.
1. http://www.cplusplus.happycodings.com/index.html
2. file:///D:/Documents%20and%20Settings/weehoong/My%20Documents/My%20Received%20Files/Tips%20and%20Tricks%20for%20Using%20C%20I-O%20(input-output).htm
3. http://cboard.cprogramming.com/showthread.php?t=68697

Hope these links help anyone having the same problems as i do. Its really a pleasure to have these forums around!
Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: assignment help [fstream, on modifying a record in a file]

 
0
  #15
Apr 3rd, 2006
How about serialization or XML? Would those help?

As Lerner said, the problem is that if you have to make a dramatic change, you have to move the whole file.

You have an option here, though. If you buffer every entry with a lot of empty space, you can use the file pointer to overwrite "safe" space without modifying the rest of the file. You just have to check that you don't overrun the buffer.

Unfortunately, this increases your file size, and could be costly if your records list gets really big....
Explainer of control logic and some basics.
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
Quick reply to this message  
Closed Thread

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