0

I'm writing a library system using struct as seen

struct Book
{
    char name[50];
    int ID;
    char author[50];
    double price;
    int copies;
};

and the file is organised as seen.

ID  Name           Author         Price   Copies  
1   HarryPotter    Lol            30      5       
2   EnglishMan    English            30      5       
3   Spiderman    Marvel            30      5       
4   Avengers    Marvel            30      5           

Let's say I want to use the program to update book no. 2 (EnglishMan) and change its name to IronMan, how can I do that using files?

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
MiDo
  • 67
  • 1
  • 6
  • you have to read the file in and parse it. query the item your interested in modify it and save the entire content to file. the implementation is up to you to choose which are you most comfortable with using. there are some libraries you can use to make things easier like STL , boost or QT. – agent_bean Aug 12 '17 at 17:03
  • @rafaelgonzalez I'm quite new to programming so i don't know most of what you say – MiDo Aug 12 '17 at 17:06
  • Consider using a SQLite database. –  Aug 12 '17 at 18:42
  • @manni66 never dealt with it to be honest – MiDo Aug 12 '17 at 19:38

2 Answers2

2

If you use plain text files as data storage you just have to follow this inconvenient workflow:

  1. Read the complete file into your data structures.
  2. Alter the data.
  3. Truncate or remove the file.
  4. Write all the data into the file.

There are ugly hacks to edit parts of the file, but they don't make things better.

For managing tabular data, as in your example, relational databases have been invented a long time ago. Start to learn SQLite, and your life will be much easier in the long run.

Murphy
  • 3,827
  • 4
  • 21
  • 35
0

What you are doing is, essentially, trying to create your own database which is counterproductive at best. But if it's for learning file I/O and string streams, following code can help you understand the concepts although It's probably not the most efficient way of doing things.

In general, as @Murphy said, you need to read a file, copy it to a buffer, adjust the buffer to your liking, truncate the file and write your own buffer to the file.

 int searchbyID(string filename, string ID, string newName);

    int main()
    {
        searchbyID("d:\\test.txt", "2", "silmarillion");
    }

    int searchbyID(string filename, string ID, string newName)
    {
        // open an input file stream
        ifstream inputfile(filename);

        if (!inputfile)
            return -1; // couldn't open the file

        string line,word;
        string buffer;

        // read the file line by line
        while (getline(inputfile, line))
        {
            std::stringstream record(line);

            //read the id from the file and check if it's the asked one
            record >> word;
            if (word == ID)
            {
                // append the ID first
                buffer += ID + "\t";

                //append the new name instead of the old one
                buffer += newName + "\t";

                //pass the old name
                record >> word;

                //copy the rest of the line just as it is
                while (record >> word)
                    buffer += "\t" + word + "\t";
                buffer += "\n";
            }
            else 
            {
                //if not, just pass the line as it is
                buffer += line + "\n";
            }

        }
        // close input file stream
        inputfile.close();

        // open an output file stream
        ofstream outputfile(filename);

        // write new buffer to the file
        outputfile << buffer;

        //close the output file stream
        outputfile.close();

        return 0;
    }