如何在C中减少FILE*指针

Eng*_*ine -2 c

我正在尝试在C中减少一个FILE指针,但是我没有完成它,这是我的代码:

#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);
}
Run Code Online (Sandbox Code Playgroud)

任何想法的想法我怎么能这样做?

UPDATE

我改变了基于doc的代码fseek:

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);
}
Run Code Online (Sandbox Code Playgroud)

程序不会崩溃,但它会显示一个错误的temp值

0.000000
Run Code Online (Sandbox Code Playgroud)

知道我在这里做错了什么吗?

Ale*_*lds 5

用于fseek()在使用之前将文件偏移量移动到所需的字节fread().

您可以从文件的开头,结尾或文件偏移当前的任何位置进行搜索.我链接的文档解释了如何.

  • @Engine:RTFM !!! 你甚至得到了链接.我们可以在这里复制并粘贴该文本吗? (6认同)