I have a problem in reading a file content which r in tab separated value
formate eg.
MODULE AD0 AD1 AD2 AD3 AD4 AD5 AD6 AD7
4011 +001.10 -001.00 +000.11 +000.11 -001.11 ~ ~ ~
4012 +001.10 -001.00 +000.11 +000.11 -001.11 ~ ~ ~
4013 +001.10 -001.00 +000.11 +000.11 -001.11 ~ ~ ~
4014
from above table i have to read 1st column like.
MODULE
4011
4012
4013
i have tried some code which is read the content & print it in desired formate
BUT THE MAIN PROBLEM IS WHEN I TRY TO STORE IN CHAR * ARRAY
IT GET STORE NEXT VALUE WHICH IS POINTED BY FILE POINTER
Code on Linux system:
Expand|Select|Wrap|Line Numbers
- #include<stdio.h>
- #include<string.h>
- #include<fcntl.h>
- #include<sys/stat.h>
- #define LINE_LENGTH 200
- #define ESC 27
- int i,j,l = 0;
- main()
- {
- FILE *fp;
- char list[LINE_LENGTH];
- char *choices[6];
- int num;
- fp=fopen("../pankaj/c1.txt","r");
- printf("\n Enter the No.of Modules :");
- scanf("%d",&num);
- system("clear");
- int k =0;
- while(fgets(list,LINE_LENGTH,fp) && num >= 0 )
- {
- char *result1=NULL;
- char *sptr=list;
- // result1 = strtok_r(list,"\n",&sptr);
- result1=strsep(&sptr,"\n");
- while(result1!=NULL)
- {
- int len;
- len =strlen(result1);
- result1[len]='\0';
- choices[k]=result1;
- printf("\n%s ",result1);
- printf(" %s\n", choices[k]);
- result1= strtok(NULL,"\n");
- k=k+1;
- }
- num--;
- }
- for(l=0;l<=5;l++)
- {
- printf("\n%s\n",choices[l]) ;
- }
- fclose(fp);
- }
printf(" %s\n", choices[k]);
gives desired output but when i try to store in char * choices[ ]
and then try to print out of while loop it get printed value as ......
4014
which is not a desired o/p
So my question is why this array ptr stores some other value??????????
plz help me I m new in C so if is there any mistake plz forgive me..............
I HOPE u will Reply me as soon as possible......