简体   繁体   中英

Reading words from file with fscanf

I have a file .txt with the following encoding:

Alpha-Beta-Gamma-Delta-Epsilon
Zeta-Eta-Theta-Iota-Kappa-Lamda-Mi
Ni-Ksi-Pi-Ro-Sigma

I want to read those words and store them in an array. I read those words with fscanf(fp, "%[^-]", word)

But when I put fscanf inside a while, it keeps reading the same word. If I put it inside the while statement it doesn't repeat.

The point is to read every word separately from Alpha to Sigma

I provide you a minimal-verifiable code with the problem:

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

int main(){
    FILE *fp;
    char *word = (char*)malloc(40);
    int count = 0;
    char* Words[30]; //max 30
    fp = fopen("MyWords.txt","r"); //read only
    int r = 1;
    while(1){
        if(fscanf(fp, "%[^-]", word) == EOF) break;
        printf("%s",word);
    }
    fclose(fp);
    return 0;
}

Note: I don't want to modify the file. Also how should I handle with the \\n characheter

Limit input with a width specifier of 39, 1 less than the 40 bytes available to word as fscanf() will append a null character . Continue reading as long as successful in that fscanf() returned 1. Consume the following delimiter be it a '-' or '\\n' ,

while(fscanf(fp, "%39[^-\n]", word) == 1) {
    printf("<%s>\n",word);
    fscanf(fp, "%*[-\n]"); // consume delimiter, no need to save.
}

Note: Use [^-\\n] and not [^\\n-] as placing the - later looks like the beginning of an incomplete scan-set range.

The above answer works just fine, If you don't want regex complications in your code even though it's simple, use strtok after reading the whole string out. Then add it onto the array.

while(1){
    if(fscanf(fp, "%s", word) == EOF) break;
    printf("%s\n",word);
    const char s[2] = "-";
    char *token;
    token = strtok(word, s);
    while( token != NULL ) 
    {
        printf("%s\n", token );
        token = strtok(NULL, s);
    }
}

Which gives the output

Alpha-Beta-Gamma-Delta-Epsilon

Alpha
Beta
Gamma
Delta
Epsilon

Zeta-Eta-Theta-Iota-Kappa-Lamda-Mi

Zeta
Eta
Theta
Iota
Kappa
Lamda
Mi

Ni-Ksi-Pi-Ro-Sigma

Ni
Ksi
Pi
Ro
Sigma

Cheers!

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