c - 得到 "Bus error 10": a program to take a file's data and calculate

标签 c debugging structure bus-error

这个任务是获取第一行作为标题,然后从文件中计算数据,负数由括号括起来。它将打印这些数据的标题和总和。每行都以换行符结束。我不知道问题是什么。我不知道如何处理“总线错误10”。也许是因为内存的分配,我不知道......任何人都可以帮助我吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// Our definition of a structure to represent department data.
struct dept {
   int id;
   char *name;
   int balance;
   struct dept *next;
};
typedef struct dept dept_t;



// For (a)
dept_t *create_dept(int dept_id, char *dept_name, int dept_balance);
// For (b)
dept_t *load_dept(int id);
// For (c)
void free_dept(dept_t *dept);
// For (d)
void print_dept(dept_t *dept);


int main(int argc, char *argv[])
{
   dept_t *d = load_dept(51423);
   print_dept(d);
   free_dept(d);
}


// (a)
dept_t *create_dept(int dept_id, char *dept_name, int dept_balance) 
{
    dept_t *d = malloc(sizeof(dept_t));
    d->id = dept_id;
    d->name = dept_name;
    d->balance = dept_balance;
    d->next = NULL;
    return d;
}


// (b)

char *prompt(FILE *fp)
{
   char ch;

   // skip leading whitespace
   do
   {
      fscanf(fp, "%c", &ch);
      if(feof(fp))
      {
   return NULL;
      }
   } while(ch == '\n');

   // read in until whitespace
   int cur_size = 8;
   char *str = malloc(sizeof(char) * cur_size);
   int cur_pos = 0;
   str[cur_pos] = ch;
   cur_pos++;
   do
   {
      fscanf(fp, "%c", &ch);
      if(feof(fp))
      {
   str[cur_pos] = '\0';
   return str;
      }
      str[cur_pos] = ch;
      cur_pos++;
      if(cur_pos >= cur_size - 1)
      {
   cur_size = cur_size * 2;
   str = realloc(str, sizeof(char) * cur_size);
      }
   } while(ch != '\n');
   str[cur_pos - 1] = '\0';

   return str;
}

dept_t *load_dept(int id)
{

    FILE *fp;
    char *filename;
    int balance = 0;
    char *name;
    char *string;
    int i;
    dept_t *d;

    filename = malloc(sizeof(char)*10);


    sprintf(filename,"%d.txt",id);


    if((fp = fopen(filename,"r")) == NULL)
    {
        fprintf (stdout,"Can't open \"%s\"file.\n",filename);
        exit(1);
    }


    name = prompt(fp);

    int value;
    for(i=0;i<6;i++)
    {
    string = prompt(fp);
    if (string[0]=='(')
    {   
        value = atoi(&string[1]);
        balance = balance - value;
    }    
    else
    { 
        value = atoi(string);
        balance = balance + value;
    }    
    }
    free(string);

    free(filename);
    if(fclose(fp)!=0)
    {
        fprintf(stderr,"Error closing file\n");
    }



     d = create_dept(id,name,balance); 

     return d;


}

// For (c)
void free_dept(dept_t *dept)
{
    free(dept->name);
    free(dept);
}


// For (d)
void print_dept(dept_t *dept)
{

    printf("Department: %s",dept->name);
    printf("     %d",dept->balance);
}

最佳答案

正如 user3629249 所指出的,由于函数:prompt() 可以返回空指针,因此您应该更改

    for(i=0;i<6;i++)
    {
    string = prompt(fp);

    while (string = prompt(fp))
    {

(数据线少于6条不会导致故障)。

关于c - 得到 "Bus error 10": a program to take a file's data and calculate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33900186/

相关文章:

c++ - 确定是否所有需要的头文件都包含在另一个头文件中

C 语言 - [原地复制] float* 到 char* 和反向

javascript - AJAX API 代码检查?地下天气API?

java - 从 Java 代码调试 Android 应用程序

c++ - 如何在 vector 中搜索结构项?

c - 将字符串数组添加到c中的字符指针

条件编译 - 实现替代方案

检查来自 C 程序的 UNIX 命令行参数、管道和重定向

c++ - 调试时出现 "myapp.exe has triggered a breakpoint"- 之后 Visual Studio 2015 Update 3 中的符号加载速度缓慢

flash - Haxe:没有帧的关键帧动画