I'm trying to read a file in c using fread, but fread didn't show all records, only a few. Note that i write the file using struct and recover the same way. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct data{
int numero;
};
void read(){
int i = 1;
FILE *arquivo = fopen(URL_NEW, "r");
struct data dataTemp;
fseek(arquivo,sizeof(struct data),SEEK_SET);
int leitura;
while (fread(&dataTemp,1,sizeof(struct data),arquivo))
{
printf("\nNumber = ");
printf("%d\n",dataTemp.numero);
}
fclose(arquivo);
}
void write(){
struct data dataTemp;
FILE *arquivo = fopen(URL_NEW, "w");
for(int i=1; i < 2000;i++){
dataTemp.numero = i;
fwrite(&dataTemp,sizeof(struct data),1,arquivo);
}
fclose(arquivo);
}
int main(){
write();
read();
}
output:
Number = 2
Number = 3
Number = 4
Number = 5
Number = 6
Number = 7
Number = 8
Number = 9
Number = 10
Number = 11
Number = 12
Number = 13
Number = 14
Number = 15
Number = 16
Number = 17
Number = 18
Number = 19
Number = 20
Number = 21
Number = 22
Number = 23
Number = 24
Number = 25
If i try to to use fseek, they got lost at 10 record. Anyone know know i can fix it or any workaround for my problem?
EDIT: Seems like using rb and wb fixed the problem, thanks! :D
the following proposed code:
read()
and write()
are prototyped in unistd.h
so not a direct problem with the code as that header file is not included. the reason it did not work (well there are a few reasons)
most notably, the compiler will output several warning and error messages. It is a wonder that it ever ran.
When compiling, always enable all the warnings, then fix those warnings.
(for gcc
, at a minimum use: -wall -wextra -pedantic -std=gnu11
I also use: `-Wconversion -Wmissing-prototypes -ggdb )
and now the proposed code:
#include <stdio.h> // perror(), fopen(), FILE, fseek(), fread(), printf(), fclose()
#include <stdlib.h> // exit(), EXIT_FAILURE
//#include <string.h>
struct data
{
int numero;
};
// prototypes
void myRead( void );
void myWrite( void );
void myRead()
{
FILE *arquivo = fopen("/tmp/URL_NEW", "r");
if( !arquivo )
{
perror( "fopen to read URL_NEW failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
struct data dataTemp;
// the following 'fseek()' skips over the first record
if( 0 != fseek(arquivo, sizeof(struct data), SEEK_SET) )
{
perror( "fseek failed" );
exit( EXIT_FAILURE );
}
// implied else, fseek successful
// note: the returned value and the third parameter must match
while ( 1 == fread(&dataTemp, sizeof(struct data), 1, arquivo))
{
printf("\nNumber = ");
printf("%d\n", dataTemp.numero);
}
fclose(arquivo);
} // end function: myRead()
void myWrite()
{
struct data dataTemp;
FILE *arquivo = fopen("/tmp/URL_NEW", "w");
if( !arquivo )
{
perror( "fopen to write URL_NEW failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
for(int i=1; i < 2000;i++)
{
dataTemp.numero = i;
// note: the returned value and the third parameter must match
if( 1 != fwrite(&dataTemp, sizeof(struct data), 1, arquivo) )
{
perror( "fwrite failed" );
exit( EXIT_FAILURE );
}
// implied else, fwrite successful
}
fclose(arquivo);
}
int main( void )
{
myWrite();
myRead();
}
a sample of the early and late output:
Number = 2
Number = 3
Number = 4
....
Number = 1996
Number = 1997
Number = 1998
Number = 1999
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.