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
35
votes
2 answers

Getting an error "fopen': This function or variable may be unsafe." when compling

I'm receiving this error when compiling: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. I'm new to C++ and open CV, therefore please help me to get rid of this…
SeverusSwan
  • 361
  • 1
  • 3
  • 4
33
votes
8 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,341
  • 5
  • 35
  • 55
31
votes
4 answers

Line break not working when writing to text file in PHP

I have the following test script:
JM4
  • 6,740
  • 18
  • 77
  • 125
28
votes
2 answers

Reading a text file - fopen vs. ifstream

Googling file input I found two ways to input text from a file - fopen and ifstream. Below are the two snippets. I have a text file consisting of one line with an integer I need to read in. Should I use fopen or ifstream? SNIPPET 1 - FOPEN FILE *…
user656925
28
votes
4 answers

Detailed error on fopen

I'm using fopen to read from a file $fh = fopen($path, 'r') or die('Could not open file'); Now I contantly get error Could not open file. I checked the file path and even changed the permissions of the file to 777. Is there a way I can get a…
Elitmiar
  • 35,072
  • 73
  • 180
  • 229
27
votes
8 answers

fopen(); "Remote host file access not accepted" on a local file?

I am using the Tcpdf module and PHP to create dymanic PDF invoices from an ordering system. The script should then save the invoice into a folder called "invoices". The folder exists, and there are full permissions for "everyone" (Windows). The code…
user2924019
  • 1,983
  • 4
  • 29
  • 49
26
votes
2 answers

PHP fopen() Error: failed to open stream: Permission denied

I learning how to write a WordPress plugin. I need some help writing some data to an XML file. I'm on my local machine, a Mac running MAMP. I have PHP 5.2.13. In my plugin, I've got: $file_handle = fopen('markers.xml', 'w'); $stringdata = "Test…
Laxmidi
  • 2,650
  • 12
  • 49
  • 81
25
votes
8 answers

file_get_contents returns empty string

I am hesitated to ask this question because it looks weird. But anyway. Just in case someone had encountered the same problem already... filesystem functions (fopem, file, file_get_contents) behave very strange for http:// wrapper it seemingly…
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
25
votes
6 answers

fopen / fopen_s and writing to files

I'm using fopen in C to write the output to a text file. The function declaration is (where ARRAY_SIZE has been defined earlier): void create_out_file(char file_name[],long double *z1){ FILE *out; int i; if((out = fopen(file_name,…
yCalleecharan
  • 4,656
  • 11
  • 56
  • 86
23
votes
7 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 don't exist. What is the best way to solve this problem? Is there are any standard way to ask fopen() to create all needed folders?
vico
  • 17,051
  • 45
  • 159
  • 315
23
votes
6 answers

Is there a way to use fopen_s() with GCC or at least create a #define about it?

MSVC compiler says that fopen() is deprecated, and recommends the use of fopen_s(). Is there any way to use fopen_s() and still be portable? Any ideas for a #define?
Shantia
  • 1,129
  • 2
  • 9
  • 9
22
votes
8 answers

Why does 'fopen' return a NULL pointer?

I'm working on a simple file splitter/merger program in the C programming language. The problem is, for some reason fopen returns NULL, and because of that, my program is crashing at the fwrite statement. How do I fix this? Here is the C file: int…
k787
  • 397
  • 2
  • 5
  • 11
22
votes
2 answers

How to check if a PHP stream resource is readable or writable?

In PHP, how do I check if a stream resource (or file pointer, handle, or whatever you want to call them) is either readable or writable? For example, if you're faced with a situation where you know nothing about how the resource was opened or…
jnrbsn
  • 2,498
  • 1
  • 18
  • 25
22
votes
4 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
  • 339,232
  • 124
  • 596
  • 636
20
votes
7 answers

fopen() returning a NULL pointer, but the file definitely exists

The code I have is as follows: FILE *txt_file = fopen("data.txt", "r"); if (txt_file == NULL) { perror("Can't open file"); } The error message returned is: Can't open file: No such file or directory The file 'data.txt' definitely exists in…
Barjavel
  • 1,626
  • 3
  • 19
  • 31