Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen I save my inputs to a file from memory, then add another packet. Then list them, there seems to be some sort of bug happen [photo included]
    primarykey
    data
    text
    <p>So I have a 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>When I add some data to this structure it lists fine, if I choose to do so. BUT if i choose to output the entered data within the structure, to a file and then add another packet it starts to mess about with the entries.</p> <p>The first screenshot is how it should always look after information is entered,</p> <p><img src="https://i.stack.imgur.com/MSl7I.png" alt="After I have saved then enter another packet"></p> <p>This screen shot shows what actually happens with data after I have saved and then add another load of information to the structure. <img src="https://i.stack.imgur.com/PLura.png" alt="List before I have saved and entered new packet"></p> <p>This is my add records function</p> <pre><code> // ADD RECORDS FUNCTION struct packet* addRecord(int *rCount, struct packet *records){ int valid = 0; //used to indicated valid input int length = 0; //used to store the string lengths int i = 0; //used in the for loops //char dataTest[51]; //temporary storage of input to be checked before adding to records do{ puts("What is the source of this packet?\nEnter a numerical value between 1 &amp; 1024"); if(scanf("%d", &amp;records[*rCount].source) == 1 // do this loop, if source is greater than 1 and less than 1024 the entry is valid &amp;&amp; records[*rCount].source &gt;= 1 &amp;&amp; records[*rCount].source &lt;= 1024) { //if correct insert the record at the index valid=1; //determined by rCount(the current record count passed to addRecord } else{ valid = 0; //else entry is invalid, get any possible whitespace and put error message getchar(); system("cls"); puts("\nError: Input incorrect\n"); } }while(valid!=1); do{ puts("What is the destination of this packet?\nEnter a numerical value between 1 &amp; 1024"); if(scanf("%d", &amp;records[*rCount].destination) == 1 //If decimal entry is detected and is between values 1 &amp; 1024 copy int into memory &amp;&amp; records[*rCount].destination &gt;= 1 &amp;&amp; records[*rCount].destination &lt;= 1024) { valid = 1; } else { valid = 0; getchar(); system("cls"); puts("Error: Input incorrect\n"); } }while(valid!=1); do{ puts("What is the packet type?\nEnter a numerical value between 0 &amp; 10"); if(scanf("%d", &amp;records[*rCount].type) == 1 //If decimal entry is detected and is ebtween values 0 &amp; 10 copy int into memory &amp;&amp; records[*rCount].type &gt;= 0 &amp;&amp; records[*rCount].type &lt;= 10) { valid = 1; } else { valid = 0; getchar(); system("cls"); puts("Error: Input incorrect\n"); } }while(valid!=1); do{ puts("What is the packet's port?\nEnter a numerical value between 1 &amp; 1024"); if(scanf("%d", &amp;records[*rCount].port) == 1 //If decimal entry is detected and is between values 1 &amp; 1024 copy int into memory &amp;&amp; records[*rCount].port &gt;= 1 &amp;&amp; records[*rCount].port &lt;= 1024) { valid = 1; } else { valid = 0; getchar(); system("cls"); puts("Error: Input incorrect\n"); } }while(valid!=1); do { puts("Enter up to 50 numerical characters"); puts("\n**************Warning!**************\nEntering over 50 numerical characters will\ndelete anything after the first 50 characters"); if (scanf(" %50[0-9]", records[*rCount].data) != 1) { valid = 0; getchar(); system("cls"); puts("Error: Input incorrect\n"); }else { valid = 1; getchar(); *rCount = *rCount+1; } }while(valid != 1); records = realloc(records,(*rCount+1)*sizeof(struct packet)); //reallocate memory to allow a new entry if needed return records; } </code></pre> <p>This is my save to file function</p> <pre><code>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); printf("\n%i Record(s) have been added to the file", rCount); printf("\nPress a key to continue"); getch(); //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 Person's 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