Questions tagged [fopen]

fopen opens a file resource, in order to read, write or append content to it.

fopen opens a file resource, in order to read, write or append content to it. It originated in the C standard library, but has been ported to other languages (and sometimes renamed simply to open).

In the C standard library, fopen resides in stdio.h, with the signature FILE *fopen(const char *path, const char *mode);.

For example:

FILE *file_pointer = fopen("filename","r");

Generally, there are 3 modes:

  • r, which opens a file for reading
  • w, which opens a file for writing (clearing it in the process)
  • a, which opens a file for appending to the end
  • There are also a selection of "hybrid" modes (r+, w+, a+) whose names vary by language

To close an open file, see fclose.

2898 questions
11
votes
3 answers

unbuffered I/O in Linux

I'm writing lots and lots of data that will not be read again for weeks - as my program runs the amount of free memory on the machine (displayed with 'free' or 'top') drops very quickly, the amount of memory my app uses does not increase - neither…
stuck
  • 2,264
  • 2
  • 28
  • 62
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
11
votes
5 answers

C fopen fails for write with errno is 2

I do not understand why this is seemingly failing with errno of 2: char debugText [256]; sprintf (debugText, "C:\\List.txt"); dfile = fopen( debugText, "w"); fprintf ( dfile, " err %d \n", errno); I say seemingly because while dfile is NULL the…
JPM
  • 445
  • 1
  • 5
  • 15
10
votes
1 answer

Using fopen() in Objective-C

I am puzzled by a crash I keep getting due to an error at this section of code: FILE *fid200; fid200 = fopen ( "Length200Vector.txt" , "w" ); if (fid200 == NULL) perror("Error…
Kevin_TA
  • 4,575
  • 13
  • 48
  • 77
10
votes
3 answers

C - Writing structs to a file (.pcap)

I am trying to write a .pcap file, which is something that can be used in Wireshark. In order to do that, I have a couple of structs with various data types I need to write to a file. (see code) So, I create the struct instances, fill in the data,…
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199
10
votes
5 answers

PHP5 giving failed to open stream: HTTP request failed error when using fopen

This problem seems to have been discussed in the past everywhere on google and here, but I have yet to find a solution. A very simple fopen gives me a PHP Warning: fopen(http://www.google.ca): failed to open stream: HTTP request failed!". The…
mickey
  • 185
  • 2
  • 2
  • 8
10
votes
3 answers

Opening a file in 'a+ 'mode

If a file is opened using the following command: FILE *f1=fopen("test.dat","a+"); The man page reads: a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file…
user191776
10
votes
4 answers

In Windows, should I use CreateFile or fopen, portability aside?

What are the differences, and in what cases one or the other would prove superior in some way?
CannibalSmith
  • 4,742
  • 10
  • 44
  • 52
10
votes
3 answers

C, check if a file exists without being able to read/write possible?

Possible Duplicate: What’s the best way to check if a file exists in C? (cross platform) i would like to check if a file exists or not. I changed the permissions of my testfile to "chmod -r somefile". However now it says that the file does not…
Susan
  • 301
  • 1
  • 5
  • 17
10
votes
3 answers

How to output the reason for a PHP file open failure

I'm trying to debug a huge, antiquated (circa 2001) PHP web service and I'm encountering file open failures. The fopen call is in an included module, the caller is logging that the file could not be opened but no reason is being logged. The code…
user24989
  • 445
  • 3
  • 8
  • 11
10
votes
1 answer

PHP: fopen: No such file or directory

I am trying to create write a log file for my web site. To do this I use the following code to try and open the file. Now the file does not exist yet, but the documentation states that adding "a+" flag ensures that the file is created if it does not…
Heshan Perera
  • 4,592
  • 8
  • 44
  • 57
9
votes
1 answer

Does fopen create a file descriptor?

Looking at the man page for fopen I cannot get a definite answer to this question. FILE *fopen(const char *path, const char *mode); I understand that fopen returns a file pointer to a stream but is a file descriptor created as a byproduct? I am…
Jon Kump
  • 574
  • 7
  • 19
9
votes
4 answers

fopen does not return

I used 'fopen' in a C program to open a file in readonly mode (r). But in my case I observed that fopen call does not return. It does not return NULL or valid pointer - execution gets blocked at fopen call. The patch of file is absolutely correct (I…
Souvik
  • 601
  • 1
  • 8
  • 16
9
votes
3 answers

How to enable large file support under Darwin?

I have a C application I am trying to compile for Mac OS X 10.6.4: $ uname -v Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 My gcc is as follows: $ gcc --version i686-apple-darwin10-gcc-4.2.1 (GCC)…
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
9
votes
5 answers

Is there a reason fopen() wouldn't work after several hundred opens?

Hey, for this piece of code, the person who wrote the system communicates data between processes using textfiles. I have a loops that looks (for all intents and purposes) like this: while (true) { //get the most up-to-date info from the other…
Paul
  • 93
  • 1
  • 4