C 保存一个程序的输入
我有一个由两部分组成的计划理念:C 保存一个程序的输入,c,io,io-redirection,C,Io,Io Redirection,我有一个由两部分组成的计划理念: 我想跟踪一个程序的输入,并将输入保存到一个文件中 然后,稍后将保存的输入反馈回程序。(我已经完成了) 简单输入程序: testProgram.c 1 #include <stdio.h> 2 int main() { 3 char str[100]; 4 int i; 5 6 printf("Enter a value : "); 7 scanf("%s %d", str, &i); 8
1 #include <stdio.h>
2 int main() {
3 char str[100];
4 int i;
5
6 printf("Enter a value : ");
7 scanf("%s %d", str, &i);
8
9 printf("\nYou entered: %s %d\n", str, i);
10
11 return 0;
12 }
这些输入应记录在上一个程序的输入(我知道很混乱)的输出文件中。此文件看起来像:
hello
3
执行程序时,默认情况下将创建三个文件,例如
standard_input [file descriptor = 0 && File pointer = stdin]
standard_output [file descriptor = 1 && File pointer = stdout]
standard_error [file descriptor = 2 && File pointer = stderr]
要完成第一步:在从用户获取输入之前刷新标准输入文件中的内容
退出程序前,将文件指针移到初始位置,并将标准输入文件的所有内容复制到“input.txt”中。
第二步:
稍后要从“input.txt”向程序提供输入,您可以使用函数freopen()
假设文件“intput.txt”包含输入
freopen("input.txt","r",stdin);
现在,stdin被指向“input.txt”
默认情况下,scanf将从stdin指向的文件中读取
请尝试以下程序:
#include <stdio.h>
int main() {
char str[100];
int i;
printf("Enter a value : ");
freopen("input.txt", "r",stdin);
scanf("%s %d", str, &i);
printf("\nYou entered: %s %d\n", str, i);
return 0;
}
#包括
int main(){
char-str[100];
int i;
printf(“输入值:”);
freopen(“input.txt”,“r”,stdin);
scanf(“%s%d”,str,&i);
printf(“\n您输入了:%s%d\n”,str,i);
返回0;
}
希望上面的答案能对您有所帮助。从您的评论中,我得到的问题是“如何获取用户输入,然后将问题和答案写入程序开头的文件中,以便稍后从该文件读回程序?” 尽管很明显,您应该以可用的形式维护内存中的数据,以避免文件I/O,但仍需要学习如何以这种方式使用文件。您有两个选项,可以在
“rw”
(读/写)模式下打开文件,并使用倒带()
、lseek()
或fgetpos()/fsetpos()
按需移动文件位置指示器,更基本的方法是在“w”
写入模式下打开文件进行写入,将所需内容写入文件,fclose()
文件,然后在读取模式下打开该文件,并以读取模式重新读取数据
让我们看一下基本情况,看看这是否就是您想要的。要打开文件进行写入,只需向fopen()
提供一个文件名,然后验证您的文件是否确实已打开并可供使用,例如,使用程序第一个参数中提供的文件名,您可以执行以下操作:
#include <stdio.h>
#include <string.h>
#define MAXC 1024 /* if you need a constant, #define one (or more) */
int main (int argc, char **argv) {
char buf[MAXC], pet[MAXC]; /* read buffer, and buffer to store pet */
FILE *fp = NULL; /* FILE* pointer to hold open file stream */
if (argc < 2) { /* validate at least 1 argument given */
fprintf (stderr, "usage: %s outfilename\n", argv[0]);
return 1;
}
/* validate file open for writing */
if ((fp = fopen (argv[1], "w")) == NULL) {
perror ("file open for write failed");
return 1;
}
现在您已经阅读了用户输入,并使用strcspn()
修剪了尾随的'\n'
,您已经准备好编写文件的第一个问题和答案,例如
fprintf (fp, "type of pet?: %s\n", pet); /* write Q & A to file */
对尽可能多的输入重复提示、读取、修剪和输出到文件,例如
/* prompt user with 2nd question */
fprintf (stdout, "your %s (\"has fleas\"/\"has no fleas\")? ", pet);
if (!fgets (buf, MAXC, stdin)) { /* read/validate input */
fputs ("(user canceled input)\n", stderr);
return 1;
}
buf[strcspn(buf, "\n")] = 0; /* trim trailing '\n' from buf */
fprintf (fp, "your %s %s\n", pet, buf); /* write Q & A to file */
现在关闭您一直在写入的文件:
if (fclose (fp)) /* always validate close after writing to file */
perror ("fclose");
接下来,打开文件以读取并读取文件中存储的所有数据行,并将这些行输出到stdout
,以便确认所写内容:
if ((fp = fopen (argv[1], "r")) == NULL) { /* open/validate for reading */
perror ("file open for read failed");
return 1;
}
fputs ("\ndata written to file:\n\n", stdout); /* write heading */
while (fgets (buf, MAXC, fp)) /* read/validate each line from file */
fputs (buf, stdout); /* print it to stdout */
fclose (fp); /* simply close file after reading */
return 0;
}
这是基本的方法。编译并尝试一下:
示例使用/输出
$ ./bin/rdstdin_wrtfile dat/petfile.txt
type of pet?: dog
your dog ("has fleas"/"has no fleas")? has fleas
data written to file:
type of pet?: dog
your dog has fleas
从shell中检查文件内容本身:
$ cat dat/petfile.txt
type of pet?: dog
your dog has fleas
所以它没有太多内容,但关键是通过检查返回来验证每个输入和输出。这是唯一能让你确信你的行动成功完成的方法。让我知道这是你问的问题,还是我没有理解你问题的意图。我很乐意进一步提供帮助。您知道工具tee
?您可以使用它将自己的输入写入一个文件并将其写入输出,然后通过管道将其传输到您自己的程序组中。这样,程序的输入也会变成一个文件。(我问这个部分是为了让你详细说明你真正想要实现的目标。你似乎不想简单地从自己的程序将输入写入文件,这将是学习最基本的文件操作教程的主题。)当说“三部分”时,只列举1和2是令人恼火的……哇,我不知道这个工具。这几乎就是我想要的。我仍然希望用户能够在stdout传递到文件时看到它的提示(例如,输入值
)。如果我执行/a.out | tee file2.txt
,则提示中会出现延迟(它们在末尾吐出),提示会被吐出到文件中,而我只需要输入。也许我可以将它与另一个工具结合使用,或者以某种方式我不想让它成为现实?编辑:抱歉,这是编辑后的剩余内容--感谢您的帮助。我认为这意味着您指的是程序的输出,而不是程序的输入。抱歉,我自己目前没有tee。您是否尝试过tee file2.txt |/a.out
?不确定它是否以这种方式工作,无法尝试…如果这是一个愚蠢的问题,很抱歉,但是我如何才能获得stdin文件的内容?我最初不清楚我的问题,但这是一个很好的答案,我的问题似乎是什么。总有一天这会有用的。非常感谢。
$ ./bin/rdstdin_wrtfile dat/petfile.txt
type of pet?: dog
your dog ("has fleas"/"has no fleas")? has fleas
data written to file:
type of pet?: dog
your dog has fleas
$ cat dat/petfile.txt
type of pet?: dog
your dog has fleas