【问题标题】:writing multiple file gives segmentation fault, signle file doesn't写入多个文件会产生分段错误,单个文件不会
【发布时间】:2023-03-27 01:05:02
【问题描述】:

我编写了一个将链表数据写入文件的函数,当我将main()WriteData() 写入单个文件时,它工作得很好。但是当我将WriteData() 写入另一个文件并使用头文件将其包含在main 文件中时,它给了我分段错误。这是我的代码

abcd.h:

#ifndef _ABCD_H_
#define _ABCD_H_
struct node{
  char word[50];
  char sort[50];
  struct node *next;
}
void WriteData(struct node *head);
void read_data(struct node **head, FILE *fp);
#endif

ma​​in.c:

#include"abcd.h"
int main(int argc, char *argv[]) //file is given as command line argument
{
  FILE *fp = fopen(argv[1], "r");
  struct node *head = NULL;
  read_data(&head, fp); //loads the contents of the file to the list
  WriteData(head);
}

writeData.c:

#include"abcd.h"
void WriteData(struct node *head)
{
  FILE *fp = fopen("dictionary.txt", "w");
  if(fp == NULL){
    printf("Error opening file..\n"); 
    return;
  } 
  while(head != NULL){
    fprintf(fp, "\n%s:", head->word);
    fprintf(fp, "%s", head->sort);
    head = head->next;
  } 
  fclose(fp);
  printf("Data saved successfully..\n");
}

readData.c:

#include "abcd.h"
void read_data(struct node **head, FILE *fp)
{
  if(fp == NULL){
    printf("Error opening file..\n");
    return;
  }
  struct node temp;
  temp.next = NULL;
  struct node *hp, *curr;
  hp = *head;
  while(!feof(fp)){
    fscanf(fp, " %[^:]", temp.word);
    fseek(fp, 1, SEEK_CUR);
    fscanf(fp, " %[^\n]", temp.sort);
    struct node *temp2 = (struct node*)malloc(sizeof(struct node));
    if(temp2 == NULL){
      printf("Couldn't make new node by malloc:");
      return;
    }
    *temp2 = temp;
    if(hp == NULL){
      curr = hp = temp2;
    } 
    else
      curr = curr->next = temp2;
  }
  fclose(fp);
  *head = hp;
  printf("Data loaded successfully..\n");
}

错误:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a5cc80 in _IO_vfprintf_internal (s=0x604830, 
     format=<optimized out>, ap=ap@entry=0x7fffffffd928) at vfprintf.c:1632
1632    vfprintf.c: No such file or directory.

【问题讨论】:

  • 我在从文件加载数据后尝试打印列表以检查数据是否正确加载..列表正常..将数据写入文件时出现问题
  • read_data(&amp;head, fp); head 在哪里以及如何定义?
  • 它在main() 内部定义为struct node *head 并用NULL 初始化
  • valgrind 可能会在这里为您提供帮助,甚至只是小心地通过调试器。
  • 您错误地使用了feof。另外,请参阅使用perror 报告文件打开错误的错误消息的示例:stackoverflow.com/questions/5431941/…

标签: c file


【解决方案1】:

我修正了您代码中的一些错字,这是我的版本:

ma​​in.c

#include "hd.h"

int main(int argc, char *argv[]) //file is given as command line argument
{
  FILE *fp = fopen(argv[1], "r");
  struct node *head = NULL;

  //read_data(&head, fp); //loads the contents of the file to the list
 WriteData(head);
}

hd.h

#ifndef _hd_h_
#define _hd_h_
struct node{
  char word[50];
  char sort[50];
  struct node *next;
};

#include <stdio.h>
extern void WriteData(struct node *head);

#endif

write.c

#include "hd.h" 

void WriteData(struct node *head)
{
  FILE *fp = fopen("dictionary.txt", "w");
  if(fp == NULL){
    printf("Error opening file..\n"); 
    return;
  } 
  while(head != NULL){
    fprintf(fp, "\n%s:", head->word);
    fprintf(fp, "%s", head->sort);
    head = head->next;
  } 
  fclose(fp);
  printf("Data saved successfully..\n");
}

使用 GCC 编译:

gcc -Wall main.c write.c -o prog

你应该考虑我们必须编译两个.c文件,并将它们链接在一起 在 main 名称下制作可执行的目标代码(二进制文件)。

然后我们可以在类Unix机器中调用加载程序来加载和执行程序main,在终端输入这个命令然后点击Enter键。

运行

./main

[编辑-待办事项]

感谢 Basile Starynkevitch 提供宝贵的 cmets 并在我的帖子中显示一些错误。

  1. 单独的编译和链接过程。
  2. 使用 make 文件将它们自动化。
  3. 一些额外的改进、解释、文档。
  4. 使用不同的链接器程序。
  5. 对代码进行一些改进。

【讨论】:

  • 每个c函数默认都是extern,即使我们没有在函数名geeksforgeeks.org/understanding-extern-keyword-in-c前显式写extern
  • 编译命令错误,应该是gcc -Wall -g main.c write.c -o prog,运行命令应该是./prog,调试命令应该是gdb ./prog
  • @BasileStarynkevitch 但这种编译方式对我有用
  • 但它可能不会做你想要的(没有链接步骤,通过将-v 也传递给你的gcc 命令来检查)。阅读关于invoking GCC 的章节......您当然首先需要警告和调试信息。
猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 2017-03-16
  • 2019-08-15
  • 2021-07-23
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
相关资源
最近更新 更多