C Isolation of "string only" in a text file

I have a text file that has 1 word followed by ~ 100 floating point numbers. Floating-point numbers are separated by a space, tab, or new line. This format is repeated several times in a text file.

For example, it looks like a text file:

one 0.00591 0.07272 -0.78274 ... 
0.0673 ...
0.0897 ...
two 0.0654 ...
0.07843 ...
0.0873 ...
three ...
...
...

My question is how can I count the number of words in a file, I tried to use fscanf, but as soon as it reads the first word, after that I need to skip all the floats to the next word.

Any help would be greatly appreciated.

Thanks.

+4
source share
4 answers

Here's a way to make it character by character (no buffer needed). I am pretty sure that logic sounds.

#include <stdio.h>

int is_alpha(char c)
{
    //only works for some character encodings
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
int main(void)
{
    FILE *file_ptr;
    int character;
    int prev_char_was_letter = 0;
    int word_count = 0;
    file_ptr = fopen("my_file.txt", "r");
    if (!file_ptr) 
    { 
        fprintf(stderr, "can't open file\n");
        return 1;
    }
    character = fgetc(file_ptr);
    while (character != EOF)
    {
        if (is_alpha(character) && !prev_char_was_letter) 
        {
            word_count++;
            prev_char_was_letter = 1;
        }
        else if (!is_alpha(character))
        {
            prev_char_was_letter = 0;
        }
        character = fgetc(file_ptr);
    }
    printf("%d\n", word_count);
    fclose(file_ptr);
}
+2

, , C.

  • ( ) .
  • . :
    • , . :
      • , .
      • .

, :

double . . ., , ( " " ).

+5

strtok() isalpha(). , .

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

char delim[4]={' ','\t',0x0a,0x0d};
#define MAX_LINE 1024

int isaword(char *);

int main(int argc,char **argv)
{
    FILE *fp;
    char line[MAX_LINE];
    char *s;
    int wcnt=0;

    if(argc==1)
    {
        fp = stdin;
    }
    else
    {
        fp = fopen(argv[1],"r");
    }
    if(fp==0)
    {
        return -1; ///file not found
    }
    while(!feof(fp))
    {
        s=fgets(line,MAX_LINE,fp);
        if(s)
        {
            s=strtok(line,delim);
            while(s!=NULL)
            {
                if(isaword(s))
                {
                    wcnt++;
                }
                s=strtok(NULL,delim);    
            }
        }
    }
    fclose(fp);
    printf("word count = %d",wcnt);
    return 0;
}

int isaword(char *w)
{
    int result = 1;
    int i;
    for(i=0;i<strlen(w);i++)
    {
        result = isalpha(w[i]);
        if(result==0)
        {
            break;
        }
    }
    return result;
}

- "" isalpha.

+1
source

You can do something like this:

void foo() {
    FILE *file = fopen("file.txt", "r");
    char buffer[10000]; // your choice
    while(fscanf(file, "%s", buffer) > 0) {
        int i = 0;
        int word = 0;
        int number_of_dots = 0;
        while(i < strlen(buffer)) {
            if(!isdigit(buffer[i]) && buffer[i] != '.') {
                if(!(i == 0 && buffer[i] == '-')) {
                    word = 1;
                    break;
                }

            }
            if(buffer[i] == '.') number_of_dots++;
            i++;
        }
        if(word || number_of_dots > 1) {
            printf("%s ", buffer);
            puts("It a word!");
        }
    }
}
0
source

Source: https://habr.com/ru/post/1624022/


All Articles