Thread: Textfile Pointers

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    28

    Textfile Pointers

    When using a pointer for a text file to point at either the next char, string, or line etc

    How is it posible to initiate the pointer at EOF and for it to start counting backwards?

    ive tried things such as

    Code:
    FILE *stream == EOF
    
    counter--
    But it just doesnt seem to work

    Much appreciated.
    Cheers

    Bazza

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Lookup fseek(), ftell() etc
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How is it posible to initiate the pointer at EOF and for it to start counting backwards?
    Why exactly do you want to do this?

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Basically I want to pointer to start at the end of the file and count backwards so i can print a text file in revese character and reverse line order

    anyone off to look up fseek and ftell

    thanx for ur interest
    Cheers

    Bazza

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so i can print a text file in revese character and reverse line order
    There are a number of ways to do this, but fseek is virtually useless for text streams. You might want to consider an alternate method such as reading the file into a buffer and printing the buffer in reverse or recursion:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    static void pr_revLine ( FILE *fp )
    {
      char buf[BUFSIZ] = {'\0'};
      if ( fgets ( buf, (int)sizeof buf, fp ) != NULL ) {
        buf[strcspn(buf, "\n")] = '\0';
        pr_revLine ( fp );
        (void)puts ( buf );
      }
    }
    
    static void pr_revChar ( FILE *fp )
    {
      int ch;
      if ( ( ch = fgetc ( fp ) ) != EOF ) {
        pr_revChar ( fp );
        (void)putchar ( ch );
      }
    }
    
    int main ( void )
    {
      FILE *fp;
      fp = fopen ( "input.txt", "r" );
      if ( fp != NULL ) {
        pr_revLine ( fp );
        rewind ( fp );
        pr_revChar ( fp );
        (void)fclose ( fp );
      }
      (void)putchar ( '\n' );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    I havent done most of the error checking (return value of scanf , ferror etc) , this might be a way to do it for moderately sized files
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void fprintrev(FILE *fp)
    {
    	int c;
    	c=fgetc(fp);
    	if(c==EOF) return;
    	fprintrev(fp);
    	putchar(c);
    }
    
    int
    main(void)
    {
    	FILE *fp;
    	char fname[300];
    	printf("Give file name:");
    	scanf("%s",fname);
    	fp=fopen(fname,"r");
    	if(fp==NULL)
    	{
    		perror(NULL);
    		return 1;
    	}
    	fprintrev(fp);
    	return 0;
    }
    The one who says it cannot be done should never interrupt the one who is doing it.

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    I forgot fclose
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void fprintrev(FILE *fp)
    {
    	int c;
    	c=fgetc(fp);
    	if(c==EOF) return;
    	fprintrev(fp);
    	putchar(c);
    }
    
    int
    main(void)
    {
    	FILE *fp;
    	char fname[300];
    	printf("Give file name:");
    	scanf("%s",fname);
    	fp=fopen(fname,"r");
    	if(fp==NULL)
    	{
    		perror(NULL);
    		return 1;
    	}
    	fprintrev(fp);
            fclose(fp); 
    	return 0;
    }
    The one who says it cannot be done should never interrupt the one who is doing it.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Thanx for that guys but prelude I think that was more what I was looking for.

    But I wouldnt mind understanding the code coz im not gr8 at c

    [Code]
    #include <stdio.h>
    #include <string.h>

    static void pr_revLine ( FILE *fp )
    {
    char buf[BUFSIZ] = {'\0'}; /* what is {'\0'} used for */
    if ( fgets ( buf, (int)sizeof buf, fp ) != NULL ) /* get each line till EOF*/
    {
    buf[strcspn(buf, "\n")] = '\0'; /* unsure */
    pr_revLine ( fp ); /* unsure why needed*/
    (void)puts ( buf ); /*outputting the file */
    }
    }

    static void pr_revChar ( FILE *fp ) how come the fp is declared within these brackets
    {
    int ch;
    if ( ( ch = fgetc ( fp ) ) != EOF ) { /* unsure about the command that tell the pointer to step back one
    pr_revChar ( fp );
    (void)putchar ( ch );
    }
    }

    int main ( void )
    {
    FILE *fp; /* File pointer*/
    fp = fopen ( "input.txt", "r" ); /* open file read */
    if ( fp != NULL ) /* if file pointer does reach end of file */
    {
    pr_revLine ( fp ); /* call pr_revline*/
    rewind ( fp ); /* Is this rewind pointer to start of file???*/
    pr_revChar ( fp ); /* same as above */
    (void)fclose ( fp ); /* close file */
    }
    (void)putchar ( '\n' ); /* whats this doing??? */
    return 0;
    }

    [\Code]

    thanx for your help really appreciated but im trying my hardest to learn about C so some insight would be appreciated

    thanx again
    Cheers

    Bazza

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >/* what is {'\0'} used for */
    It clears the array to all nul characters.

    >/* unsure */
    fgets will stop at a newline, but it includes this newline in the buffer. This line of code replaces the newline character with a nul for easier handling.

    >/* unsure why needed*/
    This is a recursive function, you read the first line and then call the function again which reads the second line. The function will do this until it reaches EOF and then it goes back through the function calls, printing each line as it goes. The end result is that the file is printed in reverse:
    Code:
    read line 1
    recurse
      read line 2
      recurse
        read line 3
        recurse
          read EOF
          return
        print line 3
        return
      print line 2
      return
    print line 1
    return
    >how come the fp is declared within these brackets
    I declared the FILE * to be used and opened the file in main. So that I didn't need to use a global variable, I chose to pass this to each function.

    >unsure about the command that tell the pointer to step back one
    You don't, trust in the recursive nature of the function and it will work.

    >/* if file pointer does reach end of file */
    No, if the file could not be opened.

    >/* Is this rewind pointer to start of file???*/
    Yes. The pr_revLine will take the file pointer to the end of the file. For pr_revChar to work the file pointer has to be sent back to the beginning.

    >/* whats this doing??? */
    It puts further output onto the next line for whatever you are going to do next.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Thanx prelude you have been a great help I really appreciat the amount of help you have given me im not sure if you said it above but i didnt see it

    Im unsure about this line as i do not recgonise the strcspn
    [code]
    buf[strcspn(buf, "\n")] = '\0';
    [\code]

    Thanx again
    Cheers

    Bazza

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    [CODE]

    if ( ( ch = fgetc ( fp ) ) != EOF )

    [\CODE]

    Also unsure of this part

    cheers
    man im dumb
    Cheers

    Bazza

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strcspn

    "The strcspn() function computes the length of the maximum initial segment of the string pointed to by s1 that consists entirely of characters that are not in the string pointed to by s2. "

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Bazza
    [CODE]

    if ( ( ch = fgetc ( fp ) ) != EOF )

    [\CODE]

    Also unsure of this part

    cheers
    man im dumb
    1) You're using the wrong slash. Use / instead of \

    2) The code can be broken down as follows:

    ch = fgetc( fp ); //get a character from file
    if( ch != EOF )
    ... do something ...

    Or, as you see it, contained on a single line. Do you understand the usage of parenthesis in math? The same applies for code:

    if ( (this) == somethingelse ) ...

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    how about using
    fseek(fp,-1,SEEK_END)?

  15. #15
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Originally posted by lambs4
    how about using
    fseek(fp,-1,SEEK_END)?
    fseek(fp, -1, SEEK_CUR);
    As lambs4 said, that's a nice way of going about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM