How to resolve the algorithm Read entire file step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Read entire file step by step in the C programming language

Table of Contents

Problem Statement

Load the entire contents of some text file as a single string variable. If applicable, discuss: encoding selection, the possibility of memory-mapping. Of course, in practice one should avoid reading an entire file at once if the file is large and the task can be accomplished incrementally instead (in which case check File IO); this is for those cases where having the entire file is actually what is wanted.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Read entire file step by step in the C programming language

The code snippet is a C program that reads the content of a file and prints it to the standard output. It uses three different methods to read the file: using the standard C library, using memory mapping, and using Windows API.

The first method uses the fopen() function to open the file, then uses fseek() to seek to the end of the file and get the file size. It then uses rewind() to go back to the beginning of the file and uses malloc() to allocate a buffer of the size of the file. It then uses fread() to read the file into the buffer and prints it to the standard output.

The second method uses the open() function to open the file, then uses fstat() to get the file size. It then uses mmap() to map the file into memory, and prints the mapped memory to the standard output.

The third method uses the CreateFile() function to open the file, then uses GetFileSize() to get the file size. It then uses CreateFileMapping() to create a file mapping object, and uses MapViewOfFile() to map the file into memory. It then prints the mapped memory to the standard output.

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>

int main()
{
  char *buffer;
  FILE *fh = fopen("readentirefile.c", "rb");
  if ( fh != NULL )
  {
    fseek(fh, 0L, SEEK_END);
    long s = ftell(fh);
    rewind(fh);
    buffer = malloc(s);
    if ( buffer != NULL )
    {
      fread(buffer, s, 1, fh);
      // we can now close the file
      fclose(fh); fh = NULL;
      
      // do something, e.g.
      fwrite(buffer, s, 1, stdout);

      free(buffer);
    }
    if (fh != NULL) fclose(fh);
  }
  return EXIT_SUCCESS;
}


#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
  char *buffer;
  struct stat s;

  int fd = open("readentirefile_mm.c", O_RDONLY);
  if (fd < 0 ) return EXIT_FAILURE;
  fstat(fd, &s);
  /* PROT_READ disallows writing to buffer: will segv */
  buffer = mmap(0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);

  if ( buffer != (void*)-1 )
  {
    /* do something */
    fwrite(buffer, s.st_size, 1, stdout);
    munmap(buffer, s.st_size);
  }

  close(fd);
  return EXIT_SUCCESS;
}


#include <windows.h>
#include <stdio.h>

int main() {
    HANDLE hFile, hMap;
    DWORD filesize;
    char *p;
    
    hFile = CreateFile("mmap_win.c", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    filesize = GetFileSize(hFile, NULL);
    hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    p = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);

    fwrite(p, filesize, 1, stdout);
    
    CloseHandle(hMap);
    CloseHandle(hFile);
    return 0;
}


  

You may also check:How to resolve the algorithm Sudan function step by step in the Draco programming language
You may also check:How to resolve the algorithm Filter step by step in the PHP programming language
You may also check:How to resolve the algorithm String case step by step in the Nial programming language
You may also check:How to resolve the algorithm Cullen and Woodall numbers step by step in the Asymptote programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Lambdatalk programming language