写多个文件给出分段错误,signle文件没有

时间:2017-06-13 19:25:57

标签: c file

我编写了一个函数来将链表数据写入文件,当我在单个文件中编写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

main.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.

1 个答案:

答案 0 :(得分:-1)

我修改了代码中的一些拼写错误,这是我的版本:

<强>的main.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机器中调用loader程序来加载并执行程序main,在终端输入这个命令,然后点击 Enter 键。

生成

./main

[编辑待办]

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

  1. 单独编译和链接过程。
  2. 使用make file自动化它们。
  3. 一些额外的改进,解释,文件。
  4. 使用不同的链接程序。
  5. 在代码上做一些改进。