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
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
  • 9,675
  • 17
  • 51
  • 98
12
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,822
  • 7
  • 23
  • 40
12
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…
dakis
  • 225
  • 1
  • 6
  • 32
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,352
  • 11
  • 54
  • 104
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,349
  • 7
  • 45
  • 59
11
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
  • 149
  • 1
  • 1
  • 4
11
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
  • 163
  • 1
  • 1
  • 6
11
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,647
  • 5
  • 20
  • 34
10
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
  • 125
  • 1
  • 5
10
votes
3 answers

C - Writing structs to a file (.pcap)

I am trying to write a .pcap file, which is something that can be used in Wireshark. In order to do that, I have a couple of structs with various data types I need to write to a file. (see code) So, I create the struct instances, fill in the data,…
KaiserJohaan
  • 8,578
  • 18
  • 105
  • 187
10
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
  • 15,843
  • 24
  • 83
  • 128
10
votes
3 answers

unbuffered I/O in Linux

I'm writing lots and lots of data that will not be read again for weeks - as my program runs the amount of free memory on the machine (displayed with 'free' or 'top') drops very quickly, the amount of memory my app uses does not increase - neither…
stuck
  • 2,096
  • 2
  • 25
  • 54
10
votes
5 answers

PHP5 giving failed to open stream: HTTP request failed error when using fopen

This problem seems to have been discussed in the past everywhere on google and here, but I have yet to find a solution. A very simple fopen gives me a PHP Warning: fopen(http://www.google.ca): failed to open stream: HTTP request failed!". The…
mickey
  • 195
  • 2
  • 2
  • 8
10
votes
1 answer

PHP can't unlink file after fclose

After my script finishes I can delete the file, but while it's running I can't touch it, even after an fclose(). Here is the code I'm trying to use: $Files = glob("$_SERVER[DOCUMENT_ROOT]/files/*.csv"); $File = fopen($Files[0], "r"); …
Brian Leishman
  • 6,574
  • 7
  • 47
  • 82
10
votes
4 answers

In Windows, should I use CreateFile or fopen, portability aside?

What are the differences, and in what cases one or the other would prove superior in some way?
CannibalSmith
  • 4,522
  • 9
  • 42
  • 52