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
275
votes
11 answers

C fopen vs open

Is there any reason (other than syntactic ones) that you'd want to use FILE *fdopen(int fd, const char *mode); or FILE *fopen(const char *path, const char *mode); instead of int open(const char *pathname, int flags, mode_t mode); when using C…
LJM
  • 6,284
  • 7
  • 28
  • 30
228
votes
11 answers

PHP - Failed to open stream : No such file or directory

In PHP scripts, whether calling include(), require(), fopen(), or their derivatives such as include_once, require_once, or even, move_uploaded_file(), one often runs into an error or warning: Failed to open stream : No such file or…
Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
129
votes
4 answers

Writing a new line to file in PHP (line feed)

My code: $i = 0; $file = fopen('ids.txt', 'w'); foreach ($gemList as $gem) { fwrite($file, $gem->getAttribute('id') . '\n'); $gemIDs[$i] = $gem->getAttribute('id'); $i++; } fclose($file); For some reason, it's writing \n as a string, so…
VIVA LA NWO
  • 3,852
  • 6
  • 24
  • 21
90
votes
7 answers

Difference between r+ and w+ in fopen()

In fopen("myfile", "r+") what is the difference between the "r+" and "w+" open mode? I read this: "r" Open a text file for reading. "w" Open a text file for writing, truncating an an existing file to zero length, or creating the file if it does…
yaylitzis
  • 5,354
  • 17
  • 62
  • 107
81
votes
4 answers

What happens if I don't call fclose() in a C program?

Firstly, I'm aware that opening a file with fopen() and not closing it is horribly irresponsible, and bad form. This is just sheer curiosity, so please humour me :) I know that if a C program opens a bunch of files and never closes any of them,…
electrodruid
  • 1,083
  • 2
  • 9
  • 13
74
votes
11 answers

fopen deprecated warning

With the Visual Studio 2005 C++ compiler, I get the following warning when my code uses the fopen() and such calls: 1>foo.cpp(5) : warning C4996: 'fopen' was declared deprecated 1> c:\program files\microsoft visual studio…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
73
votes
9 answers

Reading from a frequently updated file

I'm currently writing a program in python on a Linux system. The objective is to read a log file and execute a bash command upon finding a particular string. The log file is being constantly written to by another program. My question: If I open the…
JimS
  • 1,123
  • 3
  • 14
  • 17
61
votes
1 answer

What is the difference between fopen modes "r+" and "rw+" in PHP?

Many answers here in Stack Overflow use fopen($file, "rw+"), but the manual doesn't list the "rw+" mode, there's only the "r+" mode (or "w+" mode). So I was wondering, what does the "rw+" mode do? What's the difference between fopen($file, "rw+" or…
flen
  • 1,905
  • 1
  • 21
  • 44
48
votes
2 answers

Create a file if one doesn't exist - C

I want my program to open a file if it exists, or else create the file. I'm trying the following code but I'm getting a debug assertion at freopen.c. Would I be better off using fclose and then fopen immediately afterward? FILE *fptr; fptr =…
karoma
  • 1,548
  • 5
  • 24
  • 43
48
votes
6 answers

PHP check if file contains a string

I'm trying to see if a file contains a string that is sent to the page. I'm not sure what is wrong with this code: ?php $valid = FALSE; $id = $_GET['id']; $file = './uuids.txt'; $handle = fopen($file, "r"); if ($handle) { //…
WildBill
  • 9,143
  • 15
  • 63
  • 87
42
votes
4 answers

Can't write to /tmp with php, despite 777 permissions and no open_basedir value

I'm trying to write a file to my /tmp directory (on an apache server) with the php fopen function, but it fails:
36
votes
3 answers

PHP: fopen to create folders

I need to know if there is any way to create new folder if the path doesn't exist. When I try to fopen() a path, it says NO such File or Directory exists I tried to open the file using 'w' and 'w+' but it is not able to create new folder. Is there…
Vivek
  • 4,526
  • 17
  • 56
  • 69
35
votes
20 answers

How to read only 5 last line of the text file in PHP?

I have a file named file.txt which is update by adding lines to it. I am reading it by this code: $fp = fopen("file.txt", "r"); $data = ""; while(!feof($fp)) { $data .= fgets($fp, 4096); } echo $data; and a huge number of lines appears. I just…
Alireza
  • 5,444
  • 9
  • 38
  • 50
35
votes
4 answers

PHP: fopen error handling

I do fetch a file with $fp = fopen('uploads/Team/img/'.$team_id.'.png', "rb"); $str = stream_get_contents($fp); fclose($fp); and then the method gives it back as image. But when fopen() fails, because the file did not exists, it throws an…
Fabian
  • 1,806
  • 5
  • 25
  • 40
35
votes
2 answers

Difference between file, file_get_contents, and fopen in PHP

I am new to PHP, and I am not quite sure: what is the difference between the file(), file_get_contents(), and fopen() functions, and when should I use one over the other?
c00L
  • 599
  • 1
  • 5
  • 17
1
2 3
99 100