-2

I'm trying to decrement a FILE pointer in C , but I don't get it done, here is my code:

#include <math.h>
#include <stdio.h>
#include <complex.h>

int main() {
    FILE* inputFile = NULL;
    double* inputData = NULL; 
    unsigned int windowSize = 512; 
    unsigned int index1 = 0, index2 = 0, i = 0;
    double temp = 0.0;

    // mememory allocation
    inputData = (double*) malloc(sizeof(double)*windowSize);

    // Opning files 
    inputFile = fopen(" file","rb");

    if (inputFile == NULL) {
        printf("Couldn't open either the input or the output file \n");
        return -1;
    }
    while((i=fread(inputData,sizeof(double),windowSize,inputFile))==windowSize){   
        for (index1 =0; index1 < windowSize;index1++) {
            printf("index %d \t %d    %d\t %lf\n",index1,index2,i,inputData[index1]);
            fprintf(outputFile,"%lf ",inputData[index1]);
        }
        index2++;
    }
    // I need to start read from the end of the file - the value of i
    printf( " the last i %d \n", i);
    inputFile-i;
    while (!(feof(inputFile))) {  // the program doesnt'T get into the loop
        fread(&temp,sizeof(double),1,inputFile);
        printf("\n\n %lf \n",temp);
    }
    fclose(inputFile);
}

any idea idea how may I do this?

UPDATE

I ve changed the code based on doc of fseek like :

i=i*-1;

printf( " the last i %d \n", i);

fseek(inputFile ,i*sizeof(double),SEEK_END);
while(( fread(&temp,sizeof(double),1,inputFile)==1 )) {
    printf(" %lf \n",temp);
}

the program doesn't crash but it shows a one wrong value of temp

0.000000

any idea what I'm doing wrong here?

mafso
  • 5,433
  • 2
  • 19
  • 40
Engine
  • 5,360
  • 18
  • 84
  • 162
  • 3
    you don't "decrement" or "increment" filehandles. It's literally just an int used to lookup stuff in an internal-only array somewhere. If you want to move where that filehandle is pointing at in a file, then use `fseek()` – Marc B Jul 30 '14 at 19:58
  • 2
    Also `inputFile-i;` doesn't do anything. – Colonel Thirty Two Jul 30 '14 at 20:00
  • well to be honest , I didn't know I can do this, it was a disparate try to do this, how can I use fseek to jump to the end-X , if I don'T know how long is the file, and how x may be – Engine Jul 30 '14 at 20:01
  • See [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler Jul 30 '14 at 20:05
  • 1
    And have a look at the `fseek` documentation. – mafso Jul 30 '14 at 20:06
  • @mafso I'ce update my question ! – Engine Jul 30 '14 at 20:43
  • What is your input? Is it only printing one value before stopping? Or does the output include a value you weren't expecting? Or is it printing a value that isn't in the input? You should do more work to clarify your revised question to help others help you. – Alex Reynolds Jul 30 '14 at 20:59
  • 1
    if you want to "increment/decrement" there is memmap – technosaurus Jul 30 '14 at 21:15
  • `mmap` still requires knowing the file size (which can be obtained from an `fstat()` call) to specify the bounds from the end of the file. The `fseek()` call allows seeking from the file's end more directly. Additionally, `mmap` will usually be slower than `fseek`, if the program makes only one pass through the file. – Alex Reynolds Jul 30 '14 at 21:24
  • @AlexReynolds - I'd love to see any data supporting your argument that fseek is faster than mmap. My experience and every benchmark I have seen says otherwise except where the OS uses a mapping underneath the covers for *`read`/*`write` (windows does, but the only portable way is to use mmap directly * http://stackoverflow.com/a/46005/1162141 ) – technosaurus Jul 30 '14 at 22:45
  • Test it yourself--you'll see that a single pass is faster with C I/O calls, while multiple passes are faster with `mmap`. – Alex Reynolds Jul 31 '14 at 02:46

1 Answers1

5

Use fseek() to move the file offset to the desired byte, before using fread().

You can seek from the start of the file, the end, or from wherever the file offset is currently. The documentation I linked to explains how.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • 1. Thanks 2.is there a way to use fseek to jump to the position END -X ? if I don't know the how big the file is, which mean that I don't know how X will be ? – Engine Jul 30 '14 at 20:07
  • 6
    @Engine: RTFM!!! You've even got the link. Shall we copy-and-paste that text here? – mafso Jul 30 '14 at 20:09
  • @Engine: The third argument to `fseek()` can be `SEEK_SET`, `SEEK_CUR`, or `SEEK_END`. Guess which one you need? The second argument is a signed quantity, so it can be positive or negative (though a negative offset from the start won't get you anywhere). – Jonathan Leffler Jul 30 '14 at 20:18