Thread: fseek

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    fseek

    I am trying to print in reverse all characters in a file. I want to use fseek to do so. Here are my codes..but can't get it to work!!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	FILE *ifp;
    	int k;
    		
        if((ifp=fopen("hello.txt","rb"))==NULL)
    	{
    		perror("fseek1");
    		exit(1);
    	}
    	
    	fseek(ifp,0L,SEEK_END);
    	for (k=0;k<sizeof(ifp);k++)
    		printf("%c",ifp);
    
    	close(ifp);
    
      return 0;
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    In your code, the file-pointer doesn't change. You could try to replace the filepointer. Perhaps something like

    ifp = ifp - 1;

    Or you could use fseek to replace:

    fseek (ifp, i, SEEK_END);

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    ifp = ifp - 1;
    *Youch* That is definitely NOT valid. You can't change the file pointers value (address) thinking it would reposition the position in the file. The file pointer points to a place in memory where the file data is stored, it doesn't point to the file itself.
    I don't know if there is any good/easy way to traverse a file backwards, but you could try this:
    Code:
    FILE* File;
    
    ...
    
    fseek(File, 0, SEEK_END);
    int Position = ftell(File);
    
    while(Position >= 0)
    {
       fseek(File, Position, SEEK_SET)
    
       // Read data using fread();
    
       Position--;
    }
    
    ...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>printf("%c",ifp);
    That won't work either, I'm afraid. It will not get a character from ifp and print it out.

    Lookup fgetc(), but it won't be very efficient, you'll spend most of your time repositioning the file pointer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    Code:
    void revers(FILE *f){
    int n=0;
    if((f=fopen("f.dat","r"))==NULL)
    printf("sorry no memory\n");
    else{
    fseek(f,0,SEEK_END);
    n=ftell(f)/size//the size of file
    // n count of file
    for(i=1;i<=n;i++)
    fsee;(f,-pos(i-1),SEEK_END);
    fread(--------------------
    printf(------------------
    }
    fclose(f);
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What was the point of bumping a year old thread?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. question about fseek
    By gp364481 in forum C Programming
    Replies: 6
    Last Post: 11-07-2008, 01:45 PM
  3. fseek question
    By blondie.365 in forum C Programming
    Replies: 19
    Last Post: 10-20-2008, 04:30 PM
  4. fseek() changing an unrelated variable?
    By aaronvegh in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 02:30 PM
  5. popen and fseek
    By mach5 in forum C Programming
    Replies: 4
    Last Post: 11-29-2003, 02:03 AM