Arrays.

Kith Kanan cfiddyme at gara.une.oz.au
Tue May 21 19:52:41 AEST 1991


 hi,
     I'm new to C and have been trying out a few things and have come up with
 a problem. In the code below I am reading a series of three digit numbers
 from a file and storing them as strings in a 2-d array.When I print them out
 strange things happen. Can someone please tell me what is causing this and how
 do I prevent it. Also when I replace the fscanf function with fgets() i get
 errors. Why?
                                 Thanks
                                            Chris.

#include <stdio.h>

main()
{
   char array[10][3];
   char s[3];
   FILE *input,*fopen();
   int i = 0;

   input = fopen("inarray","r");
   while(!feof(input))  {
      fscanf(input,"%s\n",s);
      strcpy((char *) array[i],s);
      i++;
    }
   for(i=0;i<=9;i++)
     fprintf(stdout,"%s\n",array[i]);
}

 This is the output of the program.The input file is just the first three
 digit's of each line.

229657659312999867555664811109
657659312999867555664811109
659312999867555664811109
312999867555664811109
999867555664811109
867555664811109
555664811109
664811109
811109
109



More information about the Comp.lang.c mailing list