This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting Filename from file descriptor in C
How get fileName having FILE*?
Is there any way where I can find the file_name from a file-pointer in C?
fp = fopen(file,"r");
From fp, is it possible to get the file name which I have opened?
I believe not, because a file* could be to something that doesn't even have a name. There might be a platform dependent way, depending I. Your system, so if you don't care about portability, try looking at your compiler's definition of FILE if it has one. Odds are that your only way is to save the name when it's opened.
There are some other potentially hacky ways as well- check this link:
http://bytes.com/topic/c/answers/218921-how-get-filename-file-pointer
Cheers!
See here for why this is hard, and can't be done in a portable way.
http://discuss.fogcreek.com/joelonsoftware5/default.asp?cmd=show&ixPost=179112
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am writing a C program that takes 7 input files. I could pass these to my program as command line arguments but it is not exactly succinct and could lead to errors with the order they are provided.
I had thought of creating 1 input file containing the 7 required file names and just passing this to my program.
What is best practice for providing a large number of input files to a C program?
There is a variety of mechanisms in common use for designating input files to programs, prominent among them (in no particular order):
separate command-line arguments, possibly with built-in default values (more on this below)
a common filename stem passed as an argument, with different extensions for the different individual files; the specific filenames are computed by the program from the one stem
a standard or user-specified file containing the names of the files to operate upon
Combinations of those are possible, and there are other alternatives. There is no single best practice, and which method or method(s) to choose is to some extent a matter of opinion and personal preference, likely with a dose of the specific practicalities of the particular program.
I could pass these to my program as command line argument but it is not exactly succinct and could lead to errors with the order they are provided.
I agree.
I had thought of creating 1 input file containing the 7 required file names and just passing this to my program. Is this good practice?
That is one of the common methods. Note, however, that although it makes the command line more succinct, it doesn't by itself do much for the ordering issue.
Personally, I wouldn't be too keen on a program that has seven required arguments, yet I also wouldn't like being forced to write an auxiliary file just to convey filenames to the program. To the extent that you do use command-line arguments, I urge you to define them as options, in the getopt() sense of the term, because mixing up argument order will otherwise be a significant practical problem for your users. Similarly, if you provide a control file as a way to convey the working file names, then I suggest going to just a bit more effort to use an order-insensitive key / value format. To the extent that you can provide sensible defaults, that would improve ease-of-use, too.
This question already has answers here:
Reading a text file backwards in C
(5 answers)
Closed 7 years ago.
I am reading a one line string from a file and I need to read it into an array in reverse order. How can I accomplish this?(I am using C).
So, my file looks like:
ACGTGCGATCGATCGATCGATATCGATCGTCTGCTTAAGCTC
And I want my array of chars that I read into to look like:
CTCGAA...
Thanks in advance.
Read the file front-to-back into the array, then reverse the contents of the array in place. That's the best way to do that as reading a file backwards is a very slow operation.
This question already has an answer here:
Determining if file has been copied or not in C [closed]
(1 answer)
Closed 9 years ago.
In Windows, if you go to a file's properties it shows the last access time right under the time last modified. This changes when I copy it.
How do I view this in C?
You can use the GetFileTime() function to get it. This MSDN article has more details about file times.
The portable way of getting the last modified timestamp is by using fstat or stat. If you want to go the Windows-only route (by directly calling a Windows API), see #xxbbcc's answer.
See How can I get a file's size in C++? for a short piece of sample code that uses stat/fstat - only change for your purpose is that you'll want to read the time_t st_mtime field.
This question already has answers here:
Parsing of binary data with scala
(5 answers)
Closed 9 years ago.
I have some binary files which are written by a Java application in the following way:
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(complete_path_to_file)));
dos.writeInt(aInteger);
dos.writeLong(aLong);
dos.writeFloat(aFloat);
dos.close();
Now, I'd like to read this kind files in a Scala framework but I really don't have any ideas about how to do that.
Could you help me?
Reading it using the same classes and methods you'd use in Java is probably going to be the most error-free, as you have a direct correspondence between read and write.
You can also use Scala I/O, which is the best library for file I/O for Scala -- and a possible addition to the standard library in the future.
Then there's sbinary, where less concern is given to I/O itself, and more concern is given to describing the binary record.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a better way than parsing /proc/self/maps to figure out memory protection?
I have a pointer and I want to check if the referenced memory is readable, writeable and/or executable. How can I do this?
You will have to parse the file /proc/self/maps which contains memory mappings of your process, and check which of the mappings lie within the value of your pointer (if any). The /proc/self/maps file contains the memory protection (read/write/execute).
See this question for more info about the maps file.