Hi
first of all I didn't write this thread till I tried million times with this piece of code but still I'm not getting the desired results.
I have a textfile and i want to exchange the lines,
ex:
there
Hi
to:
Hi
there
I'm not trying to edit the same file, I'm trying just to print the wanted result to the screen
here is my code and tell me what's wrong (no compiling error or warnings)
Code:
void revlines(int my_fd)
{
int r=0; // to be the index of the end of a new line
int s=0;
int l=0; // to be the index of the beginning of the line
char line[600];
char buf[600];
int j=0;
int i=2;
lseek(my_fd,-2,SEEK_END);
do
{
read(my_fd,buf,1);
line[j]=buf[0];
j++;
i++;
if(line[j]=='\n')
{ l=r;
r=j;
for(s=r+1;s>l-1;s--)
printf("%c",line[s]);
}
}while(lseek(my_fd,-i,SEEK_END)!=-1);
it just prints the last line at first(which is good) then stops working, even if there's more than 2 lines in the file.
this may be an easy problem for you guys but this is just my second program in c and this is just a tiny bit of it.
Thanks in advance