fscanf() 只读入没有标点符号的字符
fscanf() to read in only characters with no punctuation marks
我想从文本文件(在命令行中指定为参数的名称)中读入一些单词(在本例中为前 20 个单词)。当下面的代码运行时,我发现它也需要带字符的标点符号。
#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] = '[=10=]';
printf("word[%d] is %s\n", wordc, ptr[wordc]);
}
return 0;
}
当我遍历句子时:"Once when a Lion was asleep a little Mouse began running up and down upon him;","him" 后面会跟一个分号。
我把fscanf()
改成fscanf(myFile, "[a-z | A-Z]", ptr[wordc]);
,把整个句子当成一个词。
我怎样才能改变它以获得正确的输出?
您可以接受分号,然后再将其删除,如下所示:
将单词存储到 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]);
我没有测试过这段代码,所以可能有错别字之类的。
或者您可以切换
fscanf(myFile, "%s", ptr[wordc]);
为了
fscanf(myFile, "%29[a-zA-Z]%*[^a-zA-Z]", ptr[wordc]);
只捕获字母。 29 限制了字的大小,所以你不会溢出,因为你只分配了 30 个字符的大小
我想从文本文件(在命令行中指定为参数的名称)中读入一些单词(在本例中为前 20 个单词)。当下面的代码运行时,我发现它也需要带字符的标点符号。
#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] = '[=10=]';
printf("word[%d] is %s\n", wordc, ptr[wordc]);
}
return 0;
}
当我遍历句子时:"Once when a Lion was asleep a little Mouse began running up and down upon him;","him" 后面会跟一个分号。
我把fscanf()
改成fscanf(myFile, "[a-z | A-Z]", ptr[wordc]);
,把整个句子当成一个词。
我怎样才能改变它以获得正确的输出?
您可以接受分号,然后再将其删除,如下所示:
将单词存储到 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]);
我没有测试过这段代码,所以可能有错别字之类的。
或者您可以切换
fscanf(myFile, "%s", ptr[wordc]);
为了
fscanf(myFile, "%29[a-zA-Z]%*[^a-zA-Z]", ptr[wordc]);
只捕获字母。 29 限制了字的大小,所以你不会溢出,因为你只分配了 30 个字符的大小