Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中从带有空格的文件中读取字符串_C_String_File_Text_Whitespace - Fatal编程技术网

在C语言中从带有空格的文件中读取字符串

在C语言中从带有空格的文件中读取字符串,c,string,file,text,whitespace,C,String,File,Text,Whitespace,我正在用c语言制作测验程序,我的程序的一部分是从文本文件中获取问题,计算并打印它们。但当我每次看到空白时都要打印或计数时,它会跳转到另一个变量。我怎样才能解决它 那是我的txt文件 What is the Capital of France? Paris Roma London Istanbul Belgrad 在我的节目里是怎样的 What a)is b)the c)capital d)of e)France 我就是这样被问的 FILE *fp = fopen("fp.txt",

我正在用c语言制作测验程序,我的程序的一部分是从文本文件中获取问题,计算并打印它们。但当我每次看到空白时都要打印或计数时,它会跳转到另一个变量。我怎样才能解决它

那是我的txt文件

What is the Capital of France? Paris Roma London Istanbul Belgrad
在我的节目里是怎样的

What
a)is
b)the
c)capital
d)of
e)France
我就是这样被问的

    FILE *fp = fopen("fp.txt", "a+");
    gets(questions[n].question_part);        
    gets(questions[n].a);
    gets(questions[n].b);
    gets(questions[n].c);
    gets(questions[n].d);
    gets(questions[n].e);
    questions[n].answer=getch();
    fprintf(fp, "%s %s %s %s %s %s %c", questions[n].question_part, questions[n].a, questions[n].b, questions[n].c, questions[n].d, questions[n].e, questions[n].answer);
    n++;

我怎么算

    int x=0;
    while(!feof(fp)){   
    fscanf(fp, "%s %s %s %s %s %s %c", questions[x].question_part, questions[x].a, questions[x].b, questions[x].c, questions[x].d, questions[x].e, questions[x].answer);
    x++;}
    n=x;
我如何打印

    FILE *fp;           
    fp = fopen("fp.txt", "r");
    int y;
    for(y=0;y<n;y++) 
    {       
        printf("\nQuestion number %d:\n",y+1);
        printf("Question: %s\n",questions[y].question_part);
        printf("a)%s\n",questions[y].a);
        printf("b)%s\n",questions[y].b);
        printf("c)%s\n",questions[y].c);
        printf("d)%s\n",questions[y].d);
        printf("e)%s\n",questions[y].e);
        printf("Answer: %c\n",questions[y].answer);
    } 
文件*fp;
fp=fopen(“fp.txt”,“r”);
int-y;

对于(y=0;y,问题在于
%s
格式读取以空格分隔的字符串

我建议你将问题和选项分别写在不同的行上来解决这个问题

fprintf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%c\n", questions[n].question_part, questions[n].a, questions[n].b, questions[n].c, questions[n].d, questions[n].e, questions[n].answer);

现在,您可以使用阅读每一行。

尝试使用另一个分隔符,例如“更改”

 fprintf(fp, "%s %s %s %s %s %s %c", questions[n].question_part, questions[n].a, questions[n].b, questions[n].c, questions[n].d, questions[n].e, questions[n].answer);


如果你想用一行来回答一个问题,否则我认为你应该回答一个建议:在一个换行符上打印每个部分。< /P> < P>或者,如果你不能控制输入文件,考虑使用“?”作为终止符的问题(假设这将适用于所有问题)。 一个建议:考虑使用指针“当前”问题-<代码> q>代码>在下面的EXAPMLE中,它将使代码更容易。

struct question *q ;   // whatever structure name describe the question/answer.
q = &question[x] ;
fscanf(fp, "%[^?]? %s %s %s %s %s %c",
    q->question_part, 
    q->a, q->b, q->c, q->d, q->e, q->answer);


首先,永远不要使用
gets
!这是一个在C规范中甚至已被删除的函数。请使用例如读取行。在写入和读取文件时,您需要使用另一个分隔符,而不是空格,否则fscanf功能将无法工作,也请阅读
fscanf(fp, "%s %s %s %s %s %s %c", questions[x].question_part, questions[x].a, questions[x].b, questions[x].c, questions[x].d, questions[x].e, questions[x].answer);
fscanf(fp, "%s_%s_%s_%s_%s_%s_%c", questions[x].question_part, questions[x].a, questions[x].b, questions[x].c, questions[x].d, questions[x].e, questions[x].answer);
struct question *q ;   // whatever structure name describe the question/answer.
q = &question[x] ;
fscanf(fp, "%[^?]? %s %s %s %s %s %c",
    q->question_part, 
    q->a, q->b, q->c, q->d, q->e, q->answer);