Recursive Reversing

This is a discussion on Recursive Reversing within the C Programming forums, part of the General Programming Boards category; I try to understand how following code, written to reverse given txt file, works. Can anybody add comments ? Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    Recursive Reversing

    I try to understand how following code, written to reverse given txt file, works. Can anybody add comments ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void reverse_str_print(const char* p, const size_t len)
    {
        const char* pstr;
        if(len > 0 && p != NULL)
        {
            pstr = &p[len -1];
            while(pstr >= p)
            {
                putchar(*pstr--);
            }
        }
    }
    
    int main()
    {
        FILE*   pf;
        char    c;
        char    filename[BUFSIZ];
        long    len;
        size_t  r;
        int     idx = 0;
    
        printf("Enter file to open: ");
        fgets(filename, sizeof(filename), stdin);
        filename[strlen(filename)-1] = '\0';
        if(strlen(filename) > 0)
        {
            pf = fopen(filename, "r");
            if(pf != NULL)
            {
                fseek(pf, 0L, SEEK_END);
                len = ftell(pf);
                while(len > 0)
                {
                    fseek(pf, len -1, SEEK_SET);
                    r = fread(&c, 1, 1, pf);
                    if(r != 0)
                    {
                        switch(c)
                        {
                        case '\n':
                            if(idx > 0)
                            {
                                reverse_str_print(filename, strlen(filename));
                                printf("\n");
                                idx = 0;
                            }
                            break;
                        case '\r':
                            break;
                        default:
                            filename[idx++] = c;
                            filename[idx] = '\0';
                            break;
                        }
                    }
                    --len;
                }
                fclose(pf);
                if(idx > 0)
                {
                    reverse_str_print(filename, strlen(filename));
                    printf("\n");
                }
            }
            else
            {
                printf("Error trying to open file named: %s\n", filename);
            }
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is something you need to do. See what you do and do not understand. Ask questions about specifically what you don't understand.

    Try stepping through the program, and watching some variables. See how they change. Being able to understand good code that you didn't write, is a skill worth developing and practicing.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    about recursion just i can say that in simple words which u can understand i hope that the function will be executed until there will be a terminated condition and accumulate the value which get changed in function on stack

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    What is this talk about recursion?
    There is nothing recursive in that code!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Recursive function for Reversing number?
    By Deshe in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2005, 06:12 PM
  2. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  3. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:27 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

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