简体   繁体   中英

read text from file

I want to read text from file. For example: HHEEHEHHEEHHHEEEHHHEEHEHHEHEEEEEEHHHHEEE

And I want to count how many "E" at every 5th symbol?

Take it step by step.

You need to:

  1. Open a file
  2. read data from the file
  3. retreive every 5th symbol
  4. Increment a counter if that symbol is an 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).

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.

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.

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).

#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..." . Symbols on the second line are:

  • "-" for an E not in a 5-th character position.
  • "|" for a 5-th character position that's not an E .
  • "*" for an E in a 5-th character position.
  • " " 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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