C/need to difference arrays in an input.txt file
Expert: Joydeep Bhattacharya - 3/5/2007
QuestionHi, How can I open a file input.txt
with the form
__________
n
word
word
word
abcdefgijl
ijklmnñov
wordxvvc
___________
n is a number, that tells me the number of words that follows here rows
n = 3; i only know the number untill i read it;
How can i save the 3 types of data here into differents arrays because i will use them later
1.- n = the number of words = n
2.- word = is an array =arrayword
3.- abcdef... = other array =arraypuzzle
I have this so far
CODE
#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<string.h>
#define MAXLEN 15
FILE *Entrada;
FILE *Salida;
void main()
{
int i,n;
char s;
char arreglopalabra[];
char puzzle[];
Entrada=fopen("C:\\TC\\BIN\\input.txt", "r");
Salida=fopen("C:\\TC\\BIN\\output.txt", "w");
if (Entrada==NULL || Salida==NULL)
{
printf("ERROR");
getch();
return;
}
else
{
while(!feof(Entrada))
{
fscanf(Entrada," %d %s ", &n, &arreglopalabra, &puzzle);
fprintf(Salida, " %d %s ", n, arreglopalabra, puzzle);
}
}
clrscr();
printf("\n %d %s", n, palabra);
getch();
fclose(Entrada);
fclose(Salida);
}
AnswerHi Edgar
When you are trying to create the array dynamically after reading the file's first line where you have the data stored for you then its a better approach to read the number from the file and then create the array dynamically [creating array on runtime]
keep a pointer like :
int*p;
p=(int *)malloc(sizeof(int)*n);
Now use this dynamically created array to store the datas from the file and use it to your convinience.
In case of any doubts or clarification regarding anything please feel free to get back to me.
regards
Joydeep Bhattacharya