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.

266 questions
71
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
  • 943
  • 2
  • 8
  • 12
45
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,488
  • 4
  • 25
  • 43
23
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
  • 341
  • 1
  • 2
  • 8
15
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
  • 15,563
  • 30
  • 91
  • 184
14
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,862
  • 3
  • 23
  • 34
12
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,521
  • 4
  • 27
  • 52
11
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
  • 149
  • 1
  • 1
  • 4
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
  • 913
  • 1
  • 10
  • 26
10
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
  • 6,574
  • 7
  • 47
  • 82
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
  • 388
  • 1
  • 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
  • 406
  • 3
  • 8
8
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,233
  • 6
  • 37
  • 68
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
  • 93
  • 7
7
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
  • 241
  • 1
  • 4
  • 5
7
votes
3 answers

use fclose to pipe of popen is a serious bug?

Some months ago I write a CGI application for Linux that uses popen() to read the output of a command, and then I close the pipe with fclose(). Now, I read that for close pipes is needs use pclose(). The manual says: The return value from popen()…
carlos
  • 973
  • 1
  • 8
  • 14
1
2 3
17 18