1
I want to create a program that:
Opens the file
teste
I figured out the size of this file
Allocate enough memory space for file + 1 (
\0
)Read the file contents using the function
fread
and saved in the allocated locationPrints the read content
But when I run the program it prints a lot of random characters as if it were converting a random number into ascii-code
I know that fread()
is not the most efficient way, more is just a test for a real problem in a larger source code and I deduced that the problem would be a similar part
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *file;
int FileSize;
int result;
char *c;
file = fopen("P:\\teste", "r");
fseek(file, 0, SEEK_END);
FileSize = ftell(file);
c = (char *) malloc((FileSize+1) * sizeof(char));
result = fread(c, sizeof(char), FileSize, file);
printf("%s", c);
free(c);
fclose(file);
return(0);
}