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
13
votes
1 answer

fopen Creates File, But how to change permissions?

I am creating a new file using fopen. $filename = 'user_data/10.xml'; $openhandle = fopen($filename, 'w+'); Then I check if the file has been created using: file_exists() function. The problem is: The file is being created with some owner, probably…
Arjun Bajaj
  • 1,932
  • 7
  • 24
  • 41
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…
Run
  • 54,938
  • 169
  • 450
  • 748
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
  • 21,365
  • 18
  • 90
  • 144
13
votes
6 answers

Open file with fopen, given absolute path on Windows

I'm trying to make a program that counts the number of lines of a file, when I try to pass the absolute path to the fopen function, is simply tells me that is not found, here is my code: #include #include #include…
franvergara66
  • 10,524
  • 20
  • 59
  • 101
12
votes
4 answers

php/iis: failed to open stream: Permission denied

I am trying to do this: $fh = fopen("req2" , 'w') and I get: Warning: fopen(D:\inetpub\wwwroot\req2) [function.fopen]: failed to open stream: Permission denied in D:\inetpub\wwwroot\test.php on line 44 can't open file I gave "Full Control"…
hoshiKuzu
  • 866
  • 1
  • 12
  • 30
12
votes
3 answers

C++ Make a file of a specific size

Here is my current problem: I am trying to create a file of x MB in C++. The user will enter in the file name then enter in a number between 5 and 10 for the size of the file they want created. Later on in this project i'm gonna do other things with…
OmegaTwig
  • 243
  • 1
  • 4
  • 15
12
votes
5 answers

fclose() causes Segmentation Fault

I've been trying simple file handling in C and I wanted to make sure that the file can be accessed tried using this #include main() { CheckFile(); } int CheckFile() { int checkfile=0; FILE *fp1; fp1 =…
user3437503
  • 159
  • 1
  • 1
  • 4
12
votes
8 answers

Unable to open a file with fopen()

I've been trying to open a file and output text, but I keep getting errors. So I thought I would start at the very beginning and just try opening the file. This is my code: #include #include #define CORRECT_PARAMETERS 3 int…
jet
  • 173
  • 1
  • 1
  • 6
12
votes
1 answer

C open file multiple times

I have a file opened with fopen. There is a way to reopen the same file (while it is opened) but have a different seek? (so i can use fread independently)
polslinux
  • 1,739
  • 9
  • 34
  • 73
12
votes
1 answer

boost::filesystem::path and fopen()

I get error when I try to do this: path p = "somepath"; FILE* file = fopen(p.c_str(), "r"); I get: argument of type "const boost::filesystem::path::value_type *" is incompatible with parameter of type "const char *" Could anyone tell me what I'm…
Martin
  • 1,877
  • 5
  • 21
  • 37
11
votes
4 answers

what is the reason for fopen's failure to open a file

I have the following code where I am trying to open a text file. char frd[32]="word-list.txt"; FILE *rd=fopen(frd,"rb"); if(!rd) std::cout<<"Coudn't open file\t"<
John
  • 794
  • 2
  • 18
  • 34
11
votes
2 answers

faster fopen or file_get_contents?

i am running multiple websites with high traffic , as a requirement , all images are downloaded via image.php?id=IMAGE_ID_HERE . If you ever done that before , you know that that file will be reading the file image and echoing it to the browser…
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
11
votes
3 answers

What happens if you exit a program without doing fclose()?

Question: What happens if I exit program without closing files? Are there some bad things happening (e.g. some OS level file descriptor array is not freed up..?) And to the answer the same in both cases programmed exiting unexpected crash Code…
mks
  • 135
  • 1
  • 5
11
votes
4 answers

Sqlite as a replacement for fopen()?

On an official sqlite3 web page there is written that I should think about sqlite as a replacement of fopen() function. What do you think about it? Is it always good solution to replece application internal data storage with sqlite? What are the…
klew
  • 14,837
  • 7
  • 47
  • 59
11
votes
5 answers

what does the error message 'Operation now in progress' mean?

When trying to open a file using this command: $fd = fopen('majestic_files/majestic_record.txt','w'); I get the following error message: Warning: fopen(majestic_files/majestic_record.txt) [function.fopen]:…
Benubird
  • 18,551
  • 27
  • 90
  • 141