Questions tagged [file-io]

File I/O is input/output that involves the file system. This could include performing operations on directories and files, such as creation and deletion, reading files, and writing output to files.

For an introduction on File I/O in C, see C Programming / File I/O on Wikibooks.

21104 questions
111
votes
22 answers

PHP: How to check if image file exists?

I need to see if a specific image exists on my cdn. I've tried the following and it doesn't work: if (file_exists(http://www.example.com/images/$filename)) { echo "The file exists"; } else { echo "The file does not exist"; } Even if the…
PaperChase
  • 1,557
  • 4
  • 18
  • 23
111
votes
7 answers

How can I read a text file without locking it?

I have a windows service writes its log in a text file in a simple format. Now, I'm going to create a small application to read the service's log and shows both the existing log and the added one as live view. The problem is that the service locks…
Homam
  • 23,263
  • 32
  • 111
  • 187
111
votes
7 answers

What is the rationale for fread/fwrite taking size and count as arguments?

We had a discussion here at work regarding why fread() and fwrite() take a size per member and count and return the number of members read/written rather than just taking a buffer and size. The only use for it we could come up with is if you want to…
David Holm
  • 17,522
  • 8
  • 47
  • 47
111
votes
7 answers

Write objects into file with Node.js

I've searched all over stackoverflow / google for this, but can't seem to figure it out. I'm scraping social media links of a given URL page, and the function returns an object with a list of URLs. When I try to write this data into a different…
sarahbkim
  • 1,133
  • 2
  • 7
  • 6
111
votes
10 answers

How to append new data onto a new line

My code looks like this: def storescores(): hs = open("hst.txt","a") hs.write(name) hs.close() so if I run it and enter "Ryan" then run it again and enter "Bob" the file hst.txt looks like RyanBob instead of Ryan Bob How do I fix…
RyanH2796
  • 1,119
  • 2
  • 7
  • 8
109
votes
9 answers

How do I use Java to read from a file that is actively being written to?

I have an application that writes information to file. This information is used post-execution to determine pass/failure/correctness of the application. I'd like to be able to read the file as it is being written so that I can do these…
Anthony Cramp
  • 4,495
  • 6
  • 26
  • 27
109
votes
7 answers

Read file-contents into a string in C++

Possible Duplicate: What is the best way to slurp a file into a std::string in c++? In scripting languages like Perl, it is possible to read a file into a variable in one shot. open(FILEHANDLE,$file); $content=; What would be…
sonofdelphi
  • 1,986
  • 5
  • 19
  • 25
109
votes
3 answers

C Programming: How to read the whole file contents into a buffer

I want to write the full contents of a file into a buffer. The file actually only contains a string which i need to compare with a string. What would be the most efficient option which is portable even on linux. ENV: Windows
Sunny
  • 7,444
  • 22
  • 63
  • 104
108
votes
11 answers

How to deal with files with a name longer than 259 characters?

I'm working on an application which walks through every file in some directories and does some actions with those files. Among others, I must retrieve the file size and the date when this file was modified. Some file full names (directory + file…
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
107
votes
8 answers

Fastest Way to Serve a File Using PHP

I'm trying to put together a function that receives a file path, identifies what it is, sets the appropriate headers, and serves it just like Apache would. The reason I am doing this is because I need to use PHP to process some information about the…
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177
106
votes
14 answers

Batch Renaming of Files in a Directory

Is there an easy way to rename a group of files already contained in a directory, using Python? Example: I have a directory full of *.doc files and I want to rename them in a consistent way. X.doc -> "new(X).doc" Y.doc -> "new(Y).doc"
Nate
  • 18,892
  • 27
  • 70
  • 93
105
votes
10 answers

How to delete a folder that name ended with a dot (".")?

I got some folders created by malware whose name ended with a dot like C:\a.\ or C:\b.\, etc. I found a solution that can remove such folder with command rd /q /s "C:\a.\" but if I call win API RemoveDirectory, it returns ERROR_FILE_NOT_FOUND. And I…
jerry.liu
  • 1,143
  • 3
  • 9
  • 8
104
votes
5 answers

Closing a file after File.Create

I check to see if a file exists with if(!File.Exists(myPath)) { File.Create(myPath); } However, when I go to create a StreamReader with this newly created file, I get an error saying that The process cannot access the file '[my file path…
John
  • 10,839
  • 8
  • 26
  • 31
103
votes
7 answers

How to add a new line of text to an existing file in Java?

I would like to append a new line to an existing file without erasing the current information of that file. In short, here is the methodology that I am using the current time: import java.io.BufferedWriter; import java.io.FileWriter; import…
CompilingCyborg
  • 4,760
  • 13
  • 44
  • 61
103
votes
10 answers

What’s the best way to check if a file exists in C++? (cross platform)

I have read the answers for What's the best way to check if a file exists in C? (cross platform), but I'm wondering if there is a better way to do this using standard c++ libs? Preferably without trying to open the file at all. Both stat and access…
c0m4
  • 4,343
  • 10
  • 35
  • 40