Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sorry for answering this very late, but I want to let know how the problem was solved. One of the answers was right, the problem was to write the size of the struct. </p> <p>For the writing process: </p> <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(regi, sizeof *regi, 1, filea); free(regi); fclose(filea); } </code></pre> <p>And then, for the reading process:</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(regi, sizeof *regi, 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> <p>I have nothing more to say but thanks =)</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.
    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