gpt4 book ai didi

c - 用C读取文件时出现问题

转载 作者:行者123 更新时间:2023-11-30 18:11:35 25 4
gpt4 key购买 nike

我正在尝试使用 fread 读取 c 中的文件,但 fread 没有显示所有记录,只显示了一些记录。请注意,我使用 struct 写入文件并以相同的方式恢复。这是我的代码:

#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

如果我尝试使用 fseek,他们会在 10 条记录处丢失。有人知道我可以修复它或解决我的问题吗?

编辑:似乎使用 rb 和 wb 解决了问题,谢谢! :D

最佳答案

以下建议代码:

  1. 干净地编译
  2. 执行所需的功能
  3. 正确检查错误
  4. 不使用众所周知的系统函数名称作为本地函数但是,read()write() 是在 unistd.h 中原型(prototype)化的,因​​此代码不是直接问题,因为该头文件不是包括在内。

它不起作用的原因(有几个原因)

最值得注意的是,编译器将输出一些警告和错误消息。它能跑起来真是一个奇迹。

编译时,始终启用所有警告,然后修复这些警告。

(对于gcc,至少使用:-wall -wextra -pedantic -std=gnu11 我还使用:`-Wconversion -Wmissing-prototypes -ggdb )

现在建议的代码:

#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();
}

早期和晚期输出的示例:

Number = 2 

Number = 3

Number = 4

....

Number = 1996

Number = 1997

Number = 1998

Number = 1999

关于c - 用C读取文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46106533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com