You are here:

C/need to difference arrays in an input.txt file

Advertisement


Question
Hi, 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);
}


Answer
Hi 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

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

Ability to solve C and Data Structure problems and puzzles with simple and easy to understand logic.

Experience

C, C++, Java, Data Structure, PHP, Web Designing

Organizations
http://www.scodz.com Designation: webmaster

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Past/Present Clients
http://analysingc.50webs.com http://www.funforu.com

©2012 About.com, a part of The New York Times Company. All rights reserved.