c - 程序编译,但随后停止工作

标签 c debugging

因此,以下程序可以编译,但是当我运行它时,它停止工作。我对汇编语言了解不够,无法调试程序。有人可以让我了解一下问题所在吗?如果您需要 Students.dat 文件,请告诉我。谢谢。

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

#define MAX 100
#define FIRSTNAME 7
#define INITIAL  1
#define LASTNAME 9
#define STREET  16
#define CITY    11
#define STATE   2
#define ZIP    5
#define START 0
#define AGE 3
#define GPA 5
#define FIRSTINDEX 8
#define IINDEX 10
#define STREETINDEX 20
#define CITYINDEX 37
#define STATEINDEX 49
#define ZIPINDEX 52
#define AGEINDEX 57
#define GPAINDEX 64

//Structures
typedef struct{
    char street[STREET + 1];
    char city[CITY + 1];
    char state[STATE + 1];
    char zip[ZIP + 1];
} Address;

typedef struct{
    char firstname[FIRSTNAME + 1];
    char intial[INITIAL + 1];
    char lastname[LASTNAME + 1];
    Address ofstudent;
    int age;
    double gpa;
} Student;

//prototypes

void strsub(char s1[], char s2[], int start, int length);
void readcontent(int *index, Student student[]);

int main(void)
{
    Student student[MAX];   // creates an empty array of student structures
    int index;
    index = 0;
    readcontent(&index, student);           // reads the file Students.dat
}

void readcontent(int *index, Student student[])         // opens content and puts it into the     array???
{
    char line[MAX];
    FILE *datafile;         //Student.dat file // its a pointer to that file and will now be        referenced to as datafile
    index = 0;

    /* try to open the data file */
    datafile = fopen("Students.dat", "r");
    if (datafile == NULL) {
        printf("'Students.dat' file not found.\n");
        exit(1);
    }

    /* read line at a time from data file */
    while (!feof(datafile)) {
        printf("Student info \n \n");
        fgets(line, MAX, datafile);
        strsub(line, student[*index].firstname, START, FIRSTNAME); // all starting indexes are given variables
        strsub(line, student[*index].intial, FIRSTINDEX, INITIAL);
        strsub(line, student[*index].lastname, IINDEX, LASTNAME);
        strsub(line, student[*index].ofstudent.street, STREETINDEX, STREET);
        strsub(line, student[*index].ofstudent.city, CITYINDEX, CITY);
        strsub(line, student[*index].ofstudent.state, STATEINDEX, STATE);
        strsub(line, student[*index].ofstudent.zip, ZIPINDEX, ZIP);
        student[*index].age = atoi(&line[AGEINDEX]);
        student[*index].gpa = atoi(&line[GPAINDEX]);
        *index++;
    }

    fclose(datafile);
}

void strsub(char s1[], char s2[], int index, int length) // strsub takes a string and puts another string into it at the end
{
    int i;

    for (i = 0; i < length; ++i)
        s1[i] = s2[index++];
        s1[i] = '\0';
}

最佳答案

首先更改readcontent中的这一行:

index = 0;

致:

*index = 0;

然后看看程序的其余部分是否有效。

编辑:正如理查德·彭宁顿指出的那样,还要更改:

*index++;

致:

(*index)++;

关于c - 程序编译,但随后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26985062/

相关文章:

django - 如何保护我的网络游戏免受作弊者的侵害?

c - 是否有可能从抽象/基本结构继承或在 C 中沿着这些线路模拟某些东西?

c++ - extern C 的 undefined reference

c - size_t (SIZE_MAX) 的最大值是否相对于其他整数类型定义?

c - 从哪里开始阅读 SQLite 源代码?

c - 超越比较 : compare whole function calls or xml blocks

将 lisp 字符串转换为 C 语言中的树 - 程序崩溃但在 Debug模式下运行

.net - 客户 PC 上的 CPU 卡在 100%,有任何调试建议吗?

linux - 为什么我不能拦截子进程的段错误信号?

php - 为什么 var_dump 在 PHP7 中格式不正确?