我正在尝试在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)
知道我在这里做错了什么吗?
| 归档时间: |
|
| 查看次数: |
1192 次 |
| 最近记录: |