【发布时间】:2014-01-05 13:18:44
【问题描述】:
我正在尝试从包含以下行的文本文件中获取输入
John,.) 789.. 89,88,79,69
但是,我无法使用我的 sscanf 语句正确获取数字,我哪里出错了?名称打印正确,但标记是垃圾值...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[20];
int ID;
int marks[4];
} details;
int main()
{
char string[100];
details student[2];
FILE *ptr = fopen("testing7.txt", "r");
if ((ptr = fopen ("testing7.txt", "r")) != NULL)
printf("file successfully opened\n");
else
{
printf("file could not be opened\n");
return 0;
}
fgets(string,100, ptr);
// testing to see if string was obtained
printf("%s", string);
sscanf(string,"%s,.) %d.. %d,%d,%d,%d", student[0].name, &student[0].ID, &student[0].marks[0], &student[0].marks[1],&student[0].marks[2],&student[0].marks[3]);
printf("%s\t%d\t%d\t%d\t%d\t%d\n", student[0].name, student[0].ID, student[0].marks[0], student[0].marks[1], student[0].marks[2], student[0].marks[3]);
fclose(ptr);
return 0;
}
【问题讨论】:
-
@BLUEPIXY 你能看看这个,看看你能不能做点什么
-
请缩进您的代码。
-
我认为缩进不会那么久..
-
你应该总是缩进你的代码。这是一个好习惯。此外,我认为它足够长,难以阅读(至少对我而言)。
-
可以给我正确的 sscanf 语句
标签: c