Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete/Modify an element in a struct array
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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?</p> <pre><code>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 info and putting in struct elements for (index = 0; index &lt; num_friends; index++) { // Create Friend Records cout &lt;&lt; "Enter Screename " &lt;&lt; endl; cin.ignore(); cin.getline(friends[index].screenname, 32); cout &lt;&lt; "Country: " &lt;&lt; endl; cin &gt;&gt; friends[index].country; cout &lt;&lt; "City: " &lt;&lt; endl; cin &gt;&gt; friends[index].city; cout &lt;&lt; "Age: " &lt;&lt; endl; cin &gt;&gt; friends[index].age; } counting += index; fstream infile; infile.open("friends.dat", ios::out | ios::binary |ios::app); if(infile.fail()) { cout &lt;&lt; "File not found!\n\t"; // exit } // Writing struct to file infile.write((char*)&amp;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*)&amp;friends, sizeof(friends)); break; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload