Hi, How can I open a file input.txt
with the form and divide its info in differents arrays

__________
n
word
word
word


abcdefgijl
ijklmnñov
wordxvvc
___________

n is a number, that tells me the number of words that follows here
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

#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);
}

Recommended Answers

All 9 Replies

First you need to declare a 2-dimensional character array whose dimensions can be allocated at runtime. char **array = 0; Next, after opening the input file read the first line which is the number of words in the array. That tells you the number of rows (the first dimension) to allocate.

int nWords = 0;
fscanf(Entrada, "%d", &nWords);
array = malloc( nWords * sizeof(char **));

Finally, read the remainder of the file

char inbuf[255];
int i = 0;
while( i < nWords && fgets(inbuf,sizeof(inbuf),Entrada) != NULL)
{
   // allocate memory for the new string
   array[i] = malloc(strlen(inbuf)+1);
   // remove the trailing '\n' that fgets() might
   // leave in the string
   if( inbuf[strlen(inbuf)-1] == '\n')
      inbuf[strlen(inbuf)-1] = 0;
   // copy to the array
   strcpy(array[i],inbuf);
   ++i;
}

Hi, well i dont understand that at all
but with this i get 1 array for the words and other array for the puzzle?

word
word =array1??
word ??

abcsdsadsd
sdaasdasdd =array2??
worddsdddl???

But whats the name here in the code for array 1, and array2?

do the code is the following?
Please help me

#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<string.h>
#define MAXLEN 15

FILE *Entrada;
FILE *Salida;
void main()
{
  
 char **array = 0;
 int nRows = 0;
  

 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))
     {
       {   
       int nRows = 0;
       fscanf(Entrada, "%d", &nRows);
       array = malloc( nRows * sizeof(char **));
       }
        
       {
       char inbuf[255];  
       int i = 0;
       while( fgets(inbuf,sizeof(inbuf),Entrada) != NULL)
       {
       array[i] = malloc(strlen(inbuf)+1);
            if( inbuf[strlen(inbuf)-1] == '\n')
            inbuf[strlen(inbuf)-1] = 0;
            strcpy(array[i],inbuf);
            i++;
       }

      }

  
 fclose(Entrada);
 fclose(Salida);
}

>> while(!feof(Entrada))
Delete that loop -- it is not needed because the loop using fgets() will take care of finding end-of-file.

>>But whats the name here in the code for array 1, and array2?
The code I posted is for array1. Is array2 in the same file or a different file? And does it have the same format as for array1?

>> while(!feof(Entrada))
Delete that loop -- it is not needed because the loop using fgets() will take care of finding end-of-file.

>>But whats the name here in the code for array 1, and array2?
The code I posted is for array1. Is array2 in the same file or a different file? And does it have the same format as for array1?

well
array 1 must be
word
word
word


and array 2 the puzzle

asadssdadasd
sdadsadsdsds
asdsadwordss

can you help me with the code in the way that has to be?
please

Hi, well i dont understand that at all
but with this i get 1 array for the words and other array for the puzzle?

word
word =array1??
word ??

abcsdsadsd
sdaasdasdd =array2??
worddsdddl???

But whats the name here in the code for array 1, and array2?

do the code is the following?
Please help me

#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<string.h>
#define MAXLEN 15
 
FILE *Entrada;
FILE *Salida;
void main()
{
 
 char **array = 0;
 int nRows = 0;
 
 
 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))
     {
       {   
       int nRows = 0;
       fscanf(Entrada, "%d", &nRows);
       array = malloc( nRows * sizeof(char **));
       }
 
       {
       char inbuf[255];  
       int i = 0;
       while( fgets(inbuf,sizeof(inbuf),Entrada) != NULL)
       {
       array[i] = malloc(strlen(inbuf)+1);
            if( inbuf[strlen(inbuf)-1] == '\n')
            inbuf[strlen(inbuf)-1] = 0;
            strcpy(array[i],inbuf);
            i++;
       }
 
      }
 
 
 fclose(Entrada);
 fclose(Salida);
}

So the prefix number indicates how many words are used for that specific array? Example:

3 word1 word2 word3
2 word1 word2
5 word1 word2....

Each one of those sets would be seperate arrays of perhaps seperate data types? If that is what you're doing then I guess you already know which sets are of which data types? If this is what you're doing, then there is many mechanisms you can implment. You'd first need to get the number so that you know how many words to read in for the array before the next array number is reached (i.e. 2). Then you'd use that number to allocate memory using malloc. Then you'd read in that many words, sepreated perhaps by whitespaces. Then you'd repeat the same process and use the same variable to extract the number of words for a given array. These suggestions are based of the aussumtion that the intended functions are that assumed above. Let me know.

Good luck, LamaBot

Thats right the input.txt file looks like


5
word
word
word
word
word


asadsdasdsdasdsadsadasda
sadasdasdaslsadasdasdassl
asdsdsswordsdsdsadsadsss
sdadasdsadawordsdsdsdsdl
sdsadasdasdsadsadsadasdl
sdsadsaasdasasworddsdadl
_________________________________

2
word
word


asadsdasdsdasdsadsadasda
sadasdasdaslsadasdasdassl
asdsdsswordsdsdsadsadsss
sdadasdsadawordsdsdsdsdl
sdsadasdasdsadsadsadasdl
sdsadsaasdasasworddsdadl

___________________________________

1
word

asadsdasdsdasdsadsadasda
sadasdasdaslsadasdasdassl
asdsdsswordsdsdsadsadsss
sdadasdsadawordsdsdsdsdl
sdsadasdasdsadsadsadasdl
sdsadsaasdasasworddsdadl


Howq to extract the data in differents arrays??????
for (words) and for (puzzle)


5(number of words)
word(words)
word
word
word
word


asadsdasdsdasdsadsadasda (puzzle)
sadasdasdaslsadasdasdassl
asdsdsswordsdsdsadsadsss
sdadasdsadawordsdsdsdsdl
sdsadasdasdsadsadsadasdl
sdsadsaasdasasworddsdadl

Howq to extract the data in differents arrays??????
for (words) and for (puzzle)


5(number of words)
word(words)
word
word
word
word


asadsdasdsdasdsadsadasda (puzzle)
sadasdasdaslsadasdasdassl
asdsdsswordsdsdsadsadsss
sdadasdsadawordsdsdsdsdl
sdsadasdasdsadsadsadasdl
sdsadsaasdasasworddsdadl

First I'd like to suggest that you put the number representing the number of words and the words that follow on the same line; It just seems easier in my opinion. Let use the above puzzle as an example. You could put a number at the very begginning of the input.txt file indicating how many puzzles or games there are. You could use structures like used simliarly as the following:

#define PUZZLE_BUFFER 100
#define MAX_ARRAY 8
#define MAX_ELEMENTS 10
 
struct puzzle {
   char word[MAX_ARRAY][MAX_ELEMENTS];
   char buffer[PUZZLE_BUFFER];
};

If you use the above method you could omit the numbers representing the number of words that follow and use a delimiter such as ":" to indicate the start and end of a given puzzle in the input.txt file.

Another way is to dynamically allocate a character array for each word per loop putting the returned address into an array element of an array of pointers. Using the read in prefixed number indicating how many words are to be read. Also, I'd just create a puzzle buffer of fixed size instead of allocating memory for it too. This method is pretty complicated and I would'nt recommend using it. There are other ways, similar to this one, you can use but the struct suggestion if you're using C would be sufficient in my opinion.

Good luck, LamaBot

Here is an example program that might give you some ideas:

#include <stdlib.h>
#include <fstream.h>
 
#define PUZZLE_BUFFER 100
#define MAX_ARRAY 8
#define MAX_ELEMENTS 10
 
#define MAX_PUZZLE 8
 
