开发者

Delete/Modify an element in a struct array

开发者 https://www.devze.com 2023-03-14 08:53 出处:网络
I have a program that stores a \"friends\" info into a struct array and writes it to a file. No problem there. But how would I be able to modify and/or delete a specific element in that struct array?

I have a program that stores a "friends" info into a struct array and writes it to a file. No problem there. But how would I be able to modify and/or delete a specific element in that struct array? Did some reading and it says I can't, unless I was to shift it all over by one after deleting in.

So I'm assuming I need to read it in, then remove it, and shift all other elements over by one and write it again...but how would I do this? Tried to include only the necessary code I have so far.

For modifying it, I would guess I'd read it in, then ask for the specific element # I want to change, then set all the values in that element to null, then allow the user to input new info? How would this code look?

struct FriendList
{
    char screenname[32];
    char country[32];
    char city[32];
    char interests[32];
    short age;

};

int main()
{
FriendList friends[num_friends];
const int num_friends = 2;

// Gets user input and puts it into struct array and writes to file

case 3:
        {   // Getting i开发者_JAVA技巧nfo and putting in struct elements
                for (index = 0; index < num_friends; index++)
                {
                // Create Friend Records
                cout << "Enter Screename " << endl;
                cin.ignore();
                cin.getline(friends[index].screenname, 32);
                cout << "Country: " << endl;
                cin >> friends[index].country;
                cout << "City: " << endl;
                cin >> friends[index].city;
                cout << "Age: " << endl;
                cin >> friends[index].age;

                }
                counting += index;

                fstream infile;
                infile.open("friends.dat", ios::out | ios::binary |ios::app);
                if(infile.fail())
                { cout << "File not found!\n\t";
                // exit
                }

                    // Writing struct to file
                infile.write((char*)&friends, sizeof(friends));

                infile.close();

            break;
        }

// Delete a friend ???
    case 5:
        {   // Reading in file contents into struct friends
                        // Then????
            fstream outfile;
            outfile.open("friends.dat", ios::in | ios::binary);
            outfile.read((char*)&friends, sizeof(friends));

            break;
        }


Yes, It can modify member of the struct. But you don't clear memory at the first, you will see garages in friends.dat. in upper of main, you have better to add memset().

memset(&friends, 0, sizeof(friends));

And you use ios::app . I guess that friends is full-set datas. Then, you should remove ios::app ?

BTW, in late about C++, most of c++er don't use binary file for like this case. :)


Change is relatively easy - just read the right entry, update it in memory and write back. In order to delete I suggest the following:

  1. Read all the entries after the one u need to delete

  2. Write those entries in the offset of the deleted entries

  3. Truncate the fuile to the new length

This is the trivial approach


It sounds like you want either a std::deque or a std::vector depending on usage.

If you are deleting items infrequently, then use the std::vector instead of a fixed array:

std::vector<FriendList> friends;

to add new friends:

friends.push_back(newFriend);

accessing a friend by index is the same as accessing an array:

friends[index]

to delete an entry in the vector, use erase() (not remove()!):

friends.erase(friends.begin() + index)


You could make a method delete, which pulls the friend after the one you want to delete, moves the info to the current struct, and continues until there are no more friends.

0

精彩评论

暂无评论...
验证码 换一张
取 消