简体   繁体   English

从文件中读取文本

[英]read text from file

I want to read text from file. 我想从文件中读取文本。 For example: HHEEHEHHEEHHHEEEHHHEEHEHHEHEEEEEEHHHHEEE 例如:HHEEHEHHEEHHEEEEHHEEHEHHEHEEEEEEHHHHEEE

And I want to count how many "E" at every 5th symbol? 我想计算每5个符号中有多少个“ E”?

Take it step by step. 逐步进行。

You need to: 你需要:

  1. Open a file 开启档案
  2. read data from the file 从文件中读取数据
  3. retreive every 5th symbol 每5个符号返回一次
  4. Increment a counter if that symbol is an E. 如果该符号是E,则增加一个计数器。

So first solve part one, then part two and so on. 因此,首先解决第一部分,然后解决第二部分,依此类推。 If you have any problem at a given stage, post some code here outlining what you expect to see and what you are seeing and we'll help you. 如果您在给定阶段遇到任何问题,请在此处发布一些代码,概述您希望看到的内容以及所看到的内容,我们将为您提供帮助。

yeah homework I reckon. 是的,我认为是家庭作业。 If you have to ask that question then you really need to start at basics. 如果您必须问这个问题,那么您确实需要从基础开始。 Asking other people how to do it will cripple you when you reach harder topics. 当您遇到较难的话题时,问别人该怎么做会使您瘫痪。 Sorry, that's the best answer I can give. 抱歉,这是我能给的最好的答案。

Since this sounds like homework, it's algorithms only from me. 由于这听起来像作业,所以它只是我的算法。

You need to initialize both a position and count variable to 0 then start reading in character by character (with each character incrementing the position variable). 您需要将位置和计数变量都初始化为0,然后开始逐个字符读取字符(每个字符递增位置变量)。

Reading the characters means opening the file once then executing a loop for each read then closing the file once end-of-file has been detected. 读取字符意味着打开文件一次,然后为每次读取执行一个循环,然后在检测到文件结束时关闭文件。

Whenever the character is "E" and the position variable is a multiple of five, increment the count. 每当字符为“ E” 位置变量为5的倍数时,请递增计数。

Once all the characters have been read, output the count. 读取所有字符后,输出计数。

Update: 更新:

As requested, here's some code that demonstrates what I meant. 根据要求,这里有一些代码演示了我的意思。 Since you haven't provided any indication that you've tried it yourself, I've done it using standard C rather than C++ and I've not added any comments. 由于您没有提供任何表明您自己尝试过的指示,因此我使用标准C而不是C ++进行了此操作,并且未添加任何注释。

But I have put in enough debug stuff so you can see what I meant in the output (see below). 但是我已经放入了足够的调试资料,因此您可以在输出中看到我的意思(见下文)。 You still have some work to do to understand it but, if you post your own attempts, you'll get a lot more help. 您仍然需要做一些工作来理解它,但是,如果您发表自己的尝试,将会获得更多帮助。

Please don't try to pass this work off as your own. 请不要试图把这项工作当做自己的事来进行。 Your educators will no doubt be able to see this code as well as you (hence you will fail if this is indeed classwork) and you should use C++ constructs for input and output (iostreams rather than stdio.h). 您的教育工作者无疑将能够和您一样看到此代码(因此,如果这确实是课堂工作,那么您将会失败),并且应该将C ++构造用于输入和输出(iostream而不是stdio.h)。

#include <stdio.h>

char buff0[1000];
char buff1[1000];

int main (int argc, char *argv[]) {
    FILE *fin;
    int chPos;
    int chVal;
    int count;

    fin = fopen ("qq.in", "r");
    if (fin == NULL) {
        fprintf (stderr, "Cannot open qq.in\n");
        return 1;
    }

    *buff0 = '\0';
    *buff1 = '\0';
    count = 0;
    chVal = fgetc (fin);
    chPos = 0;
    while (chVal != EOF) {
        putchar (chVal);
        sprintf (&(buff1[strlen(buff1)]),"%c",chPos+'1');
        if ((chPos == 4) && (chVal == 'E')) {
            sprintf (&(buff0[strlen(buff0)]),"%c",'*');
            count++;
        } else {
            if (chPos == 4) {
                sprintf (&(buff0[strlen(buff0)]),"%c",'|');
            } else {
                if (chVal == 'E') {
                    sprintf (&(buff0[strlen(buff0)]),"%c",'-');
                } else {
                    sprintf (&(buff0[strlen(buff0)]),"%c",' ');
                }
            }
        }            chVal = fgetc (fin);
        chPos = (chPos + 1) % 5;
    }
    printf ("%s\n",buff0);
    printf ("%s\n",buff1);

    fclose (fin);

    printf ("There were %d occurrence(s)\n", count);

    return 0;
}

Here's the output: 这是输出:

HHEEHEHHEEHHHEEEHHHEEHEHHEHEEEEEEHHHHEEE
  --|-  -*   -*-   *- - |- --*--- |  --*
12345123451234512345123451234512345123451
There were 5 occurrence(s)

This had a single file called qq.in that contained your input string "HHEEHEHH..." . 它有一个名为qq.in文件,其中包含您的输入字符串"HHEEHEHH..." Symbols on the second line are: 第二行上的符号是:

  • "-" for an E not in a 5-th character position. "-"表示不在第5个字符位置的E
  • "|" for a 5-th character position that's not an E . 对于不是E的第5个字符的位置。
  • "*" for an E in a 5-th character position. "*"表示第5个字符位置的E
  • " " for everything else. " "用于其他所有内容。
#!/bin/ksh
E=`echo "HHEEHEHHEEHHHEEEHHHEEHEHHEHEEEEEEHHHHEEE" | cut -c 5,10,15,20,25,30,35,40 | sed 's/[^E]//g'`
echo ${#E}

That'll learn ya' to ask a specific question. 这将学问一个特定的问题。

;-) Keith. ;-)基思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM