At the end of this question you will find a piece of code that I am trying to write to read a file called words.txt
with the following strings:
uno dos tres cuatro cinco seis siete ocho nueve diez
The aim of the code is to be able to store the strings in a two-dimensional array with dynamic memory allocation. This means it would need to work with any file that has strings.
I would need to check:
- Why the code is not working.
- How can I make it so that it stores whatever number of words the file has.
Thank you very much guys!
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
int main()
{
char c, *mystring[20];
int i = 0;
FILE *fich;
setlocale(LC_CTYPE,"spanish");
identifica();
fich = fopen("words.txt", "r");
do
{
mystring[i] = malloc (20 * sizeof(char));
fscanf("%s", mystring[i]);
printf ("%s", mystring[i]);
}
while ((c=fgetc(fich))!=EOF);
return 0;
}