Note that there are some explanatory texts on larger screens.

plurals
  1. POEverytime I save to a file, data within the structure is overwritten/edited - C programming
    primarykey
    data
    text
    <p>So when I use this 'save' function, it seems to change the data within my structure, to random numbers and icons. If i don't save the file then the integrity of the data within the structure keeps true to the original input.</p> <p>I'm not sure where the error could be or how to even start to fixing this, thanks for any help.</p> <p>here is my structure;</p> <pre><code>struct packet{ // declare structure for packet creation int source; int destination; int type; int port; char data[51]; }; </code></pre> <p>here is the function;</p> <pre><code>//Save the records to a file: follows the same principle as list but uses a file handle (pointer to a file) //and fprintf to write to the file void save(int rCount, struct packet *records){ FILE *recordFile; //file handle char fileName[30] = { '\0'}; //string to store the file name int i; puts("Enter a filename to save the records :"); //ask the user for the filename scanf("%s", fileName); //store the filename: data input should be checked //here in your program //try and open the file for writing and react accordingly if there is a problem if((recordFile = fopen(fileName,"w"))==NULL){ printf("Couldn't open the file: %s\n",fileName); exit(1); } else{ //the file opened so print the records array of packet to it for(i=0;i&lt;rCount;i++){ fprintf(recordFile,"%04d:%04d:%04d:%04d:%s\n",records[i].source, records[i].destination, records[i].type, records[i].port, records[i].data); } fclose(recordFile); //close the file } } </code></pre>
    singulars
    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.
 

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