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.

2727 questions
18
votes
3 answers

Is there a standard way to do an fopen with a unicode string file path?

Is there a standard way to do an fopen with a unicode string file path?
Brian R. Bondy
  • 314,085
  • 114
  • 576
  • 619
18
votes
7 answers

Overwrite Line in File with PHP

What is the best way to overwrite a specific line in a file? I basically want to search a file for the string '@parsethis' and overwrite the rest of that line with something else.
Wilco
  • 31,070
  • 47
  • 124
  • 158
18
votes
6 answers

To create all needed folders with fopen

I'm using fopen("aaa\bbb\ccc\myfile.txt","w") to create output file. But folders aaa, bbb and ccc does't exists. What is the best way to solve this problem? Is there are any standard way to ask fopen to create all needed folders?
vico
  • 13,725
  • 30
  • 108
  • 237
18
votes
5 answers

_wfopen equivalent under Mac OS X

I'm looking to the equivalent of Windows _wfopen() under Mac OS X. Any idea? I need this in order to port a Windows library that uses wchar* for its File interface. As this is intended to be a cross-platform library, I am unable to rely on how the…
Vincent Robert
  • 33,284
  • 13
  • 77
  • 115
17
votes
3 answers

SplFileObject vs fopen in PHP

What are the pros and cons of using fopen as opposed to SplFileObject in PHP? From what I see, SplFileObject throws exceptions where applicable which makes this convenient when using try...catch for error handling. Apart from this, are there any…
Agnel Kurian
  • 53,593
  • 39
  • 135
  • 210
17
votes
2 answers

fseek vs rewind?

I have noticed two methods to return to the beginning of a file FILE *fp = fopen("test.bin", "r") fseek(fp, 0, SEEK_END); rewind(fp); and FILE *fp = fopen("test.bin", "r") fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_SET); What would be difference…
Steven Penny
  • 82,115
  • 47
  • 308
  • 348
15
votes
6 answers

Can multiple processes append to a file using fopen without any concurrency problems?

I have a process opening a file in append mode. In this case it is a log file. Sample code: int main(int argc, char **argv) { FILE *f; f = fopen("log.txt", "a"); fprintf(f, "log entry line"); fclose(f); } Two questions: If I have…
Deleted
  • 4,368
  • 1
  • 18
  • 17
15
votes
5 answers

how to get file using php native functions and Android

I did a function in private section of my website, to get and display some file of a repository. Below is the function I did : function getFilesChantier($devis, $cp) { // Si dossier cp n'existe pas on le créé if…
Stanislas Piotrowski
  • 2,294
  • 10
  • 35
  • 57
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
14
votes
2 answers

SQLite VFS implementation guide lines with FOpen*

I am about to implement a custom VFS (virtual file system) for a Netburner embedded device (non windows) using FOpen, FRead, FWrite, FSeek, and FClose. I was surprised that i could not find a FOpen* version of the VFS available. It would make it a…
Steven Smethurst
  • 4,135
  • 15
  • 50
  • 84
14
votes
6 answers

What encoding used when invoke fopen or open?

When we invoke system call in linux like 'open' or stdio function like 'fopen' we must provide a 'const char * filename'. My question is what is the encoding used here? It's utf-8 or ascii or iso8859-x? Does it depend on the system or environment…
xeranic
  • 1,293
  • 1
  • 9
  • 16
13
votes
3 answers

php://input - what does it do in fopen()?

$handle = fopen("/home/rasmus/file.txt", "r"); $handle = fopen("/home/rasmus/file.gif", "wb"); I can understand this that /home/rasmus/file.txt and /home/rasmus/file.gif are the file path. But what these mean: php://input php://temp in…
laukok
  • 47,545
  • 146
  • 388
  • 689
13
votes
3 answers

Does fseek() move the file pointer to the beginning of the file if it was opened in "a+b" mode?

I wish to open a file using the "a+b" mode, i.e. if it does not exist it is created automatically, but if it does I don't want to overwrite it. I want to be able to read and write to the file. The file is binary, and I want to save records of a…
jbx
  • 18,832
  • 14
  • 73
  • 129
13
votes
7 answers

How do I open a file from line X to line Y in PHP?

The closest I've seen in the PHP docs, is to fread() a given length, but that doesnt specify which line to start from. Any other suggestions?
lock
  • 5,502
  • 16
  • 56
  • 75
13
votes
2 answers

What are the biggest differences between fopen and curl?

I am making a web application in PHP and want to read content from another domain. It seems that my major options are fopen and curl. What are the major differences between these two methods, especially regarding security and available options? Does…
Marco
  • 2,208
  • 2
  • 25
  • 43