写入和读取文件时出现SIGSEV错误

Raj*_*med 0 c runtime-error

我正在编写代码来读写文本文件.但是当我运行我的代码时,我收到了一个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");
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ger 7

数组:

char *name[] = {"Raju", "Nayan", "Hanif"};
char *id[] = {"100", "101", "102"};
float cgpa[] = {3.50, 5.00, 4.00};
Run Code Online (Sandbox Code Playgroud)

声明3个元素但是

for(i=0; i<5; i++){
    fprintf(fp, "%-10s%-10s%-1.2f\n", name[i], id[i], cgpa[i]); /*write to file*/
}
Run Code Online (Sandbox Code Playgroud)

试图迭代超过5,从而超越数组的界限.


Chi*_*era 5

您的代码有一些问题:

  • 您正在向文件中写入每个变量的5个元素,但每个变量只有三个元素 - 导致缓冲区溢出
  • getname,getid变量只是指针,没有存储空间

要找到这些错误,您应该可以使用调试器.

这是有效的代码:

#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);
}
Run Code Online (Sandbox Code Playgroud)

如果你有权访问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  }
Run Code Online (Sandbox Code Playgroud)

注意这一行:#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  }
Run Code Online (Sandbox Code Playgroud)

再次注意这一行:#2 0x080485be in main () at sowrong.c:21
第二个SEGFAULT,因为你的变量只是没有存储空间的指针.