简体   繁体   中英

fscanf() to read in only characters with no punctuation marks

I would like to read in some words (in this example first 20) from a text file (name specified as an argument in the command line). As the below code runs, I found it takes punctuation marks with characters too.

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

int main(int argc, char * argv[]){
int wordCap = 20;
int wordc = 0;
char** ptr = (char **) calloc (wordCap, sizeof(char*));
FILE *myFile = fopen (argv[1], "r");
if (!myFile) return 1;
rewind(myFile);
for (wordc = 0; wordc < wordCap; wordc++){
  ptr[wordc] = (char *)malloc(30 * sizeof( char ) );
  fscanf(myFile, "%s", ptr[wordc]);
  int length = strlen(ptr[wordc]);
  ptr[wordc][length] = '\0';
   printf("word[%d] is %s\n", wordc,  ptr[wordc]);
}
 return 0;
}

As I pass through the sentence: "Once when a Lion was asleep a little Mouse began running up and down upon him;", "him" will be followed with a semicolon.

I changed the fscanf() to be fscanf(myFile, "[az | AZ]", ptr[wordc]); , it takes the whole sentence as a word.

How can I change it to make the correct output?

You could accept the semi-colon and then remove it latter, like so:

after you've stored the word in ptr[wordc]:

i = 0;
while (i < strlen(ptr[wordc]))
{
    if (strchr(".;,!?", ptr[wordc][i])) //add any char you wanna delete to that string
        memmove(&ptr[wordc][i], &ptr[wordc][i + 1], strlen(ptr[wordc]) - i);
    else
        i++;
}
if (strlen(ptr[wordc]) > 0) // to not print any word that was just punctuations beforehand
    printf("word[%d] is %s\n", wordc,  ptr[wordc]);

I haven't tested this code, so there might be a typo or something in it.

Alternatively you could switch

fscanf(myFile, "%s", ptr[wordc]);

for

fscanf(myFile, "%29[a-zA-Z]%*[^a-zA-Z]", ptr[wordc]);

to capture only letters. the 29 limits word size so you don't get overflow since you're allocating size for only 30 chars

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