我正在尝试编写一个C程序,它接受MadLib大纲作为.txt文件。然后,该程序要求用户将一系列短语放入Madlib。这部分我已经完成了。但是,我希望能够将用户输入的MadLib短语插入到前面打开的.txt文件中。然后,我希望允许用户以不同的MadLib文件名保存已完成的.txt。MadLib大纲中放置了关键字,表示用户输入的单词应该放在哪里。(见下文)。
我应该如何用用户输入的短语替换这些占位符?
MadLib大纲.txt文件:
One of the most <adjective> characters in fiction is named
"Tarzan of the <plural-noun>." Tarzan was raised by a/an
<noun> and lives in the <adjective> jungle in the
heart of darkest <place>. He spends most of his time
eating <plural-noun> and swinging from tree to <noun>.
Whenever he gets angry, he beats on his chest and says,
"<funny-noise>!" This is his war cry. Tarzan always dresses in
<adjective> shorts made from the skin of a/an <noun>
and his best friend is a/an <adjective> chimpanzee named
Cheetah. He is supposed to be able to speak to elephants and
<plural-noun>. In the movies, Tarzan is played by <person's-name>.发布于 2014-11-06 11:44:40
这使用fscanf从文件中读取数据。它提示输入每种类型的单词,并将最终文本打印到输出文件中。可以将文件名作为命令行的一部分提供,就像在program inputfile outputfile中那样。如果命令行中没有文件名,则输入文件将使用madlib.txt的默认文件名,输出文件将使用madlib-out.txt。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main( int argc, char *argv[]) {
FILE *fpIn = NULL;
FILE *fpOut = NULL;
char cFileIn[100] = { 0};
char cFileOut[100] = { 0};
char cIn[500] = { 0};
char cType[100] = { 0};
char cWord[100] = { 0};
char cCh[2] = { 0};
char *pDash = NULL;
if ( argc == 3) { // use command line arguments if present
strcpy ( cFileIn, argv[1]);
strcpy ( cFileOut, argv[2]);
}
else { // default file names
strcpy ( cFileIn, "madlib.txt");
strcpy ( cFileOut, "madlib-out.txt");
}
fpIn = fopen ( cFileIn, "r");
if ( fpIn == NULL) {
printf ( "could not open input file %s\n", cFileIn);
return 1; // fopen failed
}
fpOut = fopen ( cFileOut, "w");
if ( fpOut == NULL) {
fclose ( fpIn); // close the input file
printf ( "could not open output file %s\n", cFileOut);
return 1; // fopen failed
}
// scan up to 499 characters stopping at <
while ( fscanf ( fpIn, "%499[^<]", cIn) == 1) {
// scan one character, should be the <
if ( ( fscanf ( fpIn, "%1s", cCh)) == 1) {
if ( cCh[0] == '<') {
fprintf ( fpOut, "%s", cIn); // print to the output file
// scan the type of word needed
if ( ( fscanf ( fpIn, "%99[^>]", cType)) == 1) {
if ( ( pDash = strstr ( cType, "-"))) {
*pDash = ' '; // remove - if present
}
// for each type, prompt and scan
printf ( "Enter a(n) %s\n", cType);
// skip whitespace then scan up to 99 characters stopping at newline
scanf ( " %99[^\n]", cWord);
fprintf ( fpOut, "%s", cWord); // print to the output file
}
if ( ( fscanf ( fpIn, "%1[>]", cCh)) == 1) {
; // scan the >
}
}
}
}
fclose ( fpIn); // close files
fclose ( fpOut);
return 0;
}发布于 2014-11-06 22:28:49
这是一个替代的答案,它只是找到和替换(因为编写一个解析器,不破坏不可靠的输入是困难的)。
它从用户读取值,使用strstr函数在文本中找到相关的标记,并替换用户值(如果标记出现不止一次)。替代办法包括:
密码..。
#include <stdio.h>
#include <string.h>
#define BUFSIZE 65535
#define MAXSTR 100
int main(int argc, char ** argv) {
char string_dict[4][3][MAXSTR] =
{ {"What is your name? ", "[name]", ""},
{"What country do you live in? ", "[country]", ""},
{"What is your age? ", "[age]", ""},
{"What is your favourite food?", "[favourite food]", ""} };
int arr_len = sizeof(string_dict)/sizeof(*string_dict);
char buf1[BUFSIZE];
char buf2[BUFSIZE];
char currtag[MAXSTR];
char *pos = buf1;
FILE *fd;
int i;
buf2[0] = '\0';
fd = fopen("input_file.txt", "r");
fread(buf1, BUFSIZE-1, 1, fd);
fclose(fd);
for (i = 0; i < arr_len; i++) {
printf("%s", string_dict[i][0]);
fgets(string_dict[i][2], MAXSTR, stdin);
string_dict[i][2][strlen(string_dict[i][2])-1] = '\0';
while (pos = strstr(buf1, string_dict[i][1])) {
*pos = '\0';
strcat(buf2, pos + strlen(string_dict[i][1]));
strcat(pos, string_dict[i][2]);
pos += strlen(string_dict[i][2]);
strcat(pos, buf2);
pos = buf1;
buf2[0] = '\0';
}
}
fd = fopen("output_file.txt", "w");
fputs(buf1, fd);
fclose(fd);
}这是相当有力的,但肯定不是万无一失的。问:当提示输入名称时,输入字符串[name]会发生什么?而且,您可以很容易地超过输入缓冲区。
input_file.txt看起来像:
My name is [name], and I am [age] years old.
I live in [country] and my favourite nosh
is [favourite food].发布于 2014-11-06 22:49:30
既然已经有了真正的答案,只需简单地指出,如果您有选择,如果您不使用C,您可以更容易地完成它。Python会使它变得微不足道……
infile = open("input_file.txt", "r")
template = infile.read()
infile.close()
questions = [ ["What is your name? ", "[name]"],
["How old are you? ", "[age]"],
["Which country are you from? ", "[country]"],
["What is your favourite food? ", "[favourite food]" ] ]
for q in questions:
template = template.replace(q[1], input(q[0]))
outfile = open("output_file.txt", "w")
outfile.write(template)
outfile.close但也许你别无选择。
https://stackoverflow.com/questions/26770032
复制相似问题