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
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
  • 1,261
  • 1
  • 12
  • 15
7
votes
2 answers

fclose(): 18 is not a valid stream resource

I am trying to execute a process using proc_open. The I/O for the process is handled by the pipes !! $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w") ); Now, as it happens, sometimes the…
Kapil Kaushik
  • 1,536
  • 4
  • 21
  • 28
6
votes
2 answers

Error handling in file opening

[Question 1] When I open a file into a function, generally I do something like this: int read_file (char *filename) { FILE *fin; if ( !(fin = fopen(filename, "r")) ) return 1; /* ... */ return fclose(fin); } int main…
ᴜsᴇʀ
  • 1,109
  • 2
  • 9
  • 23
6
votes
5 answers

Check if a file that has been opened with fopen has been closed

gcc (GCC) 4.7.2 c89 Is it possible for check if a file has already closed? I have opened a file using fopen() and closed using fclose(fd). This file gets opened and closed during the running of the program. However, the user can terminate the…
ant2009
  • 27,094
  • 154
  • 411
  • 609
5
votes
3 answers

Does fclose release the file immediately?

I have 2 functions. The first function is opening a file in write mode and writing some contents to it and then closing it. FILE *fp = fopen("file.txt", "w"); //writing itnot file using fwrite fclose(fp); The second function opens the file in read…
Arun
  • 2,247
  • 3
  • 28
  • 51
5
votes
4 answers

Writing to both terminal and file c++

I found this question answered for Python, Java, Linux script, but not C++: I'd like to write all outputs of my C++ program to both the terminal and an output file. Using something like this: int main () { freopen ("myfile.txt","w",stdout); cout<<…
Aly
  • 383
  • 1
  • 4
  • 14
5
votes
3 answers

fclose works differently on android and linux

Following program: #include #include #include #include int main() { fclose( stderr ); printf( "%d\n", fileno( stderr ) ); return 0; } shows -1 on ubuntu 11.04 and 2 on ICS 4.0.3 emulator. Can't…
Andrey Starodubtsev
  • 5,139
  • 3
  • 32
  • 46
4
votes
3 answers

fwrite if file doesn't exist?

Is it possible to only write a file in PHP if it doesn't exist? $file = fopen("test.txt","w"); echo fwrite($file,"Some Code Here"); fclose($file); So if the file does exist the code won't write the code but if the file doesn't exist it will create…
Josh
  • 2,835
  • 1
  • 21
  • 33
4
votes
2 answers

why does PHP fopen + fwrite double document?

I am running PHP Version 5.3.4 with Apache/2.2.17 on Windows 7 Ultimate 32bit (IIS disabled). I been looking at the fopen modes and am well aware of what mode does what, but i can't wrap my finger around why the double posting to the txt file with a…
robx
  • 3,093
  • 2
  • 26
  • 29
4
votes
1 answer

Should a file be closed before removing?

I have a file pointer and I want to remove the file after loading the content of the file in a data structure. I'm not sure whether the file should be closed before removing. Which part of the code is correct? FILE* myFile = fopen("abc.bin",…
user4869409
4
votes
5 answers

close file with fclose() but file still in use

I've got a problem with deleting/overwriting a file using my program which is also being used(read) by my program. The problem seems to be that because of the fact my program is reading data from the file (output.txt) it puts the file in a 'in use'…
Marco
  • 41
  • 1
  • 1
  • 4
4
votes
3 answers

Can fwrite & fclose be called parallely from two threads for the same file descriptor?

What will happen if fwrite & fclose are called in parallel from two threads for the same file descriptor?
Jay
  • 24,173
  • 25
  • 93
  • 141
4
votes
1 answer

Wordpress not writing to folder using fwrite();

I have some php script that writes an html file to a folder. This works perfectly outside of WordPress. But when I try to incorporate my code into a plugin, it won't write to a folder anymore. I've even tried writing it to the original folder I…
Tyler Robinson
  • 371
  • 5
  • 16
3
votes
1 answer

why fclose() is not always flushing the data to the disk?

I'm getting some weird results, while trying to write data to files in C.
I thought that fclose() closes the *FILE and flushes the data from its buffer to the file.
But for some reason it only flushes the data in my program sometimes and…
piet_lu
  • 77
  • 3
3
votes
4 answers

How to remove a file in C program?

How do I close a file and remove it? I have the following code: FILE *filePtr = fopen("fileName", "w"); ... Now I want to close filePtr and remove the file "fileName". Should…
loopi
  • 31
  • 1
  • 1
  • 2
1
2
3
18 19