Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know your binary file format. Your code tell the binary file have names only with 30 characters.</p> <pre><code> struct alu{ int cod; char name[30]; //juan - pedro - leo int sex; }; </code></pre> <p>This structure size is not 38 bytes.</p> <p>Check structure size.</p> <pre><code>printf("%d", sizeof(struct alu)); </code></pre> <p>structure size depend on your compiler option...</p> <p>If your binary format has bellow format.</p> <pre><code>{ // 1st record cod = 10 names[30] = "juan" sex = 1 } { // 2nd record cod = 20 names[30] = "pedro" sex = 1 } { // 3rd record cod = 12 names[30] = "leo" sex = 2 } </code></pre> <p>See your code.</p> <pre><code>#include &lt;stdio.h&gt; #pragma pack(push, 1) or 4 or 8 depend on your binary format. struct alu{ int cod; char name[30]; //juan - pedro - leo int sex; }; #pragma pack(pop) int FSearch(char path[],char X[]) { char Name[30]; struct alu A; FILE *arc; arc=fopen(path,"rb"); //fseek(arc,sizeof(char[30]),SEEK_SET); fseek(arc,0,SEEK_SET); // first record. //while (fread(Name,sizeof(char[30]),1,arc)) { while (fread(A,sizeof(A),1,arc)) { /*Here is when the errors happen.. The next sentence tell me that A.name don't have the name from the second time*/ printf("%s and %s.",X,A.name); if (strcmp(A.name,X)==0) { printf("Information of %s:\n",X); //fshow_str(A); fclose(arc); return 1; // found } } fclose(arc); return 0; // not found } int main(int argc, char **argv) { char path[]="file.bin"; printf("\n%d",FSearch(path,"pedro")); return 0; } </code></pre> <hr> <p>cod size depend on compiler OPTION!!!</p> <pre><code>int FSearch(char path[],char X[]) { char Name[30]; struct alu A; FILE *arc; arc=fopen(path,"rb"); fseek(arc,0,SEEK_SET); // first record. //while (fread(Name,sizeof(char[30]),1,arc)) { while (1) { //if ( -1 == fseek(arc,4,SEEK_CUR)) // Important!!! case structure pack(4) for skip cod or // break; if ( -1 == fseek(arc,8,SEEK_CUR)) // Important!!! case structure pack(8) for skip cod break; if (!fread(Name,sizeof(Name),1,arc)) break; A.name = Name; /*Here is when the errors happen.. The next sentence tell me that A.name don't have the name from the second time*/ printf("%s and %s.",X,A.name); if (strcmp(A.name,X)==0) { printf("Information of %s:\n",X); //fshow_str(A); fclose(arc); return 1; // found } } //if ( -1 == fseek(arc,4,SEEK_CUR)) // Important!!! case structure pack(4) for skip sex or // break; if ( -1 == fseek(arc,8,SEEK_CUR)) // Important!!! case structure pack(8) for skip sex break; fclose(arc); return 0; // not found } </code></pre> <hr> <p>Binary file and structure are alined with 4 bytes.</p> <pre><code>// Attention! pragma pack(push, 4) needs!!! // Your now running machine is 32 bits system. // This source code will invoke addressing fault 64 bits NON Intel processor. #pragma pack(push, 4) // align 4 bytes. struct alu{ int cod; // Integer type. 4 bytes on 32bits system. but 8 bytes on 64 bits system. // I recommend using "long" type. "long" type is 4 bytes on any system. char name[30]; //juan - pedro - leo int sex; // also, Integer type. }; #pragma pack(pop) int FSearch(char path[],char X[]) { char Name[30]; struct alu A; FILE *arc; arc=fopen(path,"rb"); fseek(arc,0,SEEK_SET); // first record. //while (fread(Name,sizeof(char[30]),1,arc)) { while (1) { if ( -1 == fseek(arc,4,SEEK_CUR)) // Skip "cod" 4 bytes. break; if (!fread(Name,sizeof(Name),1,arc)) // Read "name" 30 bytes. break; if ( -1 == fseek(arc,2,SEEK_CUR)) // Skip "name"'s aligned 2 bytes. break; A.name = Name; /*Here is when the errors happen.. The next sentence tell me that A.name don't have the name from the second time*/ printf("%s and %s.",X,A.name); if (strcmp(A.name,X)==0) { printf("Information of %s:\n",X); //fshow_str(A); fclose(arc); return 1; // found } } if ( -1 == fseek(arc,4,SEEK_CUR)) // Skip "sex" 4 bytes. break; fclose(arc); return 0; // not found } </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.
    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