Click here to Skip to main content
14,455,745 members
Rate this:
Please Sign up or sign in to vote.
See more:
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?
Updated 8-Oct-14 9:26am
[no name] 8-Oct-14 14:19pm
So open the file, read the contents, if you find that you have read any content at all, the file contains characters.
[no name] 8-Oct-14 14:27pm
thank you!
Rate this:
Please Sign up or sign in to vote.

Solution 1

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 of char 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.

Rate this:
Please Sign up or sign in to vote.

Solution 2

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);
Are you sure this is the real answer which could be posted as "solution" and even self-accepted as solution? To me, it looks like you asked a pretty unreasonable question (which would be OK), got an advice, followed it, and then accepted what you got, instead of that advice. Who are you are helping, with this solution? To me, it sounds as pure abuse of the forum. Don't yo thing it's the case?

At best, this code could be added to the question, using "Improve question" above.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100