Read text from file

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

And I want to count how many "E" is on every fifth character?

0


a source to share


4 answers


Take it step by step.

You need:



  • Open the file
  • read data from file
  • retreive every fifth character
  • Increment the counter if this character is E.

So first resolve the first part, then the second part, and so on. If you have any problems at this stage, post some code here where you will see what you expect to see and what you see, and we will help you.

+7


a source


Yes, homework, I believe. If you have to ask this question, you really need to start with the basics. Asking other people how to do this will let you down when you reach more complex topics. Sorry that the best answer I can give.



+3


a source


Since this sounds like homework, it only works for me.

You need to initialize both the position and the counting variable to 0, then start reading in character by character (with each character incrementing the position variable).

Reading characters means opening the file once, then looping through each read, and then closing the file when the end of the file is found.

Whenever the character is "E" and the position variable is a multiple of five, increase the counter.

Once all characters have been read, print the counter.

Update:

As requested, here is some code that demonstrates what I meant. Since you have not provided any indication that you have tried this yourself, I did it using standard C, not C ++, and I have not added any comments.

But I've added enough debugging stuff so you can see what I mean in the output (see below). You still have a job to figure this out, but if you post your own attempts, you get a lot more help.

Please do not try to convey this work as yours. Your teachers will no doubt be able to see this code as well as you (hence you will fail if this is really cool work) and you should use C ++ constructs for input and output (iostreams, not 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 one had one file named qq.in

which contained your input line "HHEEHEHH..."

. Characters on the second line:

  • "-"

    for E

    not in the 5th character position.
  • "|"

    for the 5th character position, not E

    .
  • "*"

    for E

    in the 5th character position.
  • " "

    for everything else.
+3


a source


#!/bin/ksh
E=`echo "HHEEHEHHEEHHHEEEHHHEEHEHHEHEEEEEEHHHHEEE" | cut -c 5,10,15,20,25,30,35,40 | sed 's/[^E]//g'`
echo ${#E}

      

This will teach you to ask a specific question.

;-) Keith.

+3


a source







All Articles