PDA

View Full Version : [Question] Coding Help!


Kakarot1212
March 16th, 2014, 8:50 PM
I think this is the best place to post this.

AND I NEED THIS TO BE ANSWERED QUICK!

So i'm working on a project, simple library system. I just don't know how to do this one thing.

Example, the user will input the following datas: title, book number and author.
My question is, 1) how do I output it to a .txt file 2) how can I edit that txt file afterwards and 3) how can I delete the txt file. I know it is through fstream, but i don't know how to do it.
Give some sample code if possible. Help is appreciated!

Edit: Here's the code of my Library system if you need.
http://pastebin.com/6pm1a5NA

th3shark
March 17th, 2014, 4:42 AM
Well I did some Google searching for you and this is what I found:

1. Writing to a text file:

ofstream myfile;
myfile.open ("fileName.txt");
myfile << title << " " << bookNumber << " " << author << '\n';
myfile.close();


2. Editing a text file is trickier than programs like Notepad would have you believe. I recommend getting all the data from a text file, storing it somewhere, then clear the text file and write to it again. It appears writing with the truncate option will delete the content:

ofstream myFile;
myFile.open("fileName.txt", ofstream::out | ofstream::trunc);
myFile.close();


3. Deleting a file:

#include <stdio.h>

if( remove( "myfile.txt" ) != 0 )
cerr << "Error deleting file" << endl;
else
cout << "File successfully deleted" << endl;

Maruno
March 17th, 2014, 5:34 AM
...Not really about a game, is it? Nor did you mention which language your little project was in, which is unhelpful.

Thread closed.