Answers for "c read all from file"

1

read entire file c

char *read_file_content_path(char const *filepath)
{
    FILE *file = fopen(filepath, "r");
    int file_size;
    char *text;

    fseek(file, 0L, SEEK_END);
    file_size = ftell(file);
    rewind(file);
    text = malloc(file_size + 1);
    fread(text, 1, file_size, file);
    text[file_size] = '';
    fclose(file);
    return text;
}
Posted by: Guest on June-12-2021
0

c read file content

char * buffer = 0;
long length;
FILE * f = fopen (filename, "rb");

if (f)
{
  fseek (f, 0, SEEK_END);
  length = ftell (f);
  fseek (f, 0, SEEK_SET);
  buffer = malloc (length);
  if (buffer)
  {
    fread (buffer, 1, length, f);
  }
  fclose (f);
}

if (buffer)
{
  // start to process your data / extract strings here...
}
Posted by: Guest on May-14-2021

Browse Popular Code Answers by Language