I want to open a .txt
file using a C program. Now the thing I am searching is how to open this file from a C program using a system call, e.g. open
.
Asked
Active
Viewed 3,660 times
0
-
1I pasted "[how to open this file from a C PROGRAM using a system call. e.g open](https://www.google.com/search?q=how+to+open+this+file+from+a+C+PROGRAM+using+a+system+call.+e.g+open&oq=how+to+open+this+file+from+a+C+PROGRAM+using+a+system+call.+e.g+open&aqs=chrome..69i57.299j0j7&sourceid=chrome&es_sm=93&ie=UTF-8)" into google and it found [this tutorial with example](http://www.techytalk.info/linux-system-programming-open-file-read-file-and-write-file/comment-page-1/). Is that any help? – that other guy Apr 09 '15 at 04:06
-
similar Stack question and answers: http://stackoverflow.com/questions/3463426/in-c-how-should-i-read-a-text-file-and-print-all-strings – iamauser Apr 09 '15 at 04:09
-
1Either you are writing a Bash script or a C program, but it can't be both. I removed the [tag:bash] stuff but if you can explain how it's relevant, please do edit your question to add it back. – tripleee Apr 09 '15 at 04:13
-
Use a system call, e.g. `open`. – hobbs Apr 09 '15 at 04:14
-
1In the meantime, if you have a concrete text processing problem you want to solve, you can probably learn Awk and solve it in less time than it takes to write and debug your first C program. Unless you specifically want to learn C or absolutely need the fastest possible program, I would take two steps back. – tripleee Apr 09 '15 at 04:14
-
It seems to me you answered your own question. `open()` is a syscall. – Havenard Apr 09 '15 at 04:16
-
Keep in mind using syscalls to read files on disk can be very inneficient because syscalls are very basic and don't offer any kind of buffering like `fopen()` does. If you use `read()` to read 1 byte, it will read 1 byte from disk, if you read another byte, there it goes to the disk again to fetch the next byte, its very inneficient. – Havenard Apr 09 '15 at 04:21
1 Answers
0
Using open()
system call you can open the file you need.
int open(const char *pathname, int flags);
It will return the file descriptor for doing the operation in that file.
Or else you can use the fopen()
function. Which will return the FILE
stream.

Karthikeyan.R.S
- 3,991
- 1
- 19
- 31
-
Note that the man page is badly laid out but gives two declarations for `open()`: _`int open(const char *pathname, int flags);int open(const char *pathname, int flags, mode_t mode);int creat(const char *pathname, mode_t mode);`_ The second one must be used if the `flags` argument include the `O_CREAT` bit. The POSIX specification for [`open()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html) uses: `int open(const char *path, int oflag, ...);` — explicitly declaring it as a varargs function. However, only the 2- and 3- argument calls shown at die.net are valid. – Jonathan Leffler Apr 09 '15 at 04:30
-
Instead of linking to linux.die.net (which is also outdated), just link to the official one: http://man7.org/linux/man-pages/man2/open.2.html – cremno Apr 09 '15 at 05:17