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.
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.
Subreddit
Post Details
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/C_Programmi...