1

I'm trying to reverse a file line by line using recursion. So basically an input of. I am looking for a way to possibly get started on this without using any pointers or arrays. If you can point me in the right direction that would perfectly fine.

dogs
cats

would have a output of

sgod
stac

This is how my source code looks like so far but I'm stuck just reversing the entire text file (via command line < ) rather than the line.

#include <stdio.h>

void Recursion();
int main (int argc, char argv)
{
  Recursion();
  printf("\n");
}

void Recursion()
{
  int c;
  while((c = getchar()) != EOF)
    {
      Recursion(c);
      printf("%c",c);
    }
}
Dnlfgby
  • 69
  • 9
  • 2
    Then read a line, reverse it, read the next one and reverse it, then read the next one and reverse it, .... keep doing that until there are no more lines to read. Look up `fgets` if you want to read a line. – Pablo Jun 23 '17 at 01:46
  • @Stargateur C programs should not depend on non-`(void)` parameter-lists as a way of passing values. Parameters should be explicitly declared. – Dai Jun 23 '17 at 01:50
  • 4
    This looks [very familiar](https://stackoverflow.com/questions/44709992/reverse-each-specific-line-in-a-string), right down to the erroneous function prototype with no argument in parenthesis.... – ad absurdum Jun 23 '17 at 01:50
  • @Dai Compiles just fine from my end, and recursion is gettings it's char from getchar(). – Dnlfgby Jun 23 '17 at 01:50
  • @Stargateur I know. I mistakenly compiled this as C++ originally instead of C which gave me the error. I've deleted my comment. – Dai Jun 23 '17 at 01:53
  • It may compile, but OP does not understand what the empty parenthesis mean. `Recursion(c);`-- this call is meaningless since the definition contains no parameters. – ad absurdum Jun 23 '17 at 01:54
  • Oh i see what you mean @David – Dnlfgby Jun 23 '17 at 01:57
  • 4
    The first step is to turn off your computer. Next, get a pencil and paper and explain **in words** the steps needed to solve the problem. – Code-Apprentice Jun 23 '17 at 01:57
  • 1
    Possible duplicate of [Reverse Each Specific Line in a String](https://stackoverflow.com/questions/44709992/reverse-each-specific-line-in-a-string) – Dai Jun 23 '17 at 01:59
  • 1
    The declaration with no parameters may not be the fundamental problem here, but if you are interested you can read about this obsolescent feature of C [here](https://stackoverflow.com/questions/13950642/why-does-a-function-with-no-parameters-compared-to-the-actual-function-definiti) and [here](https://stackoverflow.com/questions/18167390/what-does-an-empty-parameter-list-mean). – ad absurdum Jun 23 '17 at 02:00
  • @Code-Apprentice Thanks, but I've been trying to figure this out for a couple hours now and the only thing that comes to mind is using pointers & arrays which would seem like the fundamental way of doing it, right? I am leaning against that right now. – Dnlfgby Jun 23 '17 at 02:05
  • "the only thing that comes to mind is using pointers & arrays" I think that part of the problem is that you are focusing too much on programming concepts. For now you should ignore such technical details as "pointers" and "arrays". Instead, describe a step-by-step solution in plain English (or whichever human language you use most comfortably). – Code-Apprentice Jun 23 '17 at 02:19
  • Think I figured it out, thank you, everyone, I'll post my solution after I make sure. – Dnlfgby Jun 23 '17 at 03:00
  • in C, the `main()` function has two valid signatures: `int main( int argc, char *argv[] )` and `int main( void )` Neither of those signatures match the signature of the `main()` function in the posted code. Note: when the `main()` function parameters are not going to be used, then use the `int main( void )` signature – user3629249 Jun 23 '17 at 18:59

2 Answers2

1

First, you must make sure that your function "Recursion" can work to a single line. Your function "Recursion" may be declared like this:

//source must have pointed to storage space of the line and the 
//storage space of line must be not read-only. As we know "dogs" is  
// a constant value you can't change.

int Recursion(char *source,int length);

Inputting the string "dogs" to the Recursion, the output must be the "sgod".

Then, you can read each line of the file and input them to this function Recursion so that you can get the result you want.

I think the function Recursion may have bugs or you are failed to read line from the file. If you can give me detail code about function Recursion or the module of reading file, it is easy to figure out the error.

You are using the function getchar() instead of reading line from the file. So 'EOF' is not right and should be '\n'. You also should't using while in Recursion because it is already a recursion function. So the right code is following:

void Recursion()
{
  int c;
  if((c = getchar()) != '\n')
  {
     Recursion(c);
     printf("%c",c);
  }
  else
     return;
}

I think your code is not good. In my opinion, the function to reverse the string does't need Recursion and just likes this:

int Recursion(char *source,int length);

so that you can divide the program into separate modules---read line and reverse. Thus the main function just likes this:

int main()
{
    int fd = open();

    while(1)
    {
       // 1.read line
       ....
       //2. is EOF?
       break;
       //3. reverse line
       Recursion(line,strlen(line));
    }
}

I hope that can help you.

WangYang
  • 466
  • 1
  • 5
  • 15
  • The code for the function `Recursion()` is right there in the question post! – ad absurdum Jun 23 '17 at 02:07
  • In this question you are not reading string from file. So the function should like this. I have fix my answer. – WangYang Jun 23 '17 at 02:15
  • The program is reading from `stdin`, which could be a file if `stdin` has been redirected. – ad absurdum Jun 23 '17 at 02:23
  • I see. if you want to use the EOF flag, you should press the Ctrl+D to produce the EOF in stdin. – WangYang Jun 23 '17 at 02:31
  • You can redirect files from the command line. In Linux, invoke the program with `./a.out < input_file.txt` to make `stdin` read from the file. – ad absurdum Jun 23 '17 at 02:35
  • Yes, I think you are a fresh man before. actually you are good at linux. I like linux too. haha. Just replace the "while" with the "if", your Recursion will be ok. – WangYang Jun 23 '17 at 02:40
  • If stack overflow has a chat room, which is great for us to communicate. Actually I have nothing to do and am boring. Haha – WangYang Jun 23 '17 at 02:42
0

If you really want to do this with recursion than do this:

#include <stdio.h>

int recursion() {
    int c = getchar();
    if(c != '\n' && c != EOF) {
        int n = recursion();
        printf("%c", c);
        return n;
    } else {
        return c;
    }
}

int main (void) {
    while(recursion() != EOF) printf("\n");
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278