1) How can I use fscanf to read only the first character of each word in a text file?

2) How can I use fscanf to read only alphabets in a text file?

- I tried %[a-zA-Z], but that doesn't work for some reason.

Recommended Answers

All 2 Replies

1. Directly? You can't. But you can read word by word:

char word[32];
    ...
    while (fscanf(f,"%31s",word) == 1)
        printf(">%c<\n",word[0]); /* that's the 1st char */

2. You can't. Read line by line with fgets (or char by char with fgetc) then select ABCs explicitly.

> - I tried %[a-zA-Z], but that doesn't work for some reason.
Well we don't know what you tried.
Nor do we know what else is in the file.

Most likely, you didn't skip all the stuff which is in [^a-zA-Z] and thus you got stuck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.