Note that there are some explanatory texts on larger screens.

plurals
  1. PObuffer output missing first name
    primarykey
    data
    text
    <p>hi I have this question with reading data into array of structures This is what i have so far:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #define MAX_NAME 20 #define FILE_NAME 50 #define LIST_SIZE 50 //void getData(RECORD name[], RECORD score) typedef struct { char *name; int score; }RECORD; int main (void) { // Declarations FILE *fp; char fileName[FILE_NAME]; RECORD list[LIST_SIZE]; char buffer[50]; int count = 0; // Statements printf("Enter the file name: "); gets(fileName); fp = fopen(fileName, "r"); if(fp == NULL) printf("Error cannot open the file!\n"); while (fgets(buffer, LIST_SIZE, fp) != NULL) { printf("%s\n", buffer); list[count].name = (char*) calloc(strlen(buffer), sizeof(char)); sscanf(buffer,"%s%d", list[count].name, &amp;list[count].score); printf("name is: %s and score is:%d \n", list[count].name, list[count].score); count++; } printf("Read in %d data records\n", count); return 0; } </code></pre> <p>so, inside the while loop when i try to print out with out allocating name it seems to work fine, but after i allocate memory for name the buffer is missing the first name when printing out. so i have this output </p> <pre><code>Enter the file name: in.txt Ada Lovelace, 66 Torvalds, 75 Norton, 82 Thompson, 82 Wozniak, 79 Andreessen, 60 Knuth, 60 Goldberg, 71 Hopper, 82 Joy, 91 Tanenbaum, 71 Kernighan, 72 Read in 12 data records </code></pre> <p>instead of the right output which is </p> <pre><code>Enter the file name: in.txt Ada Lovelace, 66 Linus Torvalds, 75 Peter Norton, 82 Ken Thompson, 82 Steve Wozniak, 79 Marc Andreessen, 60 Donald Knuth, 60 Adele Goldberg, 71 Grace Hopper, 82 Bill Joy, 91 Andrew Tanenbaum, 71 Brian Kernighan, 72 Read in 12 data records </code></pre> <p>This is the output after i commented out the fscanf line, so I think the error may occur there.Is there something thing wrong with the memory allocation process, I'm thinking that I'm doing it wrong.</p> <p>I fix the fscanf statement now i get </p> <pre><code>Enter the file name: in.txt Ada Lovelace, 66 name is: Ada and score is:3929744 Linus Torvalds, 75 name is: Linus and score is:3929752 Peter Norton, 82 name is: Peter and score is:3929760 Ken Thompson, 82 name is: Ken and score is:3929768 Steve Wozniak, 79 name is: Steve and score is:3929776 Marc Andreessen, 60 name is: Marc and score is:3929784 Donald Knuth, 60 name is: Donald and score is:3929792 Adele Goldberg, 71 name is: Adele and score is:3929800 Grace Hopper, 82 name is: Grace and score is:3929808 Bill Joy, 91 name is: Bill and score is:3929816 Andrew Tanenbaum, 71 name is: Andrew and score is:3929824 Brian Kernighan, 72 name is: Brian and score is:3929832 Read in 12 data records </code></pre> <p>as you can see the line name is: only print out first name but not last name and the address of integer score.</p>
    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