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
20
votes
3 answers

Write and read php object in a text file?

I want to write a php object in a text file. The php object is like that $obj = new stdClass(); $obj->name = "My Name"; $obj->birthdate = "YYYY-MM-DD"; $obj->position = "My position"; I want to write this $obj in a text file. The text file is…
Nantu
  • 557
  • 1
  • 4
  • 16
19
votes
9 answers

Why does fopen take a string as its second argument?

It has always struck me as strange that the C function fopen() takes a const char * as the second argument. I would think it would be easier to both read your code and implement the library if there were bit masks defined in stdio.h, like IO_READ…
Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
19
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…
Zombo
  • 1
  • 62
  • 391
  • 407
18
votes
4 answers

CakePHP 2.0 - Cake was unable to write to File cache

I'm using CakePHP 2.0 RC-1. After checking out the project from SVN, the application is starting to complain that it can't write cache files to the tmp/cache directory. Since this is local, I know the directory is writeable and I can CLEARLY see…
Coreus
  • 5,360
  • 3
  • 35
  • 50
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
  • 32,754
  • 49
  • 128
  • 160
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
  • 35,564
  • 14
  • 82
  • 119
17
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,804
  • 1
  • 22
  • 17
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
  • 57,975
  • 43
  • 146
  • 217
16
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,982
  • 4
  • 25
  • 35
16
votes
3 answers

Create a stream from a resource

I know that I can create a PHP stream from a filename (a real one, or an URL), by using the fopen function: $stream = fopen('php://temp', 'r'); The resulting stream ($stream) is then a resource of type "stream", created from the URL php://temp. But…
PajuranCodes
  • 303
  • 3
  • 12
  • 43
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,595
  • 10
  • 40
  • 59
15
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,391
  • 1
  • 9
  • 16
14
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
  • 6,404
  • 18
  • 58
  • 76
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,495
  • 15
  • 55
  • 92
14
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,306
  • 2
  • 26
  • 43