0

我是编程新手,所以我在这里有点挣扎。目前我的错误与我的二维数组不兼容有关。这段代码的目的是读入一个大的文本文件,然后逐个字符地递归打印。

我的行动计划是将 txt 文件中读取的数据存储到二维数组中。此数据将使用递归函数存储,然后打印到另一个 txt 文件。

这是我当前的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


char fillArray(FILE* fileIn, char array, int rowCount, int colCount); 

int main (int argc, char* argv[]){
        
    int rowCount = 50000; 
    int colCount = 100; //arbitrary numbers chosen 
    char  array[rowCount][colCount];        
    
        FILE* fileIn = NULL;
        fileIn = fopen(argv[1], "r");       

        if (fileIn == NULL){
            
            printf("ERROR FILE DOES NOT OPEN");
            return 1; //to exit
            
        }   
/*  array = (char**)(malloc(rowCount*colCount* sizeof(*array)));
        if(!array){
            printf("ERROR: malloc wasnt assigned");
            return 1;
        }   */
        fillArray(fileIn, array, rowCount, colCount);       
        
            
        fclose(fileIn);
            
        
     free(array); 
    
    return 0;
}
char fillArray(FILE* fIn, char array, int rowCount, int colCount){
    if(rowCount == 0 && colCount == 0){
        return 0;
    }
    else{
        fscanf(fIn,"%s", &array[rowCount][colCount]);   
        if(array[rowCount][colCount] == '\n'){
            return fillArray(fIn, array, rowCount-1, colCount-1);
        }
        else{
            return fillArray(fIn, array, rowCount, colCount-1);
        }
    }
}
4

0 回答 0