2

I want to ask is that there is a function to read text file from bottom up in C ?

Exp: text content is abcdef --> we will get fedcba.

If there is no such function like that,I'm thinking about pass text content into an array,then reverse the array,is that ok ? Do you have better solution for this question :)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Duc Anh
  • 581
  • 1
  • 9
  • 23
  • possible duplicate of [Read a file backwards?](http://stackoverflow.com/questions/10813930/read-a-file-backwards) – Andreas Fester Mar 25 '13 at 14:01
  • Wait, this isn't a duplicate. The other question is reading it line by line backwards. – Matt K Mar 25 '13 at 14:03
  • @mkb Whats the difference between reading a file char by char to find the newline character or printing the character? At least it gives a bunch of possible solutions, like `fseek`, `memory mapped file` ... – Andreas Fester Mar 25 '13 at 14:05
  • @Andreas Well, the accepted answer to the linewise question is only applicable to C++, but the others are great starting points, yes. – Matt K Mar 25 '13 at 14:07
  • Right - probably "duplicate" is not quite correct, but at least OP could have found these starting points through a simple google query ;) – Andreas Fester Mar 25 '13 at 14:09

4 Answers4

1

There is no function in the standard library to do this. You could pass the data into a mutable char array and perform an in-place reverse.

This obviously doesn't read the file from the bottom to the top, it reads from beginning to end and uses O(N) time and O(1) space complexity. You could play around with the seek position and attempt to read backwards; it would be interesting to see the performance.

Just for additional info

See http://www.geeksforgeeks.org/an-in-place-algorithm-for-string-transformation/ for complexity analysis

ed_me
  • 3,338
  • 2
  • 24
  • 34
1

The following code might serve your purpose,

char a[MAX];
int flag=1,i=0;



fseek(fp, 0, SEEK_END); 
while(flag>0)
{
    a[i]=fgetc(fp);
    i++;
    if(fseek(fp,-2,SEEK_CUR)==-1)
    {
       flag=0;
    }
}
Deepu
  • 7,592
  • 4
  • 25
  • 47
  • do you have link of a good tutorial about fseek,I know nothing about fseek so I cant understand your code :( – Duc Anh Mar 25 '13 at 14:30
  • 1
    fseek sets the file pointer fp to the location pointed by the second and third parameter. Try this link http://beej.us/guide/bgc/output/html/multipage/fseek.html – Deepu Mar 25 '13 at 14:33
1

you can develop your own function that reads from bottom using fseek() standard function:

char *my_read(FILE *fp)
{
    int i, size;
    char *buffer;

    fseek(fp, 0, SEEK_END);
    size = ftell(fp);

    buffer = malloc((size+1) * sizeof(char));

    for (i=0; i<size; i++)
    {
        fseek(fp, size-1-i, SEEK_SET);
        buffer[i] = fgetc(fp);
    }
    buffer[size] = 0;
    return buffer;
}
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
1

You can develop your own function that read from beginning but start saving in the array from the bottom.

This function will:

  1. get the size of the file
  2. allocate char buffer with (size + 1)
  3. read the file from the beginning and at the same time start filling the char array from the end

It's more simple than using fseek each time you read

char *my_read(FILE *fp)
{
    int i, size;
    char *buffer;

    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    buffer = malloc((size+1) * sizeof(char));

    for (i=(size-1); i>=0; i--)
    {
        buffer[i] = fgetc(fp);
    }
    buffer[size] = 0;
    return buffer;
}
MOHAMED
  • 41,599
  • 58
  • 163
  • 268