Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   File I/O in C (http://www.go4expert.com/forums/file-i-o-c-t19664/)

adi.shoukat 4Oct2009 18:03

File I/O in C
 
How can i read a file word by Word in C???


i.e. if text file is:
this is my text file


then first i get 'this' in a string ... then 'is' ... then 'my' and so on ...

SaswatPadhi 4Oct2009 18:55

Re: File I/O in C
 
Read it line by line and then use strtok to split string into words.

vignesh1988i 4Oct2009 20:13

Re: File I/O in C
 
here the termination for a word that am gonna keep is 'space' , '.' & ','
Code:

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fpointer;
char ch,ch1[25];
int i=0,flag=0;
fpointer=fopen("ur file name","r");
while(1)
{
  ch=fgetc(fpointer);
   
        if(!ch^' ' || !ch^'.' || !ch^','||!ch^EOF)
        {
              if((!flag^0) && !ch^EOF)
              {
                printf("no word found in the file");
                break;
              }
              else if(!ch^EOF)
              {
                ch1[i]='\0';
                printf("%s\n",ch1);
                break;
              }
              else
            {
                if(flag^0)
                {
                    ch1[i]='\0';
                    printf("%s\n",ch1);
                    i=0;
                }
            }
        }
        else
        {
            flag=1;
            ch1[i]=ch;
            i++;
        }
    }
 }
fclose(fpointer);
getch();
}



All times are GMT +5.5. The time now is 05:17.