所以我有一个正在上班的ANSI C(C89)项目。我陷入了这个过程。我正在

分割错误:11

问题,无论我怎么看,我似乎都无法解决问题。有人可以看一下我的代码并指出正确的方向吗?

/* CS315 Lab 3: C data types */

#include <stdio.h>
#include <stdlib.h> /*added this to provide a declaration for malloc*/

#define TENMB 1048576 /*1024 kilobytes or 10 megabytes */
#define ONEB 1

FILE * fp = NULL;

End(FILE * fp)/*made END a function */
{
    fclose(fp);             /* close and free the file */
    exit(EXIT_SUCCESS);     /* or return 0; */
}

initialize(int argc, char ** argv)
{
    /* Open the file given on the command line */
    if( argc != 2 )
    {
        printf( "Usage: %s filename.mp3\n", argv[0] );
        return(EXIT_FAILURE);
    }
    FILE * fp = fopen(argv[1], "rb");
    if( fp == NULL )
    {
        printf( "Can't open file %s\n", argv[1] );
        return(EXIT_FAILURE);
    }
    return 0; /*this might need to change */
}

readFile(FILE * fp)
{
    /* How many bytes are there in the file?  If you know the OS you're
    on you can use a system API call to find out.  Here we use ANSI
    standard function calls. */
    long size = 0;
    fseek(fp, 0, SEEK_END );        /* go to 0 bytes from the end */
    size = ftell(fp);               /* how far from the beginning? */
    rewind(fp);                     /* go back to the beginning */

    if( size < ONEB || size > TENMB )
    {
        printf("File size is not within the allowed range\n");
        End(fp); /* switched from goto END:*/
    }

    printf( "File size: %.2ld MB\n", size/TENMB );  /* change %d to %ld, added .2 to print to 2 decimal places (maybe use f instead) */
    /* Allocate memory on the heap for a copy of the file */
    unsigned char * data = (unsigned char *)malloc(size);
    /* Read it into our block of memory */
    size_t bytesRead = fread( data, sizeof(unsigned char), size, fp );
    free(data); /* deallocation */
    if( bytesRead != size )
    {
        printf( "Error reading file. Unexpected number of bytes read:     %zu\n",bytesRead ); /* changed from %d to %zu */
        End(fp); /* switched from goto END:*/
        return 0;
     }
    return 0;
}

int main( int argc, char ** argv )
{
    initialize(argc, argv);

    readFile(fp);

    /* We now have a pointer to the first byte of data in a copy of the      file, have fun
    unsigned char * data    <--- this is the pointer */
}


谢谢你的帮助!

最佳答案

在代码中,您在全局和本地两次声明了fp

FILE * fp = NULL;

End(FILE * fp)/*made END a function */
{
    fclose(fp);             /* close and free the file */
    exit(EXIT_SUCCESS);     /* or return 0; */
}

initialize(int argc, char ** argv)
{
...
    FILE * fp = fopen(argv[1], "rb");
...


即使您正在编写C89,也不需要用标准来拾起坏事。例如。使用返回类型正确声明函数。

09-28 04:32