[英]program received signal sigsegv segmentation fault
I made a program from a diffrent compiler. 我用不同的编译器编写了一个程序。 I cant run it on university.
我不能在大学上运行它。 the programm read a text file called board.txt, it counts diffrent words etc
程序读取了一个名为board.txt的文本文件,它计算了不同的单词等
here is the project to download: https://mega.co.nz/#!CN11xS6Q!w549XDt4T7huRyTjv3J-b3a8vcsbbMrzEGIb2X4RRSM 这是要下载的项目: https : //mega.co.nz/#!CN11xS6Q!w549XDt4T7huRyTjv3J-b3a8vcsbbMrzEGIb2X4RRSM
Here's part of the code: 这是代码的一部分:
void countwords(FILE *board){
int word=0;
char *a;
printf("\ncounting words...\n");
rewind(board);
while(!feof(board)){
*a=fgetc(board);
if(*a==' ')
word++;
}
printf("%d",word+1);
}
a
is a pointer, and you never initialize it. a
是一个指针,并且您永远不会初始化它。 Effectively, it points to some random location in memory; 实际上,它指向内存中的一些随机位置; and you are writing to that memory location!
您正在写入该内存位置! It was just dumb luck that it didn't crash on your own computer.
没在您自己的计算机上崩溃,真是愚蠢的运气。
The pointer variable itself is useless; 指针变量本身是无用的。 you only read and process one character at a time, so a simple
char
variable will suffice. 您一次只能读取和处理一个字符,因此一个简单的
char
变量就足够了。
void countwords(FILE *board){
int word=0;
char c;
printf("\ncounting words...\n");
rewind(board);
while(!feof(board)){
c=fgetc(board);
if(c==' ')
word++;
}
printf("%d",word+1);
}
Make the same adjustment in your other functions. 在其他功能中进行相同的调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.