Linux file operations
Linux file operations
gcc --version
Ubuntu/Debian:
nano example.c
#include <stdio.h>
int main() {
printf("Hello, Linux System Calls!\n");
return 0;
}
If there are no errors, this will generate an executable file called example.
Expected Output:
If you get errors, check the error messages and fix them.
For debugging, you can compile with -g for GDB (GNU Debugger):
gdb ./example
sudo ./example
1. Enable Warnings:
2. gcc -Wall example.c -o example
3. Optimize Performance:
4. gcc -O2 example.c -o example
5. Enable Debugging Mode:
6. gcc -g example.c -o example
These functions are used to perform file operations such as opening, reading, writing,
seeking, and getting file information.
lseek(fd, 0, SEEK_SET);
close(fd);
closedir(dir);
Program: File Handling in Linux (open, read, write, close, lseek, fcntl, stat)
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
int main() {
int fd;
char buffer[100];
Code:
These header files provide functions for handling files, performing read/write
operations, and retrieving file metadata.
write(fd, buffer, size) writes size bytes from buffer to the file descriptor fd.
Here, we write "Hello, Linux System Calls!" (26 characters) to the file.
lseek(fd, 0, SEEK_SET);
close(fd);
#include <stdio.h>
#include <dirent.h>
int main() {
struct dirent *entry;
DIR *dir = opendir(".");
if (dir == NULL) {
perror("Error opening directory");
return 1;
}
closedir(dir);
return 0;
}
closedir(dir);