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
235
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
  • 5,654
  • 6
  • 25
  • 27
189
votes
7 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,705
  • 6
  • 40
  • 61
118
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,604
  • 6
  • 21
  • 21
73
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
  • 4,558
  • 13
  • 54
  • 90
71
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
  • 943
  • 2
  • 8
  • 12
70
votes
10 answers

fopen deprecated warning

On 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 8\vc\include\stdio.h(234) :…
Ashwin Nanjappa
  • 68,458
  • 72
  • 198
  • 283
62
votes
8 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
  • 973
  • 3
  • 13
  • 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,441
  • 1
  • 14
  • 39
45
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,488
  • 4
  • 25
  • 43
38
votes
5 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
  • 7,965
  • 14
  • 54
  • 85
37
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:
34
votes
6 answers

PHP php_network_getaddresses: getaddrinfo failed: No such host is known

I am having DNS issues with a certain target domain. I am using fopen() (but same issue with other functions) to retreive an image, but I get this error: Warning: fopen(): php_network_getaddresses: getaddrinfo failed: No such host is known I am…
Richard
  • 4,038
  • 5
  • 30
  • 50
33
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,204
  • 15
  • 54
  • 65
32
votes
18 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
  • 4,901
  • 9
  • 34
  • 48
30
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,706
  • 5
  • 21
  • 38
1
2 3
99 100