Note that there are some explanatory texts on larger screens.

plurals
  1. POCode is adding extra characters
    primarykey
    data
    text
    <p>I have a function that is supposed to read through a file, put the individual lines, as separate elements, into an array. Then it's supposed to go through the array and put certain elements at certain positions within the struct.</p> <p>I <em>almost</em> have it... When I go to print the struct to make sure it's getting everything right, extra characters are showing up!</p> <p>This is what is in the file:</p> <pre><code>123 pre 45 cse 67 345 ret 45 cse 56 </code></pre> <p>And this is what it's printing:</p> <pre><code>123 pre 45 cse C 67 345 ret 45 cse 8 56 </code></pre> <p>Here is the code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; struct students //Defining structure for students { int id; //Students ID Number char name[30]; //Students Name int age; //Students Age char dept[4]; //Studets Department int grade; //Students Grade }; int main() { struct students list[20]; FILE *f; char line[30]; char **temp = NULL; int num_righ = 0; int id5; int age5; int grade5; int i, k; f = fopen("records.txt", "r"); while(fgets(line, sizeof (line), f) != NULL) { if (line != NULL) { num_righ++; temp = (char**)realloc(temp, sizeof(char*) *num_righ); temp[num_righ - 1] = strdup(line); } } fclose(f); k = 0; i = 0; while (temp[i] != NULL) { id5 = atoi(temp[i]); list[k].id = id5; i++; strcpy(list[k].name, temp[i]); i++; age5 = atoi(temp[i]); list[k].age = age5; i++; strcpy(list[k].dept, temp[i]); i++; grade5 = atoi(temp[i]); list[k].grade = grade5; i++; k++; } for (i = 0; i &lt; k; i++) { printf("%d\n", list[i].id); printf("%s", list[i].name); printf("%d\n", list[i].age); printf("%s\n", list[i].dept); printf("%d\n", list[i].grade); } } </code></pre>
    singulars
    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.
 

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