Is there any if statement to check if a file is empty or not?
cyberguy007
0
Newbie Poster
Recommended Answers
Jump to PostAssuming by "empty" you mean if you try to read any characters, the input will fail immediately at end-of-file, then testing for an empty file is trivial:
#include <fstream> #include <iostream> int main() { const char *filename = "test.txt"; std::ifstream in ( filename ); if ( !in …
Jump to PostAssuming same things as Narue, you can also just peek the character
without reading it, just in case the file is not empty.#include<iostream> #include<fstream> using namespace std; int main() { ifstream read("test.txt"); if(!read) return 0; bool isEmpty = read.peek() == EOF; cout << boolalpha << "test …
All 11 Replies
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
cyberguy007
0
Newbie Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Dave Sinkula
2,398
long time no c
Team Colleague
Narue
5,707
Bad Cop
Team Colleague
mrnutty
761
Senior Poster
cyberguy007
0
Newbie Poster
Narue
5,707
Bad Cop
Team Colleague
cyberguy007
0
Newbie Poster
Narue
5,707
Bad Cop
Team Colleague
sanof3322
0
Newbie Poster
Naveen508
commented:
but it is not working
+0
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.