【发布时间】:2020-05-14 22:30:22
【问题描述】:
我很困惑如何在文件中存在的每个单词后添加换行符。
文本文件中的单词
Name Date of birth <---- I put this in code
John 02\02\1999 <---- I want to jump to this line
我想要这个
Here is your: Name
Here is your: Date of Birth
但它给了我这个
Here is your: N
Here is your: a
Here is your: m
Here is your: e
而且我不知道如何获得它。
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fr = fopen("/home/bilal/Documents/file.txt","r");
char ch;
if(fr != NULL){
while(!feof(fr)){
ch = fgetc(fr);
printf("Here is your %c\n: ", ch);
}
fclose(fr);
}
else{
printf("Unable to read file.");
}
return 0;
}
【问题讨论】:
-
fscanf与%s格式? -
你一次要求一个角色,这就是你得到的。
-
date of birth中有3个字。看来您想逐行打印,而不是逐字打印。 -
在文件中是写成
Date of birth还是Date of Birth?如果所有句子中每个单词的第一个字母都是大写的(如Date of birth),则可以自动分隔句子,但如果不是这种情况,例如Date of Birth,那么没有办法检测一个句子是否结束,下一个句子开始,唯一的选择是计算单词,而之前知道每个句子必须有多少单词将它们组合在一起。