Note that there are some explanatory texts on larger screens.

plurals
  1. POReading a File into an array of Structures in C
    primarykey
    data
    text
    <p>still really new to C but starting to get the hang of it....</p> <p>My program is supposed to create/write a file and store information from an array of structures. That part is fine. What im having trouble with is reading from that file back into an empty array of structures....</p> <p>here's my structs:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; #define MAX 100 struct Video { char name[1024]; //name int ranking; // Number of viewer hits char url[1024]; // YouTube URL }; struct Video Collection[MAX]; </code></pre> <p>here's my load method which reads from my file back into my array of structures:</p> <pre><code>void load() { FILE *fileName; fileName = fopen("ranking.dbm", "rb"); if (fileName != NULL){ fread (Collection,1,1,fileName); } else { printf("ERROR"); } } </code></pre> <p>also here is my write method:</p> <pre><code>void save() { FILE * pFile; pFile = fopen ( "Ranking.dbm" , "wb" ); fwrite (Collection, 1 , sizeof(Collection), pFile ); fclose (pFile); } </code></pre> <p>however when i print out my array <code>collection</code> after loading.... its empty... even though i can see my file in the project folder and open it and verify that the data is in there....</p> <p>am i correct in thinking that i dont need a buffer since i don't need to do any processing on it before using it? </p> <p>also since i've already statically allocated space for memory.... am i correct in thinking that i can just read directly into the array? </p> <p>here is my print code:</p> <pre><code>void printall() { int i; printf("\nCollections: \n"); for(i = 0; i &lt; tail; i++) { printf("\nVideo Name: %s", Collection[i].name); printf("\nRanking (Hits): %d", Collection[i].ranking); printf("\nURL: %s", Collection[i].url); printf("\n"); } } </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.
    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