I have problem reading file form command line.
I am trying to do " abcd: ./rev < numbers", but it gives me an error:
./rev < numbers
Usage :
./rev FILENAME
Here is the code:
#include <stdio.h>
#include <stdlib.h>
void reverse(FILE * file)
{
int fscanf_return_value;
char x;
/* read a char */
fscanf_return_value = fscanf(file,"%c",&x) ;
if(fscanf_return_value == EOF)
{ //fscanf returns EOF as the END OF FILE
return;
}
reverse(file); //Get the next char
//do something with the char e.g. print
putchar(x);
return;
}
int main(int argc, char *argv[])
{
int i;
FILE *fd;
if (argc!=2)
{
printf("Usage : \n %s FILENAME\n",argv[0]);
exit(0);
}
if(!(fd=fopen(argv[1],"r")))
{
printf("Opening file error\n");
exit(1);
}
reverse(fd);
printf("\n\n\t---\tenoD - Done\t---\n");
exit(0);
}
Also I can't read a file one per line, and prints them back out in reverse order. I need output like:
.
.
.
12
11
10
9
8
7
6
5
4
3
2
1
0
But I am getting:
21
11
01
9
8
7
6
5
4
3
2
1
0