Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

4
What could be a reason fopen fails?
Post Body

I'm executing a C binary that is meant to create 1000000 files "dir1/file1.txt", "dir1/file2.txt", "dir2/file1.txt", "dir5/file100.txt" etc.

To create a new file, I first use the system call

FILE* fp = fopen(file_path, "w");

to open the file. Then I write data to the file, and immediatelly close the file.

Most of the time the file creation succeeds, but occasionally the "fopen" part fails. I'm sure the file path is legit.

What could be a reason that fopen fails? Could one of the following be true:

1) There is a limit of how many files a C program can create during a single run?

2) I fail do free memory of other objects, and eventually the fopen system call does not have enough memory to create a new FILE pointer?

3) The standard "free" method leaves fragmented memory, and eventually there is no memory left for the fopen system call to create a new FILE pointer?

Update: thanks for suggesting to inspect the error code! Turns out I was opening a file in two ways, based on the if statement, and I was closing the file properly only in one of the two paths. The second path was rarely executed, thus it looked as if I was creating files correctly. But it turns out I had too many files open at once.

Author
Account Strength
70%
Account Age
3 years
Verified Email
Yes
Verified Flair
No
Total Karma
537
Link Karma
268
Comment Karma
269
Profile updated: 6 days ago
Posts updated: 5 days ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 year ago