【问题标题】:How to read a file word by word? [closed]如何逐字读取文件? [关闭]
【发布时间】: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,那么没有办法检测一个句子是否结束,下一个句子开始,唯一的选择是计算单词,而之前知道每个句子必须有多少单词将它们组合在一起。

标签: c function


【解决方案1】:

在您的while 循环中,将char 存储在char 数组中,而不是立即打印您读取的字符。添加一个if 语句,该语句进行比较以检查读取的char 是否为空格字符。如果是,则应打印存储的数组并将数组的索引设置回 0。

例子:

#include <stdio.h>
#include <stdlib.h>

int main(){

    FILE * fr = fopen("file.txt","r");
    char ch[100];
    int index = 0;

    if(fr != NULL){
        while((ch[index] = fgetc(fr)) != EOF){
            //printf("%c\n", ch[index]);
            if(ch[index] == ' ') {
                ch[index] = '\0';
                printf("Here is your: %s\n", ch);
                index = 0;
            }
            else {
                index++;
            }
        }
        fclose(fr);
    }
    else{
        printf("Unable to read file.");
    }
    return 0;
}

【讨论】:

  • 它给了我这是你的:11。我在文本文件中没有 11s。
  • @NatashaBibi 我进行了编辑。它现在应该可以工作了。
  • 它有效但不像我想要的那样意味着它给了我这是你的:日期,这是你的:的,这是你的:出生。我希望这是您的出生日期。
  • 很清楚。如果你想要,那么你必须以某种方式区分文件中的单词组。它目前的实现方式使得代码无法自行确定哪些单词应该组合在一起。
  • 需要检查单词的第一个字母是否为大写,如果是则它是下一个句子的开头......这是我能想到的唯一方法分组把单词变成句子。
【解决方案2】:

根据提供的文件的文本行,我们可以假设如果单词的第一个字母是大写的,那么它就是下一个句子的开头:

Name Date of birth ID card number Phone number Address Account Fixing year

并用它来将行分成句子。

下面是 Christopher 将单词分组为句子的代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(){

    FILE * fr = fopen("file.txt","r");
    char ch[100];
    int index = 0;

    if(fr != NULL){
        while((ch[index] = fgetc(fr)) != EOF){
            if(index > 0 && ch[index-1] == ' ' && isupper(ch[index])) {
                ch[index-1] = '\0';
                printf("Here is your: %s\n", ch);
                ch[0] = ch[index];
                index = 1;
            }
            else {
                index++;
            }
        }
        ch[index] = '\0';
        printf("Here is your: %s\n", ch);
        fclose(fr);
    }
    else{
        printf("Unable to read file.");
    }
    return 0;
}

【讨论】:

  • 我想读第二行。因为我把这些词放在数组中,这些词的值没有空格。
猜你喜欢
  • 2013-04-30
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
相关资源
最近更新 更多