Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a malloc'd C struct to a file, and reading from it
    primarykey
    data
    text
    <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; struct stCrimeArchive { char id_student[10]; int id_document; char judgement[30]; int id_crime; char date[12]; char id_police[12]; }; int main() { struct stCrimeArchive *regi; FILE *filea; filea = fopen("crimearchives.dat", "r+b"); if(!filea) filea = fopen("crimearchives.dat", "w+b"); int i; char num[6]; regi = (struct stCrimeArchive*)malloc (sizeof(struct stCrimeArchive)); printf("ID DOCUMENT: "); fgets(num, 6, stdin); regi-&gt;id_document= atoi(num); printf("ID STUDENT: "); fgets(regi-&gt;id_student, 30, stdin); for(i = strlen(regi-&gt;id_student)-1; i &amp;&amp; regi-&gt;id_student[i] &lt; ' '; i--) regi-&gt;id_student[i] = 0; printf("CRIME CODE: "); fgets(num, 6, stdin); regi-&gt;id_crime = atoi(num); printf("DATE OF THE CRIME: "); fgets(regi-&gt;date, 30, stdin); for(i = strlen(regi-&gt;date)-1; i &amp;&amp; regi-&gt;date[i] &lt; ' '; i--) regi-&gt;date[i] = 0; printf("ID POLICE: "); fgets(regi-&gt;id_police, 30, stdin); for(i = strlen(regi-&gt;id_police)-1; i &amp;&amp; regi-&gt;id_police[i] &lt; ' '; i--) regi-&gt;id_police[i] = 0; printf("JUDGEMENT: "); fgets(regi-&gt;judgement, 30, stdin); for(i = strlen(regi-&gt;judgement)-1; i &amp;&amp; regi-&gt;judgement[i] &lt; ' '; i--) regi-&gt;judgement[i] = 0; fseek(filea, 0, SEEK_END); fwrite(&amp;regi, sizeof(struct stCrimeArchive*), 1, filea); free(regi); fclose(filea); } </code></pre> <p>The context is the following: I am making a variable length file, and on it i'm writing many crime archive with the struct shown above. To do it i need to do a malloc for the struct involved. The problem is when I write it to file, it doesn't write anything, and I don't know what mistake(s) I'm doing. </p> <p>Also I want to read the crime archives from the same file, but I also don't know what I am doing wrong. Here's the code for what I am trying to do:</p> <pre><code>int main() { struct stCrimeArchive *regi; regi = (struct stCrimeArchive*)malloc (sizeof(struct stCrimeArchive)); FILE *filea; filea = fopen("crimearchives.dat", "r+b"); if(!filea) filea = fopen("crimearchives.dat", "w+b"); rewind(filea); while (fread(&amp;regi, sizeof(struct stCrimeArchive), 1, filea)) printf("%2d %-11s %-11s %8d %-13s %-14s\n", regi-&gt;id_document, regi-&gt;id_police, regi-&gt;date, regi-&gt;id_crime, regi-&gt;id_student, regi-&gt;judgement); fclose(filea); } </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