c - 无法将文件内容复制到缓冲区

标签 c

Operating System Used:Ubuntu 11.04
Compiler Used: GCC

程序相关文件:Git Hub Link

我正在尝试一个实现程序,它将完成与编译 .c 文件时的 CPP(C 预处理器)相同的工作。

在此特定代码中,Copy_file_to_buf 函数没有将整个文件复制到缓冲区中。 实际大小为 117406C,但在 copy_file_to_buf 函数中显示为 114689

EDIT: There is no data loss when I copied the contents of dummyfile to a buffer using same program but I've written copy_file_to_buf program seperately in temp.c file.

temp.c

#include<stdio.h>
main(int argc,char **argv)
{
    FILE *inputFile;
    int sizeofFile, rc;
    char *source_buf;
    fprintf(stderr, "In Copy_file_to_buf\n");
    sleep(1);

    inputFile=fopen(argv[1],"r");
    if (!inputFile) {
         fprintf(stderr, "Oops, failed to open inputfile \"%s\"\n", argv[1] );
         return NULL;
         }

    fseek(inputFile,0,SEEK_END);
    sizeofFile=ftell(inputFile);

    fseek(inputFile,0,SEEK_SET);
    source_buf=calloc(1,1+sizeofFile);
    rc = fread(source_buf,sizeofFile,1,inputFile);
    /* now check rc */
    fprintf(stderr, "Size of the file=%d; fread returned %d.\n", sizeofFile, rc);
    //sleep(5);
    fclose(inputFile);
    source_buf[sizeofFile] = 0;
    puts(source_buf);
    return source_buf;
}

对于下面的代码,fseek() 和 ftell 似乎没有按预期工作。

puts("Copying dummyfile contents");
test_buf=Copy_file_to_buf("dummyfile");
puts(test_buf);

Filename: preprocessor.c

#include"myheader.h"

/*   agrv[1]=preprocessor
*    argv[2]=test.c
*
*   Program on PREPROCESSOR
*
*   Steps:
* 1.Removal of comments.
* 2.Inclusion of headerfiles.
* 3.Macro substitution.
*     a.function like arguments
*     b.Stringification
*     c.Concatenation
* 4.Conditional compilation
*     a.#Ifdef
*     b.#If
*     c.#defined
*     d.#Else
*     e.#Elif

*/


int
main(int argc, char *argv[])
{
   char *source_buf,*subBuf,*rmc_buf,*test_buf;
   char **main_header_names,**sub_header_names;
   int main_header_count,sub_header_count;

    source_buf=(char *)Copy_file_to_buf(argv[1]);//...
    rmc_buf=removeComments(source_buf);//...
    main_header_names=(char **)getMainHeaderNames(rmc_buf);//...
    source_buf=(char *)Copy_file_to_buf(argv[1]);//...
    rmc_buf=removeComments(source_buf);//...
    main_header_count=mainHeaderCounter(rmc_buf);//...
    printf("Main Header Count=%d",main_header_count);//...
    includeHeaders(main_header_names,main_header_count);

    subBuf=(char *)Copy_file_to_buf("pre.i");//...
    sub_header_names=(char **)getSubHeadersNames(subBuf);//...

    subBuf=(char *)Copy_file_to_buf("pre.i");//...
    sub_header_count=subHeadersCounter(subBuf);//...

    WriteSubHeadersToFile(sub_header_count,sub_header_names,"dummyfile");//...

    puts("Copying dummyfile contents");

    test_buf=Copy_file_to_buf("dummyfile");
    puts(test_buf);

    /*test_buf=removeComments(test_buf);
    puts(test_buf);
    sub_header_names=(char **)getSubHeadersNames(test_buf);
    test_buf=(char *)Copy_file_to_buf("dummyfile");
    sub_header_count=subHeadersCounter(test_buf);

    WriteSubHeadersToFile(sub_header_count,sub_header_names,"dummyfile2");
    printf("Line:%d   File:%s",__LINE__,__FILE__);
    */

return 0;
}

Filename:CopyFile.c

#include"myheader.h"

//Copying input file data into source_buf

char * Copy_file_to_buf(char *fileName)
{
    FILE *inputFile;
    int sizeofFile;
    char *source_buf;
    puts("In Copy_file_to_buf");

    inputFile=fopen(fileName,"r");
    fseek(inputFile,0,2);
    sizeofFile=ftell(inputFile);
    sizeofFile++;
    fseek(inputFile,0,0);
    source_buf=calloc(1,sizeofFile);  
    fread(source_buf,sizeofFile,1,inputFile);
    printf("SIZE OF THE FILE=%d",sizeofFile);
    //sleep(5);
    fclose(inputFile);

    return source_buf;
}

最佳答案

检查 fseek() 的返回值(以及所有其他库调用!)

if (fseek(inputFile, 0, SEEK_END)) perror("seek to end");
sizeofFile = ftell(inputFile);
if (sizeofFile == -1) perror("ftell");

关于c - 无法将文件内容复制到缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955694/

相关文章:

c - 如何在 QNX momentics IDE 中添加特定头文件的路径?

c - 使用 VS Code 调试 C 子进程

c++ - 堆优化(但不限于)单线程使用

c++ - 如何分配一个大小为 50,000,000 个 long int 数字的数组

c - 如果在循环中插入错误的数字或字符,则会输出错误

c - 链表中的链表

objective-c - 如何在 C 或 Objective C 中的 IP/UDP 数据包中构建、格式化我自己的数据包?

c - 在 C 中使用 msgpack 接收流

c - C中复合类型的目的是什么?

android - 我们如何使用 USER-AGENT 字符串来区分 1.笔记本电脑/台式机 2.IOS 3. Android 4.其他手机(BB、诺基亚、MS Mobile)