我有以下简单的程序,可以逐个字符地读取文本文件。每次从文件中读取一个字符,都必须在 str 的末尾,这是一个字符串。出于这个原因,我做了一个名为 conc 的小函数,它获取字符,重新分配 str,然后获取字符串末尾的字符(str[count ] = ch).
在 EOF 字符之后,我将 '\0' 字符放入 str 作为字符串变量的结尾。
我的问题是为什么最后一个 printf 显示垃圾?有任何想法吗??
提前致谢。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void conc(char* str,char ch,int* count);
int main(int argc,char** argv)
{
char* str = (char*)malloc(sizeof(char));
char ch;
int count = 0,i;
FILE* fp = fopen("path","r");
if(fp == NULL){
printf("No File Found");
return 1;
}
ch=fgetc(fp);
while(ch!=EOF){
conc(str,ch,&count);
ch=fgetc(fp);
}
str[count] = '\0';
printf("%s",str);
free(str);
return(0);
}
void conc(char* str,char ch,int* count){
str[*count] = ch;
(*count)++;
//printf("\n%c",str[(*count)-1]);
str = (char*)realloc(str,(*count)+1);
}
最佳答案
问题在于您如何重新分配() 指针。您对 str
所做的更改不会修改 main()
中的原始指针。您只是在 conc()
中分配给指针 str
的副本。您需要将指针传递给指针才能对其进行修改。
void conc(char** str,char ch,int* count){
(*str)[*count] = ch;
(*count)++;
*str = realloc(*str,(*count)+1);
}
并从 main()
传递一个指向它的指针:
conc(&str,ch,&count);
更改原型(prototype)以匹配:
void conc(char** str,char ch,int* count);
其他注意事项:
1) 当 realloc()
失败时,它返回 NULL,您将丢失原始指针。所以你需要使用一个临时的并分配给原来的。
请参阅:Having dynamically allocated an array can I change its size?
2) Casting malloc()
/realloc()
etc is also dangerous.
3) 始终检查malloc()
等的返回值以查看内存分配是否失败。
3) 一次分配一个字符不是很有效。典型的方法是分配一个大小为 N 的缓冲区,并在您 realloc()
时将大小加倍。
关于C - 字符连接到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33973058/