I want to implement a function the same as cat file
. My function will be mycat(inode)
.
It will take as input the inode of the file and print out the content of the file.
How to achieve that using a C program.
Asked
Active
Viewed 150 times
0

pensee
- 375
- 1
- 10
-
What did you try? – tango-1 Dec 13 '20 at 17:47
-
what I could think of is - Assume the simplest case where the file is in current directory. So loop over all the files in the current directory and if you find the one for which the inode matches then you are done. Is this reasoning correct ? or a better way to achieve the same? – pensee Dec 13 '20 at 19:27
-
If you try to open your file with open() or fopen(), you can know that file is present or not. After that you can read the file contents by read() or fread(). No need for iterating over all files. See open() and read(). – tango-1 Dec 13 '20 at 19:31
-
Does this answer your question? [Open file by inode](https://stackoverflow.com/questions/36092559/open-file-by-inode) – Nate Eldredge Dec 13 '20 at 20:54
-
The short answer is that this requires either low-level system-specific hacks (and you haven't said what system you are using), or else something like `find` to trawl the whole filesystem until you find a pathname with the desired inode number. – Nate Eldredge Dec 13 '20 at 20:56
-
@tango-1 open and fopen takes pathname as a parameter it does not take inode as I see in the man pages. So how could these function be used? Just to give more clarity on this question **I am writing a C program on linux** – pensee Dec 14 '20 at 07:37
-
So what I understand is **inode** will be a struct given in C. But I cant find what struct is this – pensee Dec 14 '20 at 11:29