我正在编写代码来读写文本文件。但是当我运行我的代码时,我收到了一个SIGSEGV运行时错误。我需要帮助搞清楚代码有什么问题。
#include <stdio.h>
#include <stdlib.h>
int main(){
/*Variable for writing to file */
char *name[] = {"Raju", "Nayan", "Hanif"};
char *id[] = {"100", "101", "102"};
float cgpa[] = {3.50, 5.00, 4.00};
/*Variable for reading from file*/
char *getname, *getid;
float getcgpa;
int i;
FILE *fp;
fp = fopen("f:\\raju.nog", "w"); /*Create new file */
for(i=0; i<5; i++){
fprintf(fp, "%-10s%-10s%-1.2f\n", name[i], id[i], cgpa[i]); /*write to file*/
}
fclose(fp);
/*read from file and print to screen*/
fp = fopen("f:\\raju.nog", "r");
while(fscanf(fp, "%s %s %f", &getname, &getid, &getcgpa) != EOF){
printf("%-10s%-10s%-1.2f\n", getname, getid, getcgpa);
}
system("PAUSE");
}
答案 0 :(得分:7)
数组:
char *name[] = {"Raju", "Nayan", "Hanif"};
char *id[] = {"100", "101", "102"};
float cgpa[] = {3.50, 5.00, 4.00};
声明3个元素,但
for(i=0; i<5; i++){
fprintf(fp, "%-10s%-10s%-1.2f\n", name[i], id[i], cgpa[i]); /*write to file*/
}
试图迭代超过5,从而超越了数组的界限。
答案 1 :(得分:5)
您的代码存在一些问题:
要查找这些错误,您应该可以使用调试器。
以下是有效的代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
/*Variable for writing to file */
char *name[] = {"Raju", "Nayan", "Hanif"};
char *id[] = {"100", "101", "102"};
float cgpa[] = {3.50, 5.00, 4.00};
/*Variable for reading from file*/
// need storage space
char getname[40], getid[40];
float getcgpa;
int i;
FILE *fp;
fp = fopen("raju.nog", "w"); /*Create new file */
// Only 3 elements
for(i=0; i<3; i++){
fprintf(fp, "%-10s%-10s%-1.2f\n", name[i], id[i], cgpa[i]); /*write to file*/
}
fclose(fp);
/*read from file and print to screen*/
fp = fopen("raju.nog", "r");
while(fscanf(fp, "%s %s %f", &getname, &getid, &getcgpa) != EOF){
printf("%-10s%-10s%-1.2f\n", getname, getid, getcgpa);
}
return(0);
}
如果你有权访问gdb,你可能找到了第一个问题,然后是第二个问题。这是我的GDB会话的日志,指出导致代码中第一个错误的行:
(gdb) run
Starting program: /home/jrn/SO/a.out
Program received signal SIGSEGV, Segmentation fault.
0x005cc35e in vfprintf () from /lib/libc.so.6
(gdb) bt
#0 0x005cc35e in vfprintf () from /lib/libc.so.6
#1 0x005d33cf in fprintf () from /lib/libc.so.6
#2 0x08048565 in main () at sowrong.c:15
(gdb) list
1 #include <stdio.h>
2 #include <stdlib.h>
3 int main(){
4 /*Variable for writing to file */
5 char *name[] = {"Raju", "Nayan", "Hanif"};
6 char *id[] = {"100", "101", "102"};
7 float cgpa[] = {3.50, 5.00, 4.00};
8 /*Variable for reading from file*/
9 char *getname, *getid;
10 float getcgpa;
(gdb) list
11 int i;
12 FILE *fp;
13 fp = fopen("f:\\raju.nog", "w"); /*Create new file */
14 for(i=0; i<5; i++){
15 fprintf(fp, "%-10s%-10s%-1.2f\n", name[i], id[i], cgpa[i]); /*write to file*/
16 }
17 fclose(fp);
18 /*read from file and print to screen*/
19 fp = fopen("f:\\raju.nog", "r");
20 while(fscanf(fp, "%s %s %f", &getname, &getid, &getcgpa) != EOF){
(gdb) list
21 printf("%-10s%-10s%-1.2f\n", getname, getid, getcgpa);
22 }
23 }
注意这一行:#2 0x08048565 in main () at sowrong.c:15
这告诉您第15行发生了段错误。之后,您可以列出文件以查看该行。
所以现在在修复第15行之后,再次编译程序并通过GDB运行它以找到第二个问题。这是我的GDB会话的日志:
(gdb) run
Starting program: /home/jrn/SO/a.out
Program received signal SIGSEGV, Segmentation fault.
0x005cc35e in vfprintf () from /lib/libc.so.6
(gdb) bt
#0 0x005cc35e in vfprintf () from /lib/libc.so.6
#1 0x005d3410 in printf () from /lib/libc.so.6
#2 0x080485be in main () at sowrong.c:21
(gdb) list
1 #include <stdio.h>
2 #include <stdlib.h>
3 int main(){
4 /*Variable for writing to file */
5 char *name[] = {"Raju", "Nayan", "Hanif"};
6 char *id[] = {"100", "101", "102"};
7 float cgpa[] = {3.50, 5.00, 4.00};
8 /*Variable for reading from file*/
9 char *getname, *getid;
10 float getcgpa;
(gdb) list
11 int i;
12 FILE *fp;
13 fp = fopen("f:\\raju.nog", "w"); /*Create new file */
14 for(i=0; i<3; i++){
15 fprintf(fp, "%-10s%-10s%-1.2f\n", name[i], id[i], cgpa[i]); /*write to file*/
16 }
17 fclose(fp);
18 /*read from file and print to screen*/
19 fp = fopen("f:\\raju.nog", "r");
20 while(fscanf(fp, "%s %s %f", &getname, &getid, &getcgpa) != EOF){
(gdb) list
21 printf("%-10s%-10s%-1.2f\n", getname, getid, getcgpa);
22 }
23 }
再次注意这一行:#2 0x080485be in main () at sowrong.c:21
第二个SEGFAULT,因为你的变量只是没有存储空间的指针。
答案 2 :(得分:3)
你没有
的malloc记忆 char *getname, *getid;
答案 3 :(得分:3)
char *getname, *getid;
只分配指针,而不是内存来存储字符串。你需要这样的东西:
char getname[11];
char getid[11];
我已经猜到了printf
语句中的大小,但您可能需要比这些更大的数组。
答案 4 :(得分:2)
你的for循环计数器从0到4,但你的数组name
,id
和cgpa
只有3个元素:所以你要走出数组边界。