Hi,

I am trying to read a text file and tokenize the data and store in a character array.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<file.h>
int main(int argc , char *argv[])
{

printf("No of arguments %d \n",argc);
printf("Argument no 1 is %s \n" ,argv[1]);
FILE *fp;
char buffer[11];
char *bp;
char *temp;

 int i=0;  
      if ((fp = fopen(argv[1], "r"))!=NULL) {
     
		fread(buffer, 1, 6, fp);
    buffer[6] = 0;
    fclose(fp);

    printf("first 10 characters of the file:\n%s\n", buffer);
   
      }
	
   int x=0;
	  bp=strtok(buffer," ");
temp= bp;
 
 *temp++;
   
  while(bp!= NULL)
	{
  bp=strtok(NULL," ");
  temp=bp;
  *temp++;

	}


return 0;

}

Text file data is

3 5 10

You can use strcpy() to capture the strings (from tokens) into other variables.

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.