1

我有下面的代码,它在 xcode 中编译得很好,但是当我把它带到 Microsoft Visual Studio 时,我得到了一堆错误。

    void openfile(int mapArray[MAX_HEIGHT][MAX_WIDTH], int *interest, int *dimension1, int *dimension2)
    { 
    int counter = 0;
    char buffer;
    int rowss, colss;
    *interest = 0;

    FILE *f;
    f = fopen(FILENAME, "r");
    if (f==NULL) {
            printf("Map file could not be opened");
            return 0;
    }


    // create char array the dimensions of the map
    fscanf(f, "%d %d" , dimension1, dimension2 );
    // printf("%d %d\n" , dimensions[0], dimensions[1]);


    // Reads the spaces at the end of the line till the map starts
    buffer=fgetc(f);
    while (buffer!='*') {
            buffer=fgetc(f);
    }

    // Read the txt file and print it out while storing it in a char array
    while (buffer!=EOF) {

            mapArray[rowss][colss]=buffer;

            colss++;

            // Count up the points of interest
            if (((buffer>64)&&(buffer<90))||(buffer=='@') ) {
                                    counter++;

                            }

            // resets column counter to zero after newline
            if (buffer=='\n') {
                    colss=0;
                    rowss++;
            }
            buffer=fgetc(f);
    }

    // Closes the file
    fclose(f);
    *interest=counter;

    }

哪些部分造成了所有错误?我在尝试编译时得到了这个错误列表

提前致谢。

4

1 回答 1

0

我看到了一些直接的问题。首先,您没有初始化rowsscolss在使用它们之前,因此它们可以包含任何值。

其次,fgetc()返回一个int以便您可以检测文件结尾。通过使用 achar来保存返回值,您违反了与标准库的约定。

第三,0如果文件名无法打开,则返回 a,尽管函数被指定为返回void(即,什么都没有)。

毫无疑问,这是编译器发现的三个错误,可能还有其他错误,您可能应该将错误列表与您的问题一起发布,以便进行更详尽的分析。

于 2012-10-14T05:12:30.640 回答