| | |
assignment help [fstream, on modifying a record in a file]
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>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
Here's the code...
And a sample run...
Hmm?
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
C++ Syntax (Toggle Plain Text)
Movie title:The matrix Movie star 1:Keanu Reeves Movies star 2:Lawrence Fishbourne Movie producer:Joel Silver Movie director:Wachowski Brothers Production Company:Warner Brothers Number of Copies in Stock:25 Total Copies in store:1
Here's the code...
C++ Syntax (Toggle Plain Text)
//pedantic.cpp //A program to demonstrate how to ammend a text file //Released into the public domain on the 02/04/06 //All rights reserved #include <iostream> #include <string> #include <fstream> #include <vector> int main() { std::vector <std::string> bullcrap; //create a vector of strings std::vector <std::string> more_bullcrap; //create a vector of strings bullcrap.push_back("Movie title:"); bullcrap.push_back("Movie star 1:"); bullcrap.push_back("Movie star 2:"); bullcrap.push_back("Movie producer:"); bullcrap.push_back("Movie director:"); bullcrap.push_back("Production company:"); bullcrap.push_back("Number of Copies in Stock:"); bullcrap.push_back("Total Copies in store:"); std::ifstream read("c:/records.txt"); std::string tee_he; while(getline(read,tee_he,'\n'))//read in file line by line { std::cout<<tee_he<<std::endl; more_bullcrap.push_back(tee_he); } read.close(); std::cout<<"\n\n\nWhat do ya wanna change,gimme a number chump:\n"<<std::endl; for(int i=0; i<more_bullcrap.size(); i++) { std::cout<<" "<<i+1<<")"<<more_bullcrap[i]<<std::endl; } int choice; std::cin>>choice; std::cout<<"You have chosen "<<bullcrap[choice-1]; std::cout<<"\nWhat do you wanna change it to:"<<std::endl; std::cin.ignore(); std::string burp; std::getline(std::cin, burp, '\n'); more_bullcrap[choice-1].erase(); //delete entire line chosen by user more_bullcrap[choice-1]=burp; //replace line with new value std::cout<<"\n\n\n"; std::cout<<"Your file has been ammended\nThanQ"; remove("c:/records.txt");//delete file std::ofstream write("c:/records.txt");//write to file for(int i=0; i<more_bullcrap.size(); i++) { if (i==choice-1) { write<<bullcrap[i]<<more_bullcrap[i]<<std::endl; } else { write<<more_bullcrap[i]<<std::endl; } } write.close(); std::cin.get(); return 0; }
And a sample run...
C++ Syntax (Toggle Plain Text)
Movie title:The matrix Movie star 1:Keanu Reeves Movies star 2:Lawrence Fishbourne Movie producer:Joel Silver Movie director:Wachowski Brothers Production Company:Warner Brothers Number of Copies in Stock:25 Total Copies in store:1 What do ya wanna change,gimme a number chump: 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 1 You have chosen Movie title: What do you wanna change it to: The matrix Revolutions Your file has been ammended ThanQ
Hmm?
*Voted best profile in the world*
•
•
•
•
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.
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*
•
•
Join Date: Mar 2006
Posts: 6
Reputation:
Solved Threads: 0
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!
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!
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!
C++ Syntax (Toggle Plain Text)
void modifyVideoList() { videoDataTemp temp; //videodataTEMP to temp (temporary storage) fstream file4; //fstream is file4 videoData fields; //videoData is fields (original) char titleTemp [30]; //temporary storage for search function int flag=0; int num=0; //counter to count how many record it has go thru int counter=0; cout<<"\t\n\nEnter title of video to search: \n"; cin.getline(titleTemp,30); file4.open("videolist.dat", ios::out | ios::in | ios::binary); file4.seekg(0, ios::beg); file4.read( reinterpret_cast<char*>(&fields), sizeof(fields) ); //while not end of file while ( !file4.eof()) { //if string compare original title is equals to temporary //return true if(strcmp(fields.title,titleTemp)==0) { num = num+1; //num plus 1 //cout<<num<<endl; ///////display previos title cout<< "\nDisplaying '" << fields.title << "' Information\n"; cout<< "Movie Title: " << fields.title <<endl; cout<< "Movie Star 1: " << fields.star1 <<endl; cout<< "Movie Star 2: " << fields.star2 <<endl; cout<< "Movie Producer: " << fields.producer <<endl; cout<< "Movie Director: " << fields.director <<endl; cout<< "Production Company: " << fields.productionCo <<endl; cout<< "Number of Available Copies In Stock: " << fields.availableCopies <<endl; cout<< "Total Copies in Store: " << fields.copiesInStock <<endl; cout<< "\n\n"; //////////edit into temporary storage cout<<"Video Title: "; cin.getline(temp.TEMPtitle, 30); cout<<"Movie Star 1: "; cin.getline(temp.TEMPstar1, 30); cout<<"Movie Star 2: "; cin.getline(temp.TEMPstar2, 30); cout<<"Movie Producer: "; cin.getline(temp.TEMPproducer, 30); cout<<"Movie Director: "; cin.getline(temp.TEMPdirector, 30); cout<<"Production Company: "; cin.getline(temp.TEMPproductionCo, 30); cout<<"Number of Available Copies in Stock: "; cin>> temp.TEMPavailableCopies; cout<<"Total Copies in Store: "; cin>> temp.TEMPcopiesInStock; //seek position of the original file and replace using write file4.seekp(num*sizeof(fields),ios::beg); //move the write ponter this time file4.write( reinterpret_cast<char*>(&temp), sizeof(fields)); //<-- Edited Version break; } num = counter++; file4.read( reinterpret_cast<char*>(&fields), sizeof(fields) ); } if(flag==1) { cout<<"Video title not found!"<<endl; fflush(stdin); } cout << endl; file4.close(); system("Pause"); system("Cls"); }
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!
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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....
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."
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
![]() |
Similar Threads
- Saving Entered Data (C++)
Other Threads in the C++ Forum
- Previous Thread: Sound/song in this project
- Next Thread: forms question
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





