Questions tagged [fclose]

fclose closes an open file resource, releasing it from memory and any write-locks on the file.

fclose closes an open file resource, releasing it from memory and any write-locks on the file.

It is a matching function for fopen, after read and writes have been completed. This function will release any write-locks on the file. While often unimportant for batch processes (when a process ends, write-locks are automatically released), it becomes important to release unused resources for long-running applications or background processes.

278 questions
81
votes
4 answers

What happens if I don't call fclose() in a C program?

Firstly, I'm aware that opening a file with fopen() and not closing it is horribly irresponsible, and bad form. This is just sheer curiosity, so please humour me :) I know that if a C program opens a bunch of files and never closes any of them,…
electrodruid
  • 1,083
  • 2
  • 9
  • 13
48
votes
2 answers

Create a file if one doesn't exist - C

I want my program to open a file if it exists, or else create the file. I'm trying the following code but I'm getting a debug assertion at freopen.c. Would I be better off using fclose and then fopen immediately afterward? FILE *fptr; fptr =…
karoma
  • 1,548
  • 5
  • 24
  • 43
30
votes
5 answers

Why glibc's fclose(NULL) cause segmentation fault instead of returning error?

According to man page fclose(3): RETURN VALUE Upon successful completion 0 is returned. Otherwise, EOF is returned and the global variable errno is set to indicate the error. In either case any further access (including another call to…
Vdragon
  • 411
  • 1
  • 4
  • 8
19
votes
4 answers

fclose() then free()?

Assume I have the following program: #include int main () { FILE * pFile; pFile = fopen ("myfile.txt","r"); fclose (pFile); //This never happens: free(pFile) return 0; } I have never seen a program which does…
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
16
votes
2 answers

How exactly does fopen(), fclose() work?

I was just wondering about the functions fopen, fclose, socket and closesocket. When calling fopen or opening a socket, what exactly is happening (especially memory wise)? Can opening files/sockets without closing them cause memory leaks? And…
Fabian
  • 1,982
  • 4
  • 25
  • 35
13
votes
4 answers

What happens to FILE pointer after file is closed?

I wish to know what happens to FILE pointer after the file is closed. Will it be NULL? Basically, I want to check if a file has already been closed before closing a file. For example as follows: FILE *f; if(f!=NULL) { fclose(f); } Can I do this…
CuriousCoder
  • 1,582
  • 5
  • 28
  • 55
12
votes
5 answers

fclose() causes Segmentation Fault

I've been trying simple file handling in C and I wanted to make sure that the file can be accessed tried using this #include main() { CheckFile(); } int CheckFile() { int checkfile=0; FILE *fp1; fp1 =…
user3437503
  • 159
  • 1
  • 1
  • 4
11
votes
1 answer

PHP can't unlink file after fclose

After my script finishes I can delete the file, but while it's running I can't touch it, even after an fclose(). Here is the code I'm trying to use: $Files = glob("$_SERVER[DOCUMENT_ROOT]/files/*.csv"); $File = fopen($Files[0], "r"); …
Brian Leishman
  • 8,155
  • 11
  • 57
  • 93
10
votes
1 answer

ipad app exited abnormally with signal 11: Segmentation fault: 11

My app exited abnormally with signal 11. I don't know what that means. There is no crash log and the debugger shows no error. The app is just gone. I got the following log. Apr 27 21:31:31 unknown Apollo[1408] : bring tab…
Slavik
  • 1,053
  • 1
  • 13
  • 26
10
votes
1 answer

Fclose a file that is already fclose

In my programm I may close a file that is already close. What happen when I do a fclose on a file already close ? And if you can't do so, how to know if a file is closed or open ?
Evans Belloeil
  • 2,413
  • 7
  • 43
  • 76
9
votes
2 answers

If fclose() fails, is the file descriptor still open?

Imagine the following code running as a thread: void *thread_worker(void *q) { for (;;) { int fd = some_queue_get(q); FILE *writer = fdopen(fd, "w"); if (!writer) { perror("fdopen"; close(fd); continue; } // do something with writer if…
thejonny
  • 488
  • 2
  • 9
9
votes
2 answers

Why would fclose hang / deadlock? (Windows)

I have a directory change monitor process that reads updates from files within a set of directories. I have another process that performs small writes to a lot of files to those directories (test program). Figure about 100 directories with 10 files…
Haw-Bin
  • 416
  • 3
  • 8
8
votes
3 answers

Multiple file descriptors to the same file, C

I have a multithreaded application that is opening and reading the same file (not writing). I am opening a different file descriptor for each thread (but they all point to the same file). Each thread then reads the file and may close it and open it…
Gigi
  • 271
  • 1
  • 4
  • 5
8
votes
2 answers

Exists a way to free memory in atexit or similar without using global variables?

I am developing a project in C, and I need to free the allocated memory and also close all the open files before it exits. I decided to implement a clean function that will do all this stuff and call it with atexit because there are a lot of…
sir psycho sexy
  • 780
  • 7
  • 19
7
votes
2 answers

Proper error handling for fclose impossible (according to manpage)?

So I'm studying fclose manpage for quite I while and my conclusion is that if fclose is interrupted by some signal, according to the manpage there is no way to recover...? Am I missing some point? Usually, with unbuffered POSIX functions (open,…
boo-hoo
  • 123
  • 1
  • 9
1
2 3
18 19