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
366
votes
4 answers

How to open a file for both reading and writing?

Is there a way to open a file for both reading and writing? As a workaround, I open the file for writing, close it, then open it again for reading. But is there a way to open a file for both reading and writing?
bigredhat
  • 3,669
  • 2
  • 15
  • 3
347
votes
6 answers

When should I use mmap for file access?

POSIX environments provide at least two ways of accessing files. There's the standard system calls open(), read(), write(), and friends, but there's also the option of using mmap() to map the file into virtual memory. When is it preferable to use…
Peter Burns
  • 44,401
  • 7
  • 38
  • 56
345
votes
9 answers

Copy a file in a sane, safe and efficient way

I search for a good way to copy a file (binary or text). I've written several samples, everyone works. But I want hear the opinion of seasoned programmers. I missing good examples and search a way which works with C++. ANSI-C-WAY #include…
Peter
  • 2,240
  • 3
  • 23
  • 37
340
votes
19 answers

Parsing CSV files in C#, with header

Is there a default/official/recommended way to parse CSV files in C#? I don't want to roll my own parser. Also, I've seen instances of people using ODBC/OLE DB to read CSV via the Text driver, and a lot of people discourage this due to its…
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
332
votes
12 answers

Scanner vs. BufferedReader

As far I know, the two most common methods of reading character-based data from a file in Java is using Scanner or BufferedReader. I also know that the BufferedReader reads files efficiently by using a buffer to avoid physical disk operations. My…
Mads Mobæk
  • 34,762
  • 20
  • 71
  • 78
331
votes
17 answers

Python recursive folder read

I have a C++/Obj-C background and I am just discovering Python (been writing it for about an hour). I am writing a script to recursively read the contents of text files in a folder structure. The problem I have is the code I have written will only…
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
327
votes
14 answers

How can I read large text files line by line, without loading them into memory?

I want to read a large file (>5GB), line by line, without loading its entire contents into memory. I cannot use readlines() since it creates a very large list in memory.
312
votes
11 answers

What are all the common ways to read a file in Ruby?

What are all the common ways to read a file in Ruby? For instance, here is one method: fileObj = File.new($fileName, "r") while (line = fileObj.gets) puts(line) end fileObj.close I know Ruby is extremely flexible. What are the benefits/drawbacks…
dsg
  • 12,924
  • 21
  • 67
  • 111
311
votes
11 answers

How do I read the first line of a file using cat?

How do I read the first line of a file using cat?
Doboy
  • 10,411
  • 11
  • 40
  • 48
309
votes
8 answers

Read file from line 2 or skip header row

How can I skip the header row and start reading a file from line2?
super9
  • 29,181
  • 39
  • 119
  • 172
297
votes
19 answers

How do I check if file exists in jQuery or pure JavaScript?

How do I check if a file on my server exists in jQuery or pure JavaScript?
usertest
  • 27,132
  • 30
  • 72
  • 94
295
votes
3 answers

What's the de-facto way of reading and writing files in Rust 1.x?

With Rust being comparatively new, I've seen far too many ways of reading and writing files. Many are extremely messy snippets someone came up with for their blog, and 99% of the examples I've found (even on Stack Overflow) are from unstable builds…
Jared
  • 4,240
  • 4
  • 22
  • 27
290
votes
11 answers

Batch file to copy files from one folder to another folder

I have a storage folder on a network in which all users will store their active data on a server. Now that server is going to be replaced by a new one due to place problem so I need to copy sub folders files from the old server storage folder to new…
user73628
  • 3,715
  • 5
  • 29
  • 24
287
votes
12 answers

How to write a large buffer into a binary file in C++, fast?

I'm trying to write huge amounts of data onto my SSD(solid state drive). And by huge amounts I mean 80GB. I browsed the web for solutions, but the best I came up with was this: #include const unsigned long long size =…
Dominic Hofer
  • 5,751
  • 4
  • 18
  • 23
282
votes
9 answers

What is simplest way to read a file into String?

I am trying to read a simple text file into a String. Of course there is the usual way of getting the input stream and iterating with readLine() and reading contents into String. Having done this hundreds of times in past, I just wondered how can I…
Gopi
  • 10,073
  • 4
  • 31
  • 45