【发布时间】:2015-03-22 20:56:38
【问题描述】:
我已经要求编写代码来检查文本文件(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);
}
给定的错误是:
* 检测到 glibc * ./lab14_1: realloc(): 无效指针:0x000000000107649c *** ======= 回溯:========= /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f4c0499fb96] /lib/x86_64-linux-gnu/libc.so.6(realloc+0x28e)[0x7f4c049a489e] ./lab14_1[0x400946] ./lab14_1[0x400a54] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f4c0494276d] ./lab14_1[0x400689] ======= 内存映射:======== 00400000-00401000 r-xp 00000000 00:14 25693062 /home/tailedwiz/Desktop/C_programming/lab14_1 00600000-00601000 r--p 00000000 00:14 25693062 /home/tailedwiz/Desktop/C_programming/lab14_1 00601000-00602000 rw-p 00001000 00:14 25693062 /home/tailedwiz/Desktop/C_programming/lab14_1 01076000-01097000 rw-p 00000000 00:00 0 [堆] 7f4c0470b000-7f4c04720000 r-xp 00000000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c04720000-7f4c0491f000 ---p 00015000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c0491f000-7f4c04920000 r--p 00014000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c04920000-7f4c04921000 rw-p 00015000 08:01 11013974 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f4c04921000-7f4c04ad6000 r-xp 00000000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04ad6000-7f4c04cd6000 ---p 001b5000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cd6000-7f4c04cda000 r--p 001b5000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cda000-7f4c04cdc000 rw-p 001b9000 08:01 11013953 /lib/x86_64-linux-gnu/libc-2.15.so 7f4c04cdc000-7f4c04ce1000 rw-p 00000000 00:00 0 7f4c04ce1000-7f4c04d03000 r-xp 00000000 08:01 11013933 /lib/x86_64-linux-gnu/ld-2.15.so 7f4c04ee3000-7f4c04ee6000 rw-p 00000000 00:00 0 7f4c04eff000-7f4c04f03000 rw-p 00000000 00:00 0 7f4c04f03000-7f4c04f04000 r--p 00022000 08:01 11013933 /lib/x86_64-linux-gnu/ld-2.15.so 7f4c04f04000-7f4c04f06000 rw-p 00023000 08:01 11013933 /lib/x86_64-linux-gnu/ld-2.15.so 7fffd5c4d000-7fffd5c6e000 rw-p 00000000 00:00 0 [堆栈] 7fffd5c9b000-7fffd5c9d000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] 中止(核心转储)
【问题讨论】: