Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 我的代码可以';t重新分配内存(realloc())_File_Realloc - Fatal编程技术网

File 我的代码可以';t重新分配内存(realloc())

File 我的代码可以';t重新分配内存(realloc()),file,realloc,File,Realloc,我要求编写代码,检查文本文件(file1.txt)中最大的单词,并将该大小的所有单词写入另一个文本文件(file1a.txt),但它说我有一个realloc问题…如果我在file1.txt中只写一个大字,它可以工作,但当我写2个或更多单词时,它不会通过重新分配 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <malloc.h>

我要求编写代码,检查文本文件(file1.txt)中最大的单词,并将该大小的所有单词写入另一个文本文件(file1a.txt),但它说我有一个realloc问题…如果我在file1.txt中只写一个大字,它可以工作,但当我写2个或更多单词时,它不会通过重新分配

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

    int check_big_word(FILE *read){
        int count_letter = 0;
        int max_letter_size = 0;
        char letter;

        letter = fgetc(read);

        while(!feof(read) && letter != EOF){
            count_letter = 0;
            while(!(letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
                if (feof(read)){
                    break;
                }
                letter = fgetc(read);
            }
            while((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
                if (feof(read)){
                    break;
                }
                count_letter++;
                letter = fgetc(read);
            }
            if( count_letter > max_letter_size ){
                max_letter_size = count_letter;
            }
        }
        rewind(read);
        return max_letter_size;
    }

    void print_all_big(FILE *read, FILE *write){
        int big_size = check_big_word(read);
        int count_letter = 0;
        int count_words = 0,i,beggining_of_string = 0;
        char letter;
        char *string_of_biggers;
        string_of_biggers = NULL;

        letter = fgetc(read);

        while(!feof(read)){
            count_letter = 0;
            while(!(letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
                if (feof(read)){
                    break;
                }
                count_words++;
                letter = fgetc(read);
            }
            while((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')){
                if (feof(read)){
                    break;
                }
                count_letter++;
                letter = fgetc(read);
            }
            if(count_letter == big_size){
                if(string_of_biggers == NULL){
                    string_of_biggers = (char*)malloc(sizeof(char)*(big_size+1));
                }else{
                    //******* THE PROBLEM IS HERE *****
                    string_of_biggers = (char*)realloc(string_of_biggers, sizeof(char)*(big_size+1));
                }
                rewind(read);
                fseek(read,count_words*sizeof(char),SEEK_SET);
                for(i = 0; i<big_size;i++){
                    *string_of_biggers = fgetc(read);
                    beggining_of_string++;
                    string_of_biggers++;
                }
                *string_of_biggers = ' ';
                beggining_of_string++;
                string_of_biggers++;
            }
            count_words += count_letter;
        }
        string_of_biggers = string_of_biggers - beggining_of_string;
        fputs(string_of_biggers,write);
        fclose(read);
        fclose(write);
    }


void main(){
    FILE *r, *w;
    r = fopen("file1.txt", "rt");
    w = fopen("file1a.txt", "wt");
    print_all_big(r,w);
}
#包括
#包括
#包括
#包括
int check_big_word(文件*读取){
整数计数字母=0;
int max_letter_size=0;
字符字母;
字母=fgetc(已读);
而(!feof(read)&&letter!=EOF){
计数字母=0;
而(!(字母>='a'&&letter='a'&&letter='a'&&letter='a'&&letter='a'&&letter最大字母大小){
最大字母大小=计数字母;
}
}
倒带(读);
返回最大字母大小;
}
无效打印(文件*读取,文件*写入){
int big\u size=检查大单词(读取);
整数计数字母=0;
int count_words=0,i,begging_of_string=0;
字符字母;
char*string_的_biggers;
字符串\u of_biggers=NULL;
字母=fgetc(已读);
而(!feof(read)){
计数字母=0;

调用realloc时(!(letter>='a'&&letter='a'&&letter='a'&&letter='a'&&letter必须传递上一次调用返回给malloc或realloc的确切指针。您没有这样做。您传递的是同一个变量,但已更改了变量中的指针


不要修改指针,而是使用索引。这样可以防止错误并使代码更具可读性。

此外,无需检查NULL。如果将NULL指针传递给realloc,它将执行malloc