1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

File I/O in C

Discussion in 'C' started by adi.shoukat, Oct 4, 2009.

  1. adi.shoukat

    adi.shoukat New Member

    Joined:
    Jun 15, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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 ...
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Read it line by line and then use strtok to split string into words.
     
  3. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    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();
    }
     
    Last edited by a moderator: Oct 5, 2009

Share This Page