struct puzzle {
   char word[MAX_ARRAY][MAX_ELEMENTS];
   char buffer[PUZZLE_BUFFER];
};
 
int main() {
 
FILE *ifile;
char temp;
int inc1, inc2, inc3;
struct puzzle puzzle_array[MAX_PUZZLE];
 
inc1 = inc2 = inc3 = 0;
 
if ((ifile = fopen("input.txt", "r")) == NULL) {
       puts("Error opening input file");
       return 1;
}
/* Loop until end-of-file is reached */
while (!feof(ifile)) {
     temp = fgetc(ifile); 
     /* Check to see if next word is being extracted */
     if (temp == ',') {
         inc1++;
         inc2 = 0;
     } else if (temp == ':') {
         /* This will get the puzzle data */
         while ((temp = fgetc(ifile)) != ';')  {
               puzzle_array[0].buffer[inc3] = temp;
               inc3++;
         }
         break;                           
     } else {
         /* Make sure inc2 didn't exceed MAX_ELEMENTS and apped temp */
         if (inc2 <= MAX_ELEMENTS) 
             puzzle_array[0].word[inc1][inc2] = temp;
         inc2++;
     }          
} 
 
return 0;
}

Input format to the above program for "input.txt" would be:

word,word,word,word,word:asadsdasdsdasdsadsadasda sadasdasdaslsadasdasdasslasdsdsswordsdsdsadsadssssdadasdsadawordsdsdsdsdlsdsadasdasdsadsadsadasdlsdsadsaasdasasworddsdadl;

I will not do the project for you but this might give you some ideas. Also the above program is an illustration and its execution will not execute any verbose code, or print anything for that matter. It serves as a illustration.

Good luck, LamaBot

Let's go back to the code in your first post -- in this thread.

#include<stdio.h>
#include <stdlib.h>
#include<conio.h>      [I]//** kill this -- you shouldn't use it[/I]
#include<string.h>
#define MAXLEN 15

FILE *Entrada;
FILE *Salida;
void main()           [I]//** main is NEVER a [B]void[/B], it's an [B]int[/B][/I]
{
 int i,n;
 char s;
 char arreglopalabra[];  [I]//** you never assigned any space.
                         //** I assume this is for the words?  Define the 
                         //** both the max word size and max number of 
                         //** words:
                         //**  [B]char arreglopalabra[MAXWORDS][MAXLENGTH][/B]  [/I]

 char puzzle[];          /[I]/** same here[/I]
                         [I]//**  [B]char puzzle[MAXROWS][MAXCOLS][/b][/I]

 Entrada=fopen("C:\\TC\\BIN\\input.txt", "r");
 Salida=fopen("C:\\TC\\BIN\\output.txt", "w");

 if (Entrada==NULL || Salida==NULL)
 {
  printf("ERROR");
  getch();    [I]//** change to [B]getchar()[/B][/I]
  return;
 }
 else
 {
     while(!feof(Entrada))  [I]//** See [b] this [/b] -- and it doesn't go here anyway[/I]
     {
        //** read the first line (number of words
        //** Loop reading that many words into [I]arreglopalabra[/I]
        //** Don't you need to know the size of the puzzle?
        //** Read each line of the puzzle -- in a loop
        fscanf(Entrada," %d %s ", &n, &arreglopalabra, &puzzle);

        fprintf(Salida, " %d %s ", n, arreglopalabra, puzzle);

     }


 }

 clrscr();   [I]//** get rid of this, too[/I]
 printf("\n %d %s", n, palabra);
 getch();    [I]//** change to [B]getchar()[/B][/I]
 fclose(Entrada);
 fclose(Salida);
}

clrscr() and getch() should not be used -- they are not standard. clrscr() hasn't been used for 10 years (about) so won't work when you get out in the real world. getch() is not defined everywhere (only 2 current compilers) so will also cause problems in the real world.

And please indent appropriately -- 3-4 spaces, not 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.