Hello every one.
I coded a program to seperate words from a file and want to input each seperated part to an array.But it doesnot work.Please help me!!
Here is the code
#include<stdio.h>
#include <string.h>
#define max 100
int main()
{
FILE *f;
char data[128];
char *p;
int host=0;
int i=0;
char ch[max]={0};
if((f=fopen("C:\data\def.txt","r"))==NULL){
printf("File cant open.....\n");
}
else{
while(fgets(data,sizeof(data),f)!=NULL){
host++;
p=strtok(data," | ");
for(i=0;i<3;i++){
ch[i]=p;
printf("%s\n",ch[i]);
}
}
printf("Number of hosts:%d\n",host);
}
}
The text file looks like Host1 | Host2 | Host3 |
Thanks very much.Please help me!!!