fclose

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

馋奶兔 提交于 2019-12-28 04:01:36
问题 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 fclose() ) to the stream results in undefined behavior. ERRORS EBADF The file descriptor underlying fp is not valid. The fclose() function may also fail and set errno for any of the errors specified for the routines close(2) , write(2) or fflush(3) . Of course

Writing to both terminal and file c++

妖精的绣舞 提交于 2019-12-23 13:07:20
问题 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<< "Let's try this"; fclose (stdout); return 0; } outputs it to only the output file named "myfile.txt", and prevents it from showing on the terminal. How can I make it output to both simultaneously? I use visual studio 2010 express (if that would make

use fclose to pipe of popen is a serious bug?

一笑奈何 提交于 2019-12-23 09:07:13
问题 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() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(3) . My code is like this: if ((NULL != (f = popen(command.value, "r")))) { //do something fclose(f); } My question is: My mistake have a

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

我的未来我决定 提交于 2019-12-22 07:57:43
问题 What will happen if fwrite & fclose are called in parallel from two threads for the same file descriptor? 回答1: fwrite and fclose operate on the FILE data structure. Since this is a largish data structure that does store more than just the file descriptor, the answer is bad things. Don't do this unless you ensure atomic operation using mutexes. 回答2: POSIX requires FILE access to be thread-safe, but since fclose closes the file and invalidates the pointer, there is no way (i.e. it's not just a

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

女生的网名这么多〃 提交于 2019-12-21 09:22:06
问题 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] <Warning>: bring tab <VUIWebController: 0x2dcf30> to front Apr 27 21:31:31 unknown configd[29] <Notice>: jetsam: kernel termination snapshot being created Apr 27 21:31:32 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:com.apple.mobilemail[0x81d8]) Exited: Killed: 9 Apr 27 21

Is it possible to rescue file descriptor from FILE*?

陌路散爱 提交于 2019-12-20 04:24:34
问题 I have to use a certain cross-platform library which passes FILE* objects around. I get a file descriptor from another source (inherited), I want to keep same fd across fork 'd processes. I currently use fdopen to convert a file descriptor to a FILE* object. My problem is that fclose used to clean up FILE* objects closes connected file descriptor. I would very much like to keep this file descriptor after it has been used. is there a way rescue file descriptor from FILE* ? Is there a way to

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

你。 提交于 2019-12-19 08:03:58
问题 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 possible exit scenarios. The problem is that atexit doesn't allow me to set functions with parameters, so I can't send to clean the pointers that need to be freed in the end of the process. So I need to declare as global variables every pointer that may

removing a file in c

我的梦境 提交于 2019-12-18 09:03:08
问题 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 I: fclose(filePtr); remove("fileName"); Or: remove("fileName"); fclose(filePtr); Does it matter which I do first? Thanks!! 回答1: That is OS-dependent. On *nix, deleting an open file leaves it open and the data on disk, but removes the filename from the filesystem, and actually deletes the file on close; some other

C - Working with fopen, fclose, fputc etc

女生的网名这么多〃 提交于 2019-12-17 17:15:24
问题 I've got this code finally working with a single argument on my command line, i.e. one file for it to work with, although I designed the code with the concept of it working with an unlimited number of files. What it does is take some X number of text files containing words separated by spaces, and replaces spaces with \n thus creating a list of words. Though, it successfully completes the first argument, it just ignores the 2nd. Another minor problem seems that it also prints out some garbage

What happens to FILE pointer after file is closed?

末鹿安然 提交于 2019-12-17 16:27:56
问题 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 or is there any other way to go about it? 回答1: Since arguments are passed by value there is not way fclose could set your file pointer to NULL . Since fclose probably destroys the FILE you have to Manually set the file pointer to NULL after doing a