I/O Operations Reading/writing Files


Let’s explore how to perform file I/O using system calls in Linux. We’ll cover creating, opening, reading, writing, and closing files using low-level system calls.

Creating a New File:
To create a new file, you can use the open() system call.
Syntax: 
    int open(const char *pathname, int flags, mode_t mode);
Parameters:
    pathname: The path to the file you want to create.
    flags:Flags specifying the file access mode (e.g., O_CREAT | O_WRONLY for write-only).
    mode: Permissions for the new file (e.g., 0644 for read-write permissions for the owner and read-only                 permissions for others).
Example:
#include <fcntl.h> 
#include <stdio.h> 
#include <stdlib.h> 
 int main() 
{ const char *filename = "new_file.txt"; 
 int fd = open(filename, O_CREAT | O_WRONLY, 0644); 
 if (fd == -1) 
    { perror("Error creating file"); 
     exit(EXIT_FAILURE); } 
     printf("File '%s' created successfully.\n", filename); close(fd); // Close the file 
    return 0; 
   }

This program creates a new file named “new_file.txt” with read-write permissions for the owner and read-only permissions for others.

Opening an Existing File:To open an existing file, use the open() system call with appropriate flags.
Example:
int fd = open("existing_file.txt", O_RDONLY); 
if (fd == -1) { perror("Error opening file"); exit(EXIT_FAILURE); } 
 // Read from the file... 

close(fd); // Close the file

Reading from a File:Use the read() system call to read data from a file descriptor.
Example:
char buffer[100]; 
ssize_t  bytes_read = read(fd, buffer, sizeof(buffer)); 
if (bytes_read > 0) 
   { printf("Read %ld bytes: %s\n", bytes_read, buffer); }

Writing to a File:Use the write() system call to write data to a file descriptor.
Example:
const char *data = "Hello, world!"; 
ssize_t  bytes_written = write(fd, data, strlen(data)); 
if (bytes_written > 0) 
    { printf("Wrote %ld bytes.\n", bytes_written); }

Closing a File:Always close the file using close() when done.
Example:
close(fd); // Close the file

Remember that system calls provide low-level access to files, and you need to handle errors appropriately. These examples demonstrate the basic concepts; you can build more complex file I/O operations using these building blocks.

In this program:
  • We create a new file named “my_file.txt” (if it doesn’t exist) and open it for writing.
  • We write the string “Hello, world!” to the file.
  • We close the file.
  • We reopen the same file for reading.
  • We read data from the file and print it.
  • Finally, we close the file again.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> // For open() and related constants
#include <unistd.h> // For read(), write(), close()
#include <string.h>
int main() {
    const char *filename = "my_file.txt";
    int fd; // File descriptor

    // Create a new file (if it doesn't exist) and open it for writing
    fd = open(filename, O_CREAT | O_WRONLY, 0644);
    if (fd == -1) {
        perror("Error creating/opening file");
        exit(EXIT_FAILURE);
    }

    // Write data to the file
    const char *data = "Hello, world!";
    ssize_t bytes_written = write(fd, data, strlen(data));
    if (bytes_written > 0) {
        printf("Wrote %ld bytes to the file.\n", bytes_written);
    }

    // Close the file
    close(fd);

    // Reopen the file for reading
    fd = open(filename, O_RDONLY);
    if (fd == -1) {
        perror("Error opening file for reading");
        exit(EXIT_FAILURE);
    }

    // Read data from the file
    char buffer[100];
    ssize_t bytes_read = read(fd, buffer, sizeof(buffer));
    if (bytes_read > 0) {
        printf("Read %ld bytes from the file: %s\n", bytes_read, buffer);
    }

    // Close the file again
    close(fd);

    return 0;
}

Comments

Popular posts from this blog

CSL 204 OPERATING SYSTEM LAB KTU IV SEM CSE

FCFS and SJF

Shared Memory Inter-Process Communication ( IPC)