2014-11-06 50 views
0

我正在嘗試編寫一個接受MadLib大綱作爲.txt文件的C程序。該程序然後要求用戶輸入一系列短語放入Madlib中。我有這部分完整。但是,我希望能夠將用戶輸入的MadLib短語&插入到早先打開的.txt文件中。然後,我希望能夠允許用戶將完成的MadLib保存在不同的.txt文件名下。 MadLib大綱在其中放置了關鍵字,表示用戶輸入的單詞應該放在哪裏。 (見下文)。帶重定向輸入和輸出的MadLib C程序

我應該如何去用這些用戶輸入的短語替換這些佔位符?

馬德利布大綱.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>. 
+1

您可以從'fgets'開始從您的文件中讀取一行,然後使用'strchr'來查找尖括號。然後將文本複製出來,並將尖括號中的文本替換爲您閱讀的文字。我不確定除了編寫程序來完成您所描述的內容之外,還有別的方法可以回答您的問題。如果您有更具體的問題,請告訴我。 – yellowantphil 2014-11-06 01:15:50

回答

1

這使用的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; 
} 
+1

您可以通過用空格替換''(如果存在)中的短劃線,然後在提示中使用修改的字符串,使用戶輸入的提示更加緊湊嗎? – 2014-11-06 17:19:27

+0

感謝您的幫助! – Josh 2014-11-06 20:27:21

0

這是另一種答案,只是沒有查找和替換(因爲編寫解析器不上狡猾的輸入突破是)。

它從用戶讀取值,使用strstr函數在文本中查找相關標籤並替換用戶值(如果標籤多次出現,則重複)。替換涉及:

  1. 保留標記前的所有內容。
  2. 將標記後面的內容複製到另一個字符串。
  3. 插入用戶值。
  4. 複製其他字符串的內容。

代碼...

#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]. 
0

而且,因爲有真正的答案已經,只是一個快速增加一指出,如果你有一個選擇,你可以做到這一點更容易,如果你沒有使用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 

但是也許你別無選擇。