Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   File Handling Output problem (http://www.go4expert.com/forums/file-handling-output-t11866/)

rahulsince83 5Jul2008 04:29

File Handling Output problem
 
1 Attachment(s)
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


All times are GMT +5.5. The time now is 02:14.