Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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?
Posted
Updated 8-Oct-14 8:26am
v4
Comments
[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!

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.

—SA
 
Share this answer
 
Here is it!

C#
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);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Oct-14 15:50pm    
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.

—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900