因此,以下程序编译,但是当我去运行它时,它会停止工作。我不太了解汇编语言来调试程序。有人可以告诉我一些问题可能是什么?如果您需要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';
}
答案 0 :(得分:0)
首先在readcontent
中更改此行:
index = 0;
要:
*index = 0;
然后看看你的程序的其余部分是否有效。
编辑:正如理查德·彭宁顿指出的那样,也改变了:
*index++;
要:
(*index)++;