在C中读取文件时出现问题

时间:2017-09-07 23:34:06

标签: c

我正在尝试使用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 个答案:

答案 0 :(得分:-1)

以下提议的代码:

  1. 干净地编译
  2. 执行所需的功能
  3. 正确检查错误
  4. 不使用众所周知的系统函数名称来表示本地函数 但是,read()write()unistd.h中的原型,因此代码没有直接问题,因为不包含该头文件。
  5. 它不起作用的原因(有几个原因)

    最值得注意的是,编译器将输出几条警告和错误消息。 它曾经是一个奇迹。

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

    (对于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