File Handling Output problem

Discussion in 'C' started by rahulsince83, Jul 4, 2008.

  1. rahulsince83

    rahulsince83 New Member

    Joined:
    Jul 4, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi, This is a fairly simple program but i seem to be stuck a little. I 've been trying to write to a file all alphabets and then trying to read out the file every 5th alphabet from the beginning of the file. Program runs fine but its printing the first character at the beginning of file twice, both at 0th position and fifth position but after that program seems to go swiftly

    here's the code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main(void)
    {
     FILE *fp;
     char c;
     long i=0L;
     clrscr();
     printf("Enter the characters now:");
     fp=fopen("fseek.txt","w");
     while((c=getchar())!=EOF)
     {
        putc(c,fp);
        putc(' ',fp);
     }
     printf("\nThe number of characters written in file is:%d",ftell(fp));
     printf("\nFile written successfully");
     fclose(fp);
     printf("\n Now opening file for reading every fifth character from starting of file:");
     fp=fopen("fseek.txt","r");
     if(fp==NULL)
     printf("Opening file failed:Error");
    
     else
     {
        while((c=getc(fp))!=EOF)
        {
           //printf("%c",c);
           fseek(fp,i,0);
           i=i+5L;
           printf("\nThe character %c is at position %d",c,ftell(fp));
    
        }
      }
      fclose(fp);
    getch();
    }
    
    and here's the output attached with file....

    any help would be appreciated , I think i'm missing just something but everything else works fine......

    Thanks in advance
     

    Attached Files:

    • op.jpg
      op.jpg
      File size:
      29.3 KB
      Views:
      318
    Last edited by a moderator: Jul 5, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice