如何检查文件中是否包含c中的字符? [英] How to check if a file contains characters in c?
本文介绍了如何检查文件中是否包含c中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想读一个文件是否包含任何caharactes。如果是这样,那么我将代码如果它的emty将 printf
我的消息和返回0;
。
如何使用C?
I want to read if a file contains any caharactes. If so then I will procide to code if its emty will printf
me a message and return 0;
.
How do I do that with C?
推荐答案
绝对可以将任何内容视为序列零个或多个字符。这一切都取决于假定的编码,但是即使是单个字节也始终至少有一种解释,将其视为一个字符(这种理解对于C来说非常典型,其中char的声明
用于声明单个字节。
因此,检查文件是否存在而不是空。网络搜索将为您提供一些功能。
-SA
Absolutely any content can be considered as a sequence of zero or more characters. It all depends on assumed encoding, but there is always at least one interpretation even of a single byte, to consider it as a character (and this understanding is very typical for C, where the declaration ofchar
is used to declare a single byte).
Therefore, check up if the file exists and not empty. Web search will give you some functions to do that.
—SA
这是它!
Here is it!
fscanf(fp,"%d",&num); // here goes the fscanf() command ?Do I need to use fget()?
fseek(fp, 0, SEEK_END);
if (ftell(fp) == 0) {
printf("file is emty"); // empty file
}
fseek(fp, 0, SEEK_SET);
这篇关于如何检查文件中是否包含c中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文