• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • Our weekly protagonist poll is now up! Vote for your favorite Trading Card Game 2 protagonist in the poll by clicking here.
  • 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
    11
    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.
    https://pastebin.com/6pm1a5NA
     
    Last edited:
    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;
     
    ...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