2
how to read data from a file in c and assign it to a string?
file. c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CONFIG "config.ini"
void main()
{
char *data, *data1, *stream;
FILE* arquivo;
arquivo = fopen(CONFIG, "r");
/*
// data = fgets(stream, sizeof(arquivo), arquivo);
// data1 = fscanf(arquivo, "%s", stream);
*/
// printf("%s\t%s\n", data, data1);
fclose(arquivo);
// fopen(CONFIG, "w");
return;
}
config.ini
Lorem ipsum auctor curabitur at justo maecenas hendrerit feugiat, adipiscing augue
accumsan ornareeu nunc iaculis cubilia, sodales quisque bibendum dapibus ullamcorper
ornare diam. consectetur pretium eros velit ante pellentesque taciti ullamcorper interdum
gravida himenaeos viverra mauris luctus hendrerit habitasse arcu fringilla, praesent
habitant mi facilisis curae fames quam sapien.
Thank you again Isac
– Vanderson
The string contained in the pointer
data
is not well formed because it does not have the terminator\0
andmalloc()
does not allocate an extra byte for inclusion of that terminator.– Lacobus
@Lacobus Well put, it was something I missed. Thank you for warning me
– Isac
I didn’t know that
ftell
. Always learning new things by reading your answers– Jefferson Quesado
@Jeffersonquesado Which is very useful by the way, for cases like this.
– Isac