file-io interview questions
Top file-io frequently asked interview questions
I'm writing a program in C# that needs to repeatedly access 1 image file. Most of the time it works, but if my computer's running fast, it will try to access the file before it's been saved back to the filesystem and throw an error: "File in use by another process".
I would like to find a way around this, but all my Googling has only yielded creating checks by using exception handling. This is against my religion, so I was wondering if anyone has a better way of doing it?
Source: (StackOverflow)
Is it possible to clear an <input type='file' />
control value with jQuery? I've tried the following:
$('#control').attr({ value: '' });
But it's not working.
Source: (StackOverflow)
I need to read a whole file into memory and place it in a C++ std::string
.
If I were to read it into a char[]
, the answer would be very simple:
std::ifstream t;
int length;
t.open("file.txt"); // open input file
t.seekg(0, std::ios::end); // go to the end
length = t.tellg(); // report location (this is the length)
t.seekg(0, std::ios::beg); // go back to the beginning
buffer = new char[length]; // allocate memory for a buffer of appropriate dimension
t.read(buffer, length); // read the whole file into the buffer
t.close(); // close file handle
// ... Do stuff with buffer here ...
Now, I want to do the exact same thing, but using a std::string
instead of a char[]
. I want to avoid loops, i.e. I don't want to:
std::ifstream t;
t.open("file.txt");
std::string buffer;
std::string line;
while(t){
std::getline(t, line);
// ... Append line to buffer and go on
}
t.close()
Any ideas?
Source: (StackOverflow)
I am looking for a way to delete all files older than 7 days in an MS-DOS batch file. I've searched around the web, and found some examples with hundreds of lines of code, and others that required installing extra command line utilities to accomplish the task.
Similar things can be done in BASH in just a couple lines of code. It seems that something at least remotely easy could be done for batch files in Windows. I'm looking for a solution that works in a standard Windows command prompt, without any extra utilities. Please no PowerShell or Cygwin either.
Source: (StackOverflow)
I'm used to doing print >>f, "hi there"
However, it seems that print >>
is getting deprecated. What is the recommended way to do the line above?
Update:
Regarding all those answers with "\n"
...is this universal or Unix-specific? IE, should I be doing "\r\n"
on Windows?
Source: (StackOverflow)
How can I check whether a file exists, before opening it for reading in Java? (equivalent of Perl's -e $filename
).
The only similar question on SO deals with writing the file and was thus answered using FileWriter which is obviously not applicable here.
If possible I'd prefer a real API call returning true/false as opposed to some "Call API to open a file and catch when it throws an exception which you check for 'no file' in text", but I can live with the latter.
Source: (StackOverflow)
Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I find I need to restore that file.
I know I can checkout a file using git checkout HEAD^ foo.bar
, but I don't really know when that file was deleted.
- What would be the quickest way to find the commit that deleted a given filename?
- What would be the easiest way to get that file back into my working copy?
I'm hoping I don't have to manually browse my logs, checkout the entire project for a given SHA and then manually copy that file into my original project checkout.
Source: (StackOverflow)
I've been using this idiom for some time now. And it seems to be the most wide-spread, at least in the sites I've visited.
Does anyone have a better/different way to read a file into a string in Java?
private String readFile( String file ) throws IOException {
BufferedReader reader = new BufferedReader( new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
try {
while( ( line = reader.readLine() ) != null ) {
stringBuilder.append( line );
stringBuilder.append( ls );
}
return stringBuilder.toString();
} finally {
reader.close();
}
}
Source: (StackOverflow)
Is there a way to delete entire directories recursively in Java?
In the normal case it is possible to delete an empty directory.
However when it comes to deleting entire directories with contents, it is not that simple anymore.
How do you delete entire directories with contents in Java?
Source: (StackOverflow)
Is there a standard and reliable way of creating a temporary directory inside a Java application? There's an entry in Sun's issue database, which has a bit of code in the comments, but I wonder if there is a standard solution to be found in one of the usual libraries (Apache Commons etc.)
Source: (StackOverflow)
2015 update See Dan Dascalescu's answer for the Node native way - which has become stable and bug-free in 0.12 and is available in Node 4.0.
2014 update: it seems the original answer is deprecated (read comments) apparently a transform stream http://strongloop.com/strongblog/practical-examples-of-the-new-node-js-streams-api/ is the new hotness.
I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I'm missing some connections to make the whole thing fit together.
(link to the Quora answer: http://www.quora.com/What-is-the-best-way-to-read-a-file-line-by-line-in-node-js)
var Lazy=require("lazy");
new Lazy(process.stdin)
.lines
.forEach(
function(line) {
console.log(line.toString());
}
);
process.stdin.resume();
The bit that I'd like to figure out is how I might read one line at a time from a file instead of STDIN as in this sample.
I tried:
fs.open('./VeryBigFile.csv', 'r', '0666', Process);
function Process(err, fd) {
if (err) throw err;
// DO lazy read
}
but it's not working. I know that in a pinch I could fall back to using something like PHP, but I would like to figure this out.
I don't think the other answer would work as the file is much larger than the server I'm running it on has memory for.
EDIT
The tested solution as provided by Raynos below is:
var lazy = require("lazy"),
fs = require("fs");
new lazy(fs.createReadStream('./MyVeryBigFile.csv'))
.lines
.forEach(function(line){
console.log(line.toString());
}
);
Source: (StackOverflow)
What is the simplest way to get the directory that a file is in? I'm using this to set a working directory.
string filename = @"C:\MyDirectory\MyFile.bat";
In this example, I should get "C:\MyDirectory".
Source: (StackOverflow)
I have a file.txt like:
5 3
6 4
7 1
10 5
11 6
12 3
12 4
Where 5, 3 is a coordinate.
How can I process this data line by line in C++?
I can get the first line, but how do I get the next line of the file?
ofstream myfile;
myfile.open ("text.txt");
Source: (StackOverflow)
I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I somehow can combine the multiple open calls with the with
statement:
try:
with open('a', 'w') as a and open('b', 'w') as b:
do_something()
except IOError as e:
print 'Operation failed: %s' % e.strerror
If that's not possible, what would an elegant solution to this problem look like?
Source: (StackOverflow)