Thanks for your feed back Salem ,
As per your feed back I modified the code. It will now indicate the empty file.Ans also in the previous code the word which occur last will get one count more because of the while loops last iteration.Now I made the alternative thing by coping '\0' to the oneword array.
Code:
#include <stdlib.h>
#include <string.h>
#include<stdlib.h>
#include <stdio.h>
int processFile();
int main()
{
processFile();
return 0;
}
int processFile()
{
char c , fname[25] , arr[1000][15]= {0} , oneword[15] ;
int count[1000] = {0} , x = 0 , y , i = 0 , p ;
FILE *file1;
int found=0;
printf("\nEnter file to process : ");
scanf("%s" , &fname);
file1 = fopen(fname , "r");
if (file1 == NULL)
{
printf("error file missing");
return 1;
}
else
{
while(!feof(file1))
{
strcpy(oneword,"\0");
found=0;
c = fscanf(file1 , "%s" , &oneword);
for( i = 0 ; i < 1000 ; i++) //arr[i] != '\0'
{
if(strcmp(arr[i],oneword)==0)
{
count[i]++;
found=1; // Have a indicator that the word already occurred
break;
}
}
if(found==0) // Otherwise the word occurring first
{
strcpy(arr[x] , oneword);
count[x]++; // Add the word to the array and have the count to 1
x++;
}
}
if(x!=0)
{
for(p = 0 ; p < x ; p++)
{
printf("%8s%11d\n" , arr[p] , count[p]);
}
}
else
printf("File is empty \n");
fclose(file1);
return 0;
}
}