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
277
votes
17 answers

How to read an entire file to a string using C#?

What is the quickest way to read a text file into a string variable? I understand it can be done in several ways, such as read individual bytes and then convert those to string. I was looking for a method with minimal coding.
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
275
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
  • 6,284
  • 7
  • 28
  • 30
273
votes
11 answers

Read/Write String from/to a File in Android

I want to save a file to the internal storage by getting the text inputted from EditText. Then I want the same file to return the inputted text in String form and save it to another String which is to be used later. Here's the code: package…
Major Aly
  • 2,570
  • 3
  • 16
  • 19
270
votes
17 answers

C read file line by line

I wrote this function to read a line from a file: const char *readLine(FILE *file) { if (file == NULL) { printf("Error: file pointer is null."); exit(1); } int maximumLineLength = 128; char *lineBuffer = (char…
lron
  • 2,717
  • 2
  • 16
  • 3
270
votes
15 answers

Merge PDF files

Is it possible, using Python, to merge separate PDF files? Assuming so, I need to extend this a little further. I am hoping to loop through folders in a directory and repeat this procedure. And I may be pushing my luck, but is it possible to…
Btibert3
  • 38,798
  • 44
  • 129
  • 168
270
votes
9 answers

Read binary file as string in Ruby

I need an easy way to take a tar file and convert it into a string (and vice versa). Is there a way to do this in Ruby? My best attempt was this: file = File.open("path-to-file.tar.gz") contents = "" file.each {|line| contents << line } I thought…
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127
263
votes
4 answers

How to open a file using the open with statement

I'm looking at how to do file input and output in Python. I've written the following code to read a list of names (one per line) from a file into another file while checking a name against the names in the file and appending text to the occurrences…
Disnami
  • 2,903
  • 2
  • 17
  • 15
256
votes
8 answers

Android; Check if file exists without creating a new one

I want to check if file exists in my package folder, but I don't want to create a new one. File file = new File(filePath); if(file.exists()) return true; Does this code check without creating a new file?
MBH
  • 16,271
  • 19
  • 99
  • 149
255
votes
11 answers

Delete all files in directory (but not directory) - one liner solution

I want to delete all files inside ABC directory. When I tried with FileUtils.deleteDirectory(new File("C:/test/ABC/")); it also deletes folder ABC. Is there a one liner solution where I can delete files inside directory but not directory?
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
255
votes
24 answers

How do I read an entire file into a std::string in C++?

How do I read a file into a std::string, i.e., read the whole file at once? Text or binary mode should be specified by the caller. The solution should be standard-compliant, portable and efficient. It should not needlessly copy the string's data,…
wilbur_m
252
votes
8 answers

In-place edits with sed on OS X

I'd like edit a file with sed on OS X. I'm using the following command: sed 's/oldword/newword/' file.txt The output is sent to the terminal. file.txt is not modified. The changes are saved to file2.txt with this command: sed…
SundayMonday
  • 19,147
  • 29
  • 100
  • 154
252
votes
27 answers

getResourceAsStream returns null

I'm loading a text file from within a package in a compiled JAR of my Java project. The relevant directory structure is as follows: /src/initialization/Lifepaths.txt My code loads a file by calling Class::getResourceAsStream to return a…
user1131435
248
votes
12 answers

Writing a Python list of lists to a csv file

I have a long list of lists of the following form --- a = [[1.2,'abc',3],[1.2,'werew',4],........,[1.4,'qew',2]] i.e. the values in the list are of different types -- float,int, strings.How do I write it into a csv file so that my output csv file…
user1403483
247
votes
6 answers

How do I create directory if it doesn't exist to create a file?

I have a piece of code here that breaks if the directory doesn't exist: System.IO.File.WriteAllText(filePath, content); In one line (or a few lines), is it possible to check if the directory leading to the new file doesn't exist and if not, to…
Diskdrive
  • 18,107
  • 27
  • 101
  • 167
241
votes
6 answers

Creating temporary files in Android

What's the best way to create a temporary file in Android? Can File.createTempFile be used? The documentation is very vague about it. In particular, it's not clear when temporary files created with File.createTempFile are deleted, if ever.
hpique
  • 119,096
  • 131
  • 338
  • 476