reading from end to beginnig of file

This is a discussion on reading from end to beginnig of file within the C Programming forums, part of the General Programming Boards category; I know i am supposed to use fseek...what i don't understand is how does it work I am trying to ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    Unhappy reading from end to beginnig of file

    I know i am supposed to use fseek...what i don't understand is how does it work
    I am trying to read character by character starting from last to first from a file and writing them in another one(reversing bytes)
    I tried:
    Code:
    fseek(file1,0,SEEK_END);
    ch=fgetc(file1)
    but obviously there's something wrong with it...

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    your fseek sets the file pointer at the end of file. It means - AFTER the last byte.

    Do you really hope to read something there?

    Step 1 byte to the beginning to read it...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    then
    Code:
            int i=1;
    	char ch;
    	do{
    
    		fseek(file1,i,SEEK_END);
    		ch=getc(file1);
    		fputc(ch,file2);
    		i+=1;
    
    	}while(WHATs the CONDITION here)
    thanks

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    when i is > 0 - you are trying to go beyond the end-of-file, so you moving in the wrong direction

    use ftell to determine how many bytes you need to read
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    thank you i got it working
    only, it prints these characters in my output file before the actual text. :

    I'm thninking it has sth to do with unix EOF or CR :-??

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by liaa View Post
    thank you i got it working
    only, it prints these characters in my output file before the actual text. :

    I'm thninking it has sth to do with unix EOF or CR :-??
    Could you post the actual code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    Code:
    #include<stdio.h>
    
    int main( int argc, char* argv[])
    {
    	FILE* file1= fopen(argv[1],"r");
    	FILE* file2= fopen("output","w");
    	if((file1==NULL)||(file2==NULL))
    		printf("open error!\n");
    	else {
            int i=2,pos;
    	char ch;
    	fseek(file1,0,SEEK_END);
    	pos=ftell(file1);
    	
            while(pos>0)
    	{		
    		ch=getc(file1);
    		fputc(ch,file2);
    		pos--;
    		fseek(file1,i,SEEK_END);
    		i--;
    	}
    	fclose(file1);
    	fclose(file2);
    }
    	return 0;
    
    }

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    my mistake again should have initialized i= -2 ;
    and first
    fseek(file1,-1,SEEK_END);

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, a few corrections needed:
    1. Start i with -1 (since "end-of-file-1" is one character from the end).
    2. Move fseek() to BEFORE the fgetc(). Otherwise, you read the FIRST character first, then jump to the end.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21