如何使用 fscanf 跳过文件中的内容? [英] How to skip stuff in a file using fscanf?
问题描述
我正在学习 C 文件操作,我有一个特定的问题,我需要将字母(A-Z、a-z)存储为从文件中读取的字符串.所以,如果我有 abcd&*dvcd
那么 abcd
是一个字符串而 dvcd
是一个字符串.
I am learning C file operations and I have a specific problem where I need to store alphabets (A-Z, a-z) as a string read from a file. So, if I have abcd&*dvcd
then abcd
is a string and dvcd
is a string.
基本上我有两个问题:
- 我事先不知道要存储的字符串的大小.
- 我需要跳过非字母字符
我想我可以通过使用 fscanf 格式字符串来解决这两个问题.这是我的想法.我需要以某种方式跳过使用 fscanf
格式字符串来查找我在文件中的位置.然后,我可以从保存的开始位置减去以找到我需要 malloc
的字符串.然后,我malloc
指定长度的字符串,然后,我可以回到保存的位置,实际读取它.
I thought I would solve both by using fscanf format string. This is my thought. I need to somehow skip using fscanf
format string to find my position in file. Then, I could subtract from the saved beginning position to find the string I need to malloc
. Then, I malloc
the string of specific length, and then, I could go back to the saved position and actually read it.
但是,我不知道如何告诉 fscanf
不存储扫描的字符串?
However, I have no idea how to tell fscanf
to not store the scanned string?
或者是否有另一种方法可以跳过非字母字符?
Or if there is another method that allows to skip non-alphabetic characters?
推荐答案
因为@suresh 的回答是对我不起作用(输出应该是 asd gddf
而不是 asd asd
),我写了另一个版本,它不是那么短,但对我来说很好用:
Since @suresh' answer is not working for me (the output should be asd gddf
and not asd asd
), I have written another version, which is not as short but works fine for me:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(void) {
char ch;
while(scanf("%1c", &ch) == 1) { //while we have characters to read
if(isalpha(ch)) { // if the next one is alphanumeric
ungetc(ch, stdin); // we put it back
char str[101];
scanf("%100[a-zA-Z]", str); // to read the whole string (including that char)
printf("found string %s\n", str);
}
}
return 0;
}
这里是一个有效的 ideone 示例.我为此使用标准输入,但您可以轻松地调整它以使用您打开的另一个文件.我用于 scanf
(%100[a-zA-Z]]
) 的格式说明符意味着它最多应该读取 100 个字符(str 的大小)只包含小写或大写字母.%1c
表示单个字符,unget
将该字符放回缓冲区以避免在它不是符号时跳过它.
Here is a working ideone example. I use stdin for this one, but you can easily adapt it to use another file you have open. The format specifier I used for scanf
(%100[a-zA-Z]]
) means that it should read at most 100 characters (the size of str) containing only lowercase or uppercase letters. %1c
means a single character, and unget
puts the character back into the buffer to avoid skipping it if it's not a symbol.
正如@m-m 指出的那样,方括号内的 -
的使用不是标准的,在某些实现中可能不可用.如果它不适合您,您可以随时使用 %100[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]
.
as @m-m pointed out, the use of -
inside the square brackets is not standard and may not be available in some implementations. If it is not available for you, you can always use %100[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]
.
这篇关于如何使用 fscanf 跳过文件中的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!