• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Question] Coding Help!

Status
Not open for further replies.

Kakarot1212

Resident Programmer
562
Posts
10
Years
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
 
Last edited:

th3shark

Develops in AS3/C++
79
Posts
10
Years
Well I did some Google searching for you and this is what I found:

1. Writing to a text file:
Code:
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:
Code:
ofstream myFile;
myFile.open("fileName.txt", ofstream::out | ofstream::trunc);
myFile.close();

3. Deleting a file:
Code:
#include <stdio.h>

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

Maruno

Lead Dev of Pokémon Essentials
5,285
Posts
16
Years
...Not really about a game, is it? Nor did you mention which language your little project was in, which is unhelpful.

Thread closed.
 
Status
Not open for further replies.
Back
Top