How about splitting words terminated by new line in text file (see attached
test.txt)?
I tried this, it works not perfect :

#include <stdio.h>

int main() {
   FILE *fp;

   int letter,i,k,j;
   char myarray[10][5];
   char mywords[30];
   i=k=j=0;

   if( ( fp = fopen( "test.txt", "r")) == NULL) {
       puts("Cannot open the file");
       exit( 0 );
   }

   while( ( letter = fgetc( fp ) ) != EOF){
       mywords[i] = letter;
       if( mywords[i] == '\n' ){
       myarray[j][k]="\0";
       i++;
       j++;
       k=0;
       }
   myarray[j][k++] = mywords[i];
   }

   fclose(fp);
   printf("0 %s\n" ,myarray[0]);
   printf("1 %s\n" ,myarray[1]);
   printf("2 %s\n" ,myarray[2]);

}

Please help if you have time,

Regards,
Hermann


On 2/15/07, Sudhanshu Gupta <[EMAIL PROTECTED]> wrote:

This  is ur solution...
For the sake of simplicity, i hav used arrays instead of pointers.
So plz forgive me if u r keen on using pointers.
well sometimes using of pointers as strings may cause some prob
as the data of variables in mem may get overwritten.
so i personally avoid using them.



#include <stdio.h>
#include <string.h>
int main()
{
char mywords[]="Hallo,Hi,Hai,Hey";
unsigned int i,j,k;
char myarray[4][10];
clrscr();
i=j=k=0;
for(i=0;mywords[i];i++){
if(mywords[i] ==','){
myarray[j][k]='\0';
i++;
j++;
k=0;
}
myarray[j][k++] = mywords[i];
}
printf("%s\n" ,myarray[ 0]);
printf("%s\n" ,myarray[ 1]);
printf("%s\n" ,myarray[ 2]);
printf("%s\n" ,myarray[ 3]);
getch();
}







Sudhanshu Gupta

------------------------------
It's here! Your new message!
Get new email 
alerts<http://us.rd.yahoo.com/evt=49938/*http://tools.search.yahoo.com/toolbar/features/mail/>with
 the free Yahoo!
Toolbar.

this1 is1 from1 line1
this2 is2 from2 line2
this3 is3 from3 line3

Reply via email to