You are here:

C/File Read/Write failures

Advertisement


Question
Dear Narendra,

Thanks for the reply. Ulimit couldn't help me because the problem that I face is the disk space gets full and because of which it creates zero byte or partially created files. Sorry if I had posted the previous question incorrectly.

I wish to know if there's any standard C library or any other means that would help me to check the current disk space so that once it reaches a threshold point I shall exit from the program.

Currently I am planning to use the below system command to get the current disk space and exit once it reaches a threshold point. Please do let me know if there's any other better means to handle this issue.
"df -l ./ | awk '{print $5}' | grep '[0-9]' | sed -e 's/%//'"

Thanks once again
Dinesh


-------------------------
Followup To
Question -
Dear Narendra,

I am using oracle PRO*C in both UNIX and LINUX platform. The program that we use creates batches of files one by one writing line by line using fprintf function. At times uses fwrite function as well.

The problem that I face currently is if the file system becomes full it starts to create partial files and at times zero byte files.

I found through one of my friend that zero byte files shall be found using the stat library but havent found how to use it. But for partial created files we havent found any solution.

It will do help a lot if you shall let me know on how to verify/exit from the program if there is a file space issue and if it starts to create partial files or zero byte files.

Thanks
Dinesh
Answer -
Using ulimit, you can find out if you have reached the limit.
So, put this in your code and keep on checking this and you will know when to come out.

-Narendra

Answer
There is one utility called du, which you can try out and see if it works for you.

But, I don't think you need to check the disk space.
Instead check the return value of fwrite().
It has to return the number of bytes written.
If this is less than the actual number of bytes you are trying to write, then there must be error.
Also if it returns 0 then there is error.
So, check the errno and it it is ENOSPC, that means there is no space left on the device.
So, at that point you can exit, or delete this file or close it and again open in write mode so that you start from begining.

I think this is a better solution than checkig the space on device separately.

-Narendra

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.