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

Closed Thread

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

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

 
0
  #1
Mar 29th, 2006
Hi there guys!

Just want to say these communities are great! i've really benefited alot from the forum and its tutorials. Special thanks to FireNet and his file handling tutorials. It really helped me alot! Not forgetting, thanks alot to all of the members here too. :cheesy:

anyway, ive got this assignent to finish off and are having problems with a small block of codes here.

  1.  
  2. class video
  3. {
  4.  
  5. protected:
  6.  
  7. struct videoData
  8. {
  9. char title[30];
  10. char star1[30];
  11. char star2[30];
  12. char producer[30];
  13. char director[30];
  14. char productionCo[30];
  15. int copiesInStock;
  16. int totalCopiesInStock;
  17. };
  18.  
  19. public:
  20.  
  21.  
  22. //////[Modify Record]/////////////////////////////////////////////////////
  23.  
  24. void modifyVideoList()
  25. {
  26.  
  27. fstream file4;
  28. videoData fields;
  29. char titleTemp [30];
  30.  
  31. int flag=0;
  32.  
  33. cout<<"\t\n\nEnter title of video to search: \n";
  34. cin.getline(titleTemp,30);
  35.  
  36. file4.open("videolist.dat", ios::app | ios::out | ios::in | ios::binary);
  37. file4.seekg(0, ios::beg);
  38. file4.read( reinterpret_cast<char*>(&fields), sizeof(fields) );
  39.  
  40. while ( !file4.eof())
  41. {
  42.  
  43.  
  44. if(strcmp(fields.title,titleTemp)==0)
  45. {
  46.  
  47. //Display old record information
  48. cout<< "\n\t::Displaying '" << fields.title << "' information::\n";
  49. cout<< "Movie Title: " << fields.title <<endl;
  50. cout<< "Movie Star 1: " << fields.star1 <<endl;
  51. cout<< "Movie Star 2: " << fields.star2 <<endl;
  52. cout<< "Movie Producer: " << fields.producer <<endl;
  53. cout<< "Movie Director: " << fields.director <<endl;
  54. cout<< "Production Company: " << fields.productionCo <<endl;
  55. cout<< "Number of Copies In Stock: " << fields.copiesInStock <<endl;
  56. cout<< "Total Copies in Store: " << fields.totalCopiesInStock <<endl;
  57.  
  58. cout<< "\n\n";
  59.  
  60. //Enter new record information
  61. cout<< "\n\n"<<endl;
  62. cout<< "\t::Enter new information::\n"<<endl;
  63.  
  64. cout<<"Video Title: ";
  65. cin.getline(fields.title, 30);
  66.  
  67. cout<<"Movie Star 1: ";
  68. cin.getline(fields.star1, 30);
  69.  
  70. cout<<"Movie Star 2: ";
  71. cin.getline(fields.star2, 30);
  72.  
  73. cout<<"Movie Producer: ";
  74. cin.getline(fields.producer, 30);
  75.  
  76. cout<<"Movie Director: ";
  77. cin.getline(fields.director, 30);
  78.  
  79. cout<<"Production Company: ";
  80. cin.getline(fields.productionCo, 30);
  81.  
  82. cout<<"Number of Copies in Stock: : ";
  83. cin>> fields.copiesInStock;
  84.  
  85. cout<<"Total Copies in Store: ";
  86. cin>> fields.totalCopiesInStock;
  87.  
  88. file4.seekp(0, ios::beg);
  89. file4.write( reinterpret_cast<char*>(&fields), sizeof(fields) );
  90.  
  91.  
  92. }
  93. file4.read( reinterpret_cast<char*>(&fields), sizeof(fields) );
  94.  
  95. }
  96.  
  97. if(flag==1)
  98. {
  99. cout<<"Video title not found!"<<endl;
  100. fflush(stdin);
  101. }
  102.  
  103. cout<< "\nRecord has been updated!\n" <<endl;
  104.  
  105. file4.close();
  106. system("Pause");
  107. system("Cls");
  108.  
  109.  
  110. }

what im trying to do here is to create a file handling database with add, display records and modify records for a video rental store. the problem here is the modify record section where i cant get any datas into my file.

can anyone help me with this? this block of codes are copied from my header file so if anyone needs the whole thing please tell me so. thanks alot!
Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #2
Mar 29th, 2006
Originally Posted by weehoong
Hi there guys!

Just want to say these communities are great! i've really benefited alot from the forum and its tutorials. Special thanks to FireNet and his file handling tutorials. It really helped me alot! Not forgetting, thanks alot to all of the members here too. :cheesy:

anyway, ive got this assignent to finish off and are having problems with a small block of codes here.

  1.  
  2. class video
  3. {
  4.  
  5. protected:
  6.  
  7. struct videoData
  8. {
  9. char title[30];
  10. char star1[30];
  11. char star2[30];
  12. char producer[30];
  13. char director[30];
  14. char productionCo[30];
  15. int copiesInStock;
  16. int totalCopiesInStock;
  17. };
  18.  
  19. public:
  20.  
  21.  
  22. //////[Modify Record]/////////////////////////////////////////////////////
  23.  
  24. void modifyVideoList()
  25. {
  26.  
  27. fstream file4;
  28. videoData fields;
  29. char titleTemp [30];
  30.  
  31. int flag=0;
  32.  
  33. cout<<"\t\n\nEnter title of video to search: \n";
  34. cin.getline(titleTemp,30);
  35.  
  36. file4.open("videolist.dat", ios::app | ios::out | ios::in | ios::binary);
  37. file4.seekg(0, ios::beg);
  38. file4.read( reinterpret_cast<char*>(&fields), sizeof(fields) );
  39.  
  40. while ( !file4.eof())
  41. {
  42.  
  43.  
  44. if(strcmp(fields.title,titleTemp)==0)
  45. {
  46.  
  47. //Display old record information
  48. cout<< "\n\t::Displaying '" << fields.title << "' information::\n";
  49. cout<< "Movie Title: " << fields.title <<endl;
  50. cout<< "Movie Star 1: " << fields.star1 <<endl;
  51. cout<< "Movie Star 2: " << fields.star2 <<endl;
  52. cout<< "Movie Producer: " << fields.producer <<endl;
  53. cout<< "Movie Director: " << fields.director <<endl;
  54. cout<< "Production Company: " << fields.productionCo <<endl;
  55. cout<< "Number of Copies In Stock: " << fields.copiesInStock <<endl;
  56. cout<< "Total Copies in Store: " << fields.totalCopiesInStock <<endl;
  57.  
  58. cout<< "\n\n";
  59.  
  60. //Enter new record information
  61. cout<< "\n\n"<<endl;
  62. cout<< "\t::Enter new information::\n"<<endl;
  63.  
  64. cout<<"Video Title: ";
  65. cin.getline(fields.title, 30);
  66.  
  67. cout<<"Movie Star 1: ";
  68. cin.getline(fields.star1, 30);
  69.  
  70. cout<<"Movie Star 2: ";
  71. cin.getline(fields.star2, 30);
  72.  
  73. cout<<"Movie Producer: ";
  74. cin.getline(fields.producer, 30);
  75.  
  76. cout<<"Movie Director: ";
  77. cin.getline(fields.director, 30);
  78.  
  79. cout<<"Production Company: ";
  80. cin.getline(fields.productionCo, 30);
  81.  
  82. cout<<"Number of Copies in Stock: : ";
  83. cin>> fields.copiesInStock;
  84.  
  85. cout<<"Total Copies in Store: ";
  86. cin>> fields.totalCopiesInStock;
  87.  
  88. file4.seekp(0, ios::beg);
  89. file4.write( reinterpret_cast<char*>(&fields), sizeof(fields) );
  90.  
  91.  
  92. }
  93. file4.read( reinterpret_cast<char*>(&fields), sizeof(fields) );
  94.  
  95. }
  96.  
  97. if(flag==1)
  98. {
  99. cout<<"Video title not found!"<<endl;
  100. fflush(stdin);
  101. }
  102.  
  103. cout<< "\nRecord has been updated!\n" <<endl;
  104.  
  105. file4.close();
  106. system("Pause");
  107. system("Cls");
  108.  
  109.  
  110. }

what im trying to do here is to create a file handling database with add, display records and modify records for a video rental store. the problem here is the modify record section where i cant get any datas into my file.

can anyone help me with this? this block of codes are copied from my header file so if anyone needs the whole thing please tell me so. thanks alot!
I've not looked at your code properly, I'm only responding to the question about modifying lines in a text file.

This happens to be problematic, appending a text file is not.

It would require you to read the original file into memory, ammend the line/lines whilst still in memory, than write it over the original file. Me thinks
*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
  #3
Mar 29th, 2006
yah, i was thinking of that too. mabe what i can do is create another set of temporary variables as a temporary memory location, edit the temp location, then store the temp location overwriting the original location.

but im not so sure on which direction should i be looking at.

maybe a pseudocode from someone will do?
Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
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: 261
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #4
Mar 29th, 2006
Here's a version using two files rather than holding the file contents in memory.

Mirror the original file contents to a temporary file.
Find the spot you want to change.
Write the correction to the temporary file.
write the rest of the original file to the temporary file.
rename the temporary file the same as the original.
delete the original file.

IF the correction has the same number of bytes as the original---for example you wanted to change the word cold to the word bold, then you could do something different. But if the correction isn't the exact same number of bytes, you need to do it the other way, tedious as it is.
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
  #5
Mar 30th, 2006
Hmmm. This could be one way if im just doing a modify record.

But are there any other ways i could do this in a much simpler way? what i have here is other functions that, for example needs to add an incremental counter into the "file" that im trying to modify.

Erm.. i dont knwo how to put into words. For example, i have a video data list with records of how many stocks left for a particualr video. whenevr someone checks out (rent a video) it will decrease the stock level and updates the database accordingly so i can konw how many stocks are left.

i believe this uses a modify record technique but to do this for my all other functions is just too tedious. are there any other ways that i could for example,

find a record
read the contents
edit the record content
point the pointer to the record i found prevuosly
and simply rewrite it

Thanks alot you all for the help. Really appreaciates it.
Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
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: 261
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #6
Mar 30th, 2006
If the file is set up predictably then you can modify just the data you need, but it can't change any data beyond the memory of the original. Say you have a file with a series of records. Each record has an int representing the number of copies available and a char array that has exactly 50 characters to it that represents the name of whatever it is you are trying to keep track of. Some of the char in the char array may be fillers without any "useful" meaning---sort of like leading zeros to a number, but for this to work each record has to have the same exact number of characters to the string. Under those circumstances you could use seekp() (or maybe it's seekg(), you'll have to look it up for sure) to locate the exact bits/bytes of memory you want to change without destroying the entire file and rewriting it. To do this you can try something like this:

read in each record one at a time until you find the one you want based on the char array field of the record. Keep track of how many records from the beginning of the file the desired record is. Then call seekp() using the stream you are using to write to the file and pass it two parameters, the first being the start of the file and the second being the product of the number of records from the beginning the file the desired record is and the size of each record as determined by the sizeof() operator. This will place the file pointer used to write to the file at the memory location representing the start of the desired record within the desired file (assuming the file is sequential in file memory). Then determine the number of bytes the desired field is within the desired record from the start of the desired record. If the int value is the first field in each record you are already there. Otherwise call seekp() again to move the file pointer the desired distance within the given record. Once the file pointer is correctly postioned you can use the << operator to overwrite the desired memory you want with the desired value you want without reading the entire file in, changing the desired value, and rewriting the entire file back. However, you have to have some way to know where to go to get to the desired memory in the file to change the information you want to change and then the memory used for the desired changes must match the memory used by the original information in memory, otherwise you are likely to corrupt the file.
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
  #7
Apr 1st, 2006
if i assign lets say

char video[40];

and for example a word "hello world" is in that video[40]. if i want to updat it to "hello big world"

is that suppose to mean i already have a fix memory space?


If its ok can someone please provide me a little bit of sample code on how this update thing suppose to be done. thanks alot!
Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #8
Apr 1st, 2006
Originally Posted by weehoong
if i assign lets say

char video[40];

and for example a word "hello world" is in that video[40]. if i want to updat it to "hello big world"

is that suppose to mean i already have a fix memory space?


If its ok can someone please provide me a little bit of sample code on how this update thing suppose to be done. thanks alot!
Hello, let me introduce you to your newest friend. The std::string. In time you will wonder how you ever got things done without them.

  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6. std::string crap="hello";
  7.  
  8. crap=crap+" big world"; //ammend the string
  9.  
  10.  
  11. std::cout<<crap; //new string;
  12. std::cout<<"\nHey that was easy!";
  13. std::cin.get();
  14. return 0;
  15. }

The example isn't exactly as you have described but is shown for simplicity and understanding.
*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
  #9
Apr 1st, 2006
Yup i noe using strings is a good choice. What im stucked here is to modify a record. I really need some answers on how i can do this. Ive done tons of research about how to do it and really have the concepts on how to do it but i dont konw how to implement it!

i know what im suppose to do it using seekg to seek for the record i want to modify, simply using cin.getline to input a new record into a temporary "char" so when i want to use the fstream.write() funtion, i dont know how to go around with it..

i really just need a sample of how modify code works...
Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
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: 261
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #10
Apr 2nd, 2006
IMO, in this case, using strings may not be the best answer, you might want to go with plain old char array work. I can't say that I've done this so I'm talking theory here, but I confident it should work. In order to use seekx() to change file contents you need to know exactly where to place the file pointer. That works easiest if you count char (each char is one byte) to place the pointer where you want or if you know the exact size of each record written to or contained within the file. Assume for arguments sake that each record consists of an int and a string. We know the int has a standard memory size irregardless of whether the value stored is 4 or 4444, but the string does not. That is, if the string in the record is declared as char s[20] each string could have up to 20 char, but it may have none or 3 or 12 or 18, or maybe even twenty. Each char in the string will have a memory footprint of one byte, but the entire string may or may be 20 bytes. The point is each string could be of different length. In contrast, you can be sure that each char array written to file is exactly 20 char long if your treat it as a char array and not as a string. In order for each record to take up the same memory footprint in the file if you write a string to the file you will need to pad the string field to be 20 char if the string isn't 20 char long. If you use << to write the string to the file, then you will need to determine the length of the string and determine the amount to pad from there. On the other hand the char array can be initialized to some default value and then the characters of the string can be entered into the array as appropriate leaving default values for anything beyond the length of the string, and then you could use a function to loop through all 20 char writing each char one at a time to the file thereby assuring that each record written to file has an int to begin and 20 char after that. Either way, using a string and calculating how much to pad, or using a standard char array with defaults, you could replace "hello world" with "hello big world" within the string file since it doesn't take up more than the memory footprint for the string field, in fact you will still need some padding to get you to the 20 char desired to keep the file intact.
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