我正在编写一个程序来分割通过命令行输入的数据并将其放入单独的字段中。现在我在分割数据并将其分配给各自的指针时遇到问题。文本文件是这样的:
5, 08:00:00, 2, 60
其中5是患者编号,读数是早上8点,它是字段2,他得到60.我运行代码时不断出现分段错误错误,所以我把它放到了gdc并得到了这个,我想第88行:
*hours = atoi(j);
搞砸了:
欢迎使用健康监测系统
编程接收信号SIGBUS,总线错误。 get_field()中的0x0000000000400826
这是我的代码
/*
* Health Monitoring System
*/
#include <stdio.h>
#include <ctype.h>
#define MAXPATIENTS 5
#define MAXREADINGS 10
#define MAXTYPES 5
#define MAXTIME 8
/* One health type reading: timestamp + actual value */
typedef struct{
char timestamp[MAXTIME+1];
int value;
}Element;
/* Circular buffer of health type readings */
typedef struct{
int start; /* index of oldest reading */
int end; /* index of most current reading */
Element reading[MAXREADINGS];
}CircularBuffer;
/* Patient's health chart: ID + multiple health type readings */
typedef struct{
int id;
CircularBuffer buffer[MAXTYPES];
}Chart;
/*
* Health records for all patients defined here.
* The variable record is visible to all functions
* in this file, i.e. it is global.
*/
Chart record[MAXPATIENTS];
void main(){
int i, j;
/* initialize health data records for each patient */
for( i=0; i < MAXPATIENTS; i++ ){
record[i].id = i + 1;
for( j=0; j < MAXTYPES; j++ ){
record[i].buffer[j].start = 0;
record[i].buffer[j].end = 0;
}
}
printf("Welcome to the Health Monitoring System\n\n");
int id;
int hours;
int mins;
int secs;
int field;
int score;
get_field(&id, &hours, &mins, &secs, &field, &score);
printf("%d This is the ID\n", id);
printf("%d This is the hours\n", hours);
printf("%d This is the mins\n", mins);
printf("%d This is the secs\n", secs);
printf("%d This is the field\n", field);
printf("%d This is the score\n", score);
printf("\nEnd of Input\n");
}
int get_field(int* id, int* hours, int* mins, int* secs, int* field, int* score){
//get the patient ID
int z = getchar();
*id = z;
getchar();
getchar();
//this gets the hour
char j[MAXTIME];
int m,n = 0;
while((n=getchar()) != ':'){
j[m] = n;
m++;
}
*hours = atoi(j);
//this gets the mins
char k[MAXTIME];
n = 0;
m = 0;
while((n=getchar()) != ':'){
k[m] = n;
m++;
}
*mins = atoi(k);
// this gets the seconds
char l[MAXTIME];
n = 0;
m = 0;
while((n=getchar()) != ':'){
l[m] = n;
m++;
}
*secs = atoi(l);
getchar();
getchar();
// this gets the field
z = getchar();
*field = z;
getchar();
getchar();
// this gets the score
m = 0;
n = 0;
char x[MAXTIME];
while ((n=getchar()) != '\n'){
x[m] = n;
m++;
}
*score = atoi(x);
return 0;
}
答案 0 :(得分:3)
我会使用scanf而不是手动运行...
scanf("%d, %d:%d:%d, %d, %d", &field1, &hour, &min, &sec, &field2, &field3);
这可能会清理你遇到的一些问题。 希望有所帮助。
问题是,在get_field混乱的某个地方,你遇到了一个你不需要的错误。 scanf使用所谓的格式字符串,这意味着它们与SPECIFIC格式匹配,并将数据插入到各自的字段中。这会让你不必要的解析带来痛苦,并且当你可以调试硬件而不是那些琐碎的东西时,它会变得更容易。
答案 1 :(得分:1)
你不是在终止你正在建立的字符串; atoi()
可能正在读取数组的末尾。
// x will be uninitialized, not necessarily zero-filled
char x[MAXTIME];
while ((n=getchar()) != '\n'){
x[m] = n;
m++;
}
x[m] = '\0'; // make it a valid C string
*score = atoi(x);
所有这些都假设我们不会超过MAXTIME
个字符。
要避免这个问题:
while ((m < (MAXTIME - 1)) && ((n=getchar()) != '\n')){
答案 2 :(得分:0)
您将遇到以下问题:
*hours = atoi(j);
*mins = atoi(k);
*secs = atoi(l);
*score = atoi(x);
因为在调用atoi
之前,您没有使用空字符终止字符串。