【发布时间】:2014-04-02 23:19:49
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct fileData
{
char fileName[100];
int size;
char type;
long timestamp;
};
void print(struct fileData *myFile);
int main(void)
{
struct fileData *toUse = malloc(sizeof(toUse));
char temp[100] = {0};
printf("Enter the type:");
getchar();
scanf("%c", toUse->type);
getchar();
printf("Enter the filename:");
fgets(temp, 100, stdin);
strcpy(toUse->fileName, temp);
printf("Enter the access time:");
scanf("%lf", toUse->timestamp);
printf("Enter the size:");
scanf("%d", toUse->size);
print(toUse);
free(toUse);
}
void print(struct fileData *myFile)
{
printf("Filename: %s - Size: %d - Type: [%c] - Accessed @ %ld\n", myFile->fileName, myFile->size, myFile->type, myFile->timestamp);
}
好的,这就是程序应该做的事情:
- 为文件的数据创建结构(必须在 main 中使用指针)
- 为结构分配内存,并提示/读取有关结构的数据
- 编写 1 个将打印出该数据的函数
所以...以上是我的代码,我有几个问题:
-
1234563按回车它卡在标准输入中并将其解析为scanf,因此将getchar()的工作放在一起。但是有没有办法避免这种情况一起发生?这在我的任何其他程序中从未发生过......
如您所见,我正在使用 fgets 来获取字符串,因为文件名可能包含空格,而 scanf 无法使用。但我的问题是,我是否必须将其存储在 temp 中,然后将其复制过来,或者我可以这样做: fgets(toUse->fileName, 100, 标准输入); 我假设不是,出于同样的原因,我不能使用它: toUse->fileName = "测试"; 那么我目前的做法是正确的,还是有更好的方法?
现在对于导致我的程序失败的实际问题,在“输入访问时间:”中的下一个读数,它将让我输入一个数字,但只要我按下回车,我就会得到一个 Seg Fault..所以为什么?是因为我使用的是 %lf 吗?这是完全不同的东西吗?我应该只在 scanf 中使用 %l 吗? [当我这样做时,它会直接跳过问题..我假设出于同样的原因它会跳过其他问题..处理输入的东西]。那么可能是这样,我输入长“1234567890”后的输入导致它出现段错误还是我做错了什么?
回答的任何问题都会有所帮助,谢谢!
【问题讨论】:
标签: c pointers struct scanf fgets