Skip to content

Instantly share code, notes, and snippets.

@pshaddel
Last active October 13, 2024 08:59
Show Gist options
  • Save pshaddel/e78edc3e6ecaaf5447addf2c732c0495 to your computer and use it in GitHub Desktop.
Save pshaddel/e78edc3e6ecaaf5447addf2c732c0495 to your computer and use it in GitHub Desktop.
Read File in C using different implementations
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
FILE *file;
char buffer[BUFFER_SIZE];
size_t bytesRead;
int repetitions = 1;
// Parse the number of repetitions
repetitions = atoi(argv[1]);
// Repeat the read and log operation
for (int i = 0; i < repetitions; i++)
{
// Open the file in read mode
file = fopen("hello.txt", "r");
// Check if the file exists and is opened successfully
if (file == NULL)
{
fprintf(stderr, "Error: Could not open file 'hello.txt'\n");
return 1;
}
// Read and log chunks of data from the file until EOF
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, file)) > 0)
{
fwrite(buffer, 1, bytesRead, stdout); // Output each chunk to the console
}
// Close the file
fclose(file);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int repeatations = atoi(argv[1]);
int fd;
struct stat sb;
char *fileContent;
for (int i = 0; i < repeatations; i++)
{
// Open the file
fd = open("hello.txt", O_RDONLY);
if (fd == -1)
{
perror("Error: Could not open file");
return 1;
}
// Get the file size
if (fstat(fd, &sb) == -1)
{
perror("Error: Could not get file size");
close(fd);
return 1;
}
// Map the file into memory
fileContent = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (fileContent == MAP_FAILED)
{
perror("Error: Could not map file");
close(fd);
return 1;
}
// Output the file content to the console
fwrite(fileContent, 1, sb.st_size, stdout);
// Unmap the file and close the file descriptor
munmap(fileContent, sb.st_size);
close(fd);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
int fd;
char buffer[BUFFER_SIZE];
ssize_t bytesRead;
int repetitions = 1;
// Parse the number of repetitions
repetitions = atoi(argv[1]);
// Repeat the read and log operation
for (int i = 0; i < repetitions; i++)
{
// Open the file
fd = open("hello.txt", O_RDONLY);
if (fd == -1)
{
perror("Error: Could not open file 'hello.txt'");
return 1;
}
// Reset the file descriptor to the beginning of the file
if (lseek(fd, 0, SEEK_SET) == -1)
{
perror("Error: Could not reset file descriptor");
close(fd);
return 1;
}
// Read and log chunks of data from the file until EOF
while ((bytesRead = read(fd, buffer, BUFFER_SIZE)) > 0)
{
if (write(STDOUT_FILENO, buffer, bytesRead) == -1)
{
perror("Error: Could not write to stdout");
close(fd);
return 1;
}
}
if (bytesRead == -1)
{
perror("Error: Could not read file");
close(fd);
return 1;
}
close(fd);
}
// Close the file descriptor
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment