473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to check is a file is empty

8
Hi, i would like to check is a file is empty after opening it using i/o processing.
is there any function provide by fstream to check is a file is empty?

thanks.
Dec 12 '06 #1
2 9944
DeMan
1,806 1GB
If you mean empty (as opposed to does not exist) i would suggest checking whether you are at EOF (end of file)
Dec 12 '06 #2
horace1
1,510 Expert 1GB
Hi, i would like to check is a file is empty after opening it using i/o processing.
is there any function provide by fstream to check is a file is empty?

thanks.
you can use the fstat() function to obtain information on a file including its size, e.g. see file_stat.st_size in the following program
Expand|Select|Wrap|Line Numbers
  1. // read file status, print size, dates etc
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. int main()
  7. {
  8. struct stat file_stat;
  9. int         status;
  10. char filename[50]={0};
  11. printf("enter filename ? ");
  12. scanf("%s", filename);
  13. int fildes = open(filename, O_RDWR);             // open file
  14.  
  15. // get file status for details see
  16. // http://www.opengroup.org/onlinepubs/007908799/xsh/fstat.html
  17. status = fstat(fildes, &file_stat);                 // get its status
  18.  
  19. // print file status informations for details see
  20. //   http://www.opengroup.org/onlinepubs/007908799/xsh/sysstat.h.html
  21. printf("file %s size %d\n", filename, file_stat.st_size);        // print status
  22. printf("last access %s\n", ctime(&file_stat.st_atime));
  23. printf("last mofification %s\n", ctime(&file_stat.st_mtime));
  24. printf("last status change %s\n", ctime(&file_stat.st_ctime));
  25. return 0;
  26. }
  27.  
Dec 12 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Krechting | last post by:
Hi ALl, I have a code that checks if the documents in a hyperlink field are still where they should be. I use fileexist(). First I want to filter out all the hyperlink fields that are empty. I...
30
by: S. van Beek | last post by:
Dear reader A record set can be empty because the condition in the query delivers no records. Is there a VBA code to check the status of a record set, record set empty
4
by: Magix | last post by:
Hi, I would like to check if the data file is empty or not before reading it. How should I achieve that ? fseek/ftell ? ....fopen process here(..) if filesize is 0 // mean empty data printf...
19
by: wetherbean | last post by:
Hi group..I am writing a playlist management protocol where I have a file that holds all the playlists and a file that holds all the songs....before a playlist is created I need to check to see if...
5
by: Shapper | last post by:
Hello, I am using this code line to check i a variable exists: If cookie Is Nothing Then .... How can I check if cookie is empty when cookie exists? Thanks, Miguel
7
by: lphang | last post by:
I am reading a simple text file that will contains three set of string values as follow: "1234567", "ABC123456", "" I have my codes as follow trying to check for an empty string, with the input...
31
by: noagbodjivictor | last post by:
How to check if a string is empty in python? if(s == "") ??
16
by: mtuller | last post by:
I am creating a page for nomination and want to let the information pass if any field are filled out, but if none are filled out, a message will appear. I can't get the check to happen on multiple...
10
by: klharding | last post by:
I am reading the contents of a text file into variables. All works well if the text file does not exist, or it does exist and contains 4 rows of data. But I need to account for rows being empty or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.