您在fwrite 上遇到了段错误,因为fp 是NULL
您正在使用argv[0],这是可执行文件的名称。而且,您没有检查fopen 的结果是否成功/失败。
这是更正后的代码。 #if 0 表示旧的/您的代码。 #if 1 表示添加代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if 1
#include <errno.h>
#endif
int
main(int argc, char **argv)
{
int ops = 0;
// do this first so you abort before prompting the user
#if 1
// skip over program name
--argc;
++argv;
if (argc < 1) {
fprintf(stderr,"No output filename specified\n");
exit(1);
}
FILE *fp = fopen(*argv, "wb");
if (fp == NULL) {
fprintf(stderr,"Unable to open '%s' -- %s\n",argv[0],strerror(errno));
exit(1);
}
#endif
printf("Enter number of operations:");
scanf("%d", &ops);
#if 0
FILE *fp = fopen(argv[0], "wb");
#endif
// int i = 0;
int key = 0;
char op;
for (int i = 0; i < ops; i++) {
printf("\nEnter a key:");
// printf("\nhi");
scanf("%d", &key);
printf("\nh");
fwrite(&key, sizeof(int), 1, fp);
printf("\nNow enter an operation for that key:");
scanf("%c", &op);
fwrite(&op, sizeof(char), 1, fp);
}
fclose(fp);
printf("Operations file written");
return EXIT_SUCCESS;
}
更新:
当我之后运行我的程序时,我输入了我的密钥,它会打印出该密钥的输入操作,但是一旦发生这种情况,就会出现下一个密钥的提示。可能是什么原因造成的?
您没有考虑换行符 ['\n'] 和/或空格。 scanf 可以将这些视为空格,因此在格式中添加空格可以处理:
scanf(" %d",&key);
通常,%d 在查找数字时会跳过空格,因此 %d 就足够了。
但是,这仍然不能解决真正的问题,因为问题在于 第二 scanf。上面的 [更改] scanf 将在格式之前删除空格。
但是,它不会在数字之后去除空格。
因此,第二个 scanf("%c",&op); [for a single char] 将由 previous 中的 newline 实现 /em> 输入行。
所以,我们需要:
scanf(" %c",&op);
以上是一个简单的修复。大多数情况下,您必须查阅 scanf 的联机帮助页并尝试使用格式说明符来转储换行符。
还有一些事情......
fwrite [有效地]:
fwrite(buf,number_of_elements,size_of_single_element,stream)
因此,您的 fwrite 参数与惯用语相反。
另外,当您为key 执行fwrite 时,它会将二进制 值(即4 个字节)写入输出文件。没关系,但是,这就是你真正想要的 [vs.正在做(例如)printf(fp,"%d");?
无论如何,这是更新后的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if 1
#include <errno.h>
#endif
#ifndef DEBUG
#define DEBUG 0
#endif
#define dbgprt(_fmt...) \
do { \
if (DEBUG) \
printf("DEBUG: " _fmt); \
} while (0)
int
main(int argc, char **argv)
{
int ops = 0;
// do this first so you abort before prompting the user
#if 1
// skip over program name
--argc;
++argv;
if (argc < 1) {
fprintf(stderr,"No output filename specified\n");
exit(1);
}
FILE *fp = fopen(*argv, "wb");
if (fp == NULL) {
fprintf(stderr,"Unable to open '%s' -- %s\n",argv[0],strerror(errno));
exit(1);
}
#endif
printf("Enter number of operations:");
scanf("%d", &ops);
#if 0
FILE *fp = fopen(argv[0], "wb");
#endif
// int i = 0;
int key = 0;
char op;
for (int i = 0; i < ops; i++) {
printf("\nEnter a key:");
// printf("\nhi");
#if 0
scanf("%d", &key);
#else
scanf(" %d", &key);
#endif
dbgprt("key=%d\n",key);
printf("\nh");
#if 0
fwrite(&key, sizeof(int), 1, fp);
#else
fwrite(&key, 1, sizeof(key), fp);
#endif
printf("\nNow enter an operation for that key:");
// NOTE/BUG: need to account for newline in scanf below
#if 0
scanf("%c", &op);
#else
scanf(" %c", &op);
#endif
dbgprt("op='%c'\n",op);
#if 0
fwrite(&op, sizeof(char), 1, fp);
#else
fwrite(&op, 1, sizeof(op), fp);
#endif
}
fclose(fp);
printf("Operations file written\n");
return EXIT_SUCCESS;
}