IS THERE ANYONE OUT THERE THAT KNOWS HOW TO WRITE TO A FILE'S LINE, WITHOUT CHANGING OR DELETING ANY OF THE OTHER LINES
FOR EXAMPLE
IF A LINE SAYS
Status: Show
and i want it to change that line to
Status: James
plz help
IS THERE ANYONE OUT THERE THAT KNOWS HOW TO WRITE TO A FILE'S LINE, WITHOUT CHANGING OR DELETING ANY OF THE OTHER LINES
FOR EXAMPLE
IF A LINE SAYS
Status: Show
and i want it to change that line to
Status: James
plz help
Read the whole file to the program. Find the line you want to change and change it. Read the data back to the original file overwriting what was there before. Technically, that involves no changing or deleting of the other lines. There may other possible ways, depending on the exact layout of the file, but that is certainly the most straightforward.
Remember, the file isn't stored in lines. It's stored in (probably consecutive) bytes without blank spaces that we humans like to see to keep things neat. Therefore, selectively increasing the size of any part of the data in the original file and writing it back to where the original data was without moving the part of the file that will come after the change will overwrite something, unless the piece of data you change is the very last piece of data in the file.
Read the whole file to the program. Find the line you want to change and change it. Read the data back to the original file overwriting what was there before. Technically, that involves no changing or deleting of the other lines. There may other possible ways, depending on the exact layout of the file, but that is certainly the most straightforward.
Remember, the file isn't stored in lines. It's stored in (probably consecutive) bytes without blank spaces that we humans like to see to keep things neat. Therefore, selectively increasing the size of any part of the data in the original file and writing it back to where the original data was without moving the part of the file that will come after the change will overwrite something, unless the piece of data you change is the very last piece of data in the file.
heres what i have i re-wrote everything in the file, so whats the best way to reduce it to just the line that says status......i hope you understand.. i appreciate the help though
void statusBooks()
{
char bookName[SIZE], authorName[SIZE], isbnNumber[SIZE],publisherName[SIZE], bookNumber[SIZE];
char bookStatus[SIZE];
listBooks();
ofstream changebookStatus;
changebookStatus.open(bookFilename);
cin.ignore(130,'\n');
cout<< "*****************************************"<<endl;
cout<< "***************Book Status***************"<<endl;
cout<< "*****************************************"<<endl<<endl<<endl;
cout<<"Please Re-Enter Book Info.\n";
cout<<"Book number:\n";
cin.getline (bookNumber,SIZE);
cout<<"Book name:\n";
cin.getline (bookName,SIZE);
cout<<"Author's name:\n";
cin.getline (authorName,SIZE);
cout<<"Book's ISBN#:\n";
cin.getline (isbnNumber,SIZE);
cout<<"Publisher:\n";
cin.getline (publisherName,SIZE);
cout<<"Are you checking the book in or out? (Press 1 for Checking-In & Press 2 for Checking-Out)\n";
cin.getline (bookStatus,SIZE);
if (strcmp(bookStatus,"1")==0)
{
changebookStatus<<bookNumber<<". ";
changebookStatus<<"Title: "<<bookName<<endl; //send to file
changebookStatus<<" Author: "<<authorName<<endl;
changebookStatus<<" ISBN#: "<<isbnNumber<<endl;
changebookStatus<<" Publisher: "<<publisherName<<endl;
changebookStatus<<"Status: Checked In"<<endl;
}
else if (strcmp(bookStatus,"2")==0)
{
changebookStatus<<bookNumber<<". ";
changebookStatus<<"Title: "<<bookName<<endl; //send to file
changebookStatus<<" Author: "<<authorName<<endl;
changebookStatus<<" ISBN#: "<<isbnNumber<<endl;
changebookStatus<<" Publisher: "<<publisherName<<endl;
changebookStatus<<"Status: Checked Out"<<endl;
}
changebookStatus.close();
}
Sorry I don't see any line that starts with status..... Please refer to the line you indicate using a line number from you post.
this:
changebookStatus.open(bookFilename);
opens the file bookFilename in truncate mode, meaning whatever you write to bookFilename will be written at the start of the file and the rest of the file will be "removed" (that is an EOF will be placed at the end of the read to file). The code as written only writes a single books data to file at any one time. Therefore, with every call to statusBooks, datayou will overwrite the single book's data in bookFilename and bookFilename will never have more than the data for the last book in it.
Some place you need to open bookFilename and read everything in it to a container. It would be easiest to do this if you declare a user defined type to hold the data for a single book and create a container of that type, for example create a struct called Book and then declare a vector or a list of type Book called Library or something. Once you have read the file into Library then search Library for the book you want to change status of. Change just the status field to represent whether the book is checked in or out. When all the work with the Library has been completed, then write all the information in Library with all the completed changes back to bookFilename, overwriting everything that was in bookFilename before.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.