[英]How I do read the last line of a file?
I have a file like this from which I need some values from its last line. 我有一个像这样的文件,从中我需要最后一行的一些值。 This is the file:
这是文件:
XFOIL Version 6.96 Calculated polar for: pane 1 1 Reynolds number fixed Mach number fixed xtrf = 1.000 (top) 1.000 (bottom) Mach = 0.000 Re = 0.100 e 6 Ncrit = 4.000 alpha CL CD CDp CM Top_Xtr Bot_Xtr ------ -------- --------- --------- -------- -------- -------- 0.000 0.3882 0.01268 0.00440 -0.0796 0.6713 1.0000
What I want to do is to read the values of alpha
, CL
and CD
located in the last line. 我想做的是读取位于最后一行的
alpha
, CL
和CD
的值。
I use this code 我用这个代码
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE * pFile;
FILE * test1;
char ch;
double alpha,lift,drag;
int i;
pFile = fopen("save.txt","r");
test1 = fopen("test1.txt","w");
fseek ( pFile , 434 , SEEK_SET );
while( ( ch = fgetc(pFile) ) != EOF ){
fputc(ch, test1);
}
for(i = 0; i < 3; i++)
{
fscanf(test1, "%lf ",&alpha);
fscanf(test1, "%lf ",&lift);
fscanf(test1, "%lf",&drag);
}
printf("alpha = %lf cl = %lf cd = %lf",alpha,lift,drag);
fclose(test1);
fclose ( pFile );
return 0;
}
Thank you in advance... 先感谢您...
Guys thank you all for your answers what i forgot
to mention is that it prints out that alpha = 0.00000 cl = 0.00000 cd = 0.00000 which actually are non zero but 0.000 0.3882 0.01268 respectivelly...!! 值得一提的是它打印出alpha = 0.00000 cl = 0.00000 cd = 0.00000实际上不是零,而是分别为0.000 0.3882 0.01268 ...
pFile = fopen("save.txt","r");
test1 = fopen("test1.txt","w");
You are opening save.txt
for reading, and test1.txt
for writing. 您正在打开
save.txt
进行阅读,并打开test1.txt
进行写作。
fseek ( pFile , 434 , SEEK_SET );
while( ( ch = fgetc(pFile) ) != EOF ){
fputc(ch, test1);
}
You are now skipping to character 434 in save.txt
, and then reading the rest of the file, printing each character out into test1.txt
. 现在,您
save.txt
字符434,然后读取文件的其余部分,将每个字符打印到test1.txt
。
for(i = 0; i < 3; i++)
{
fscanf(test1, "%lf ",&alpha);
fscanf(test1, "%lf ",&lift);
fscanf(test1, "%lf",&drag);
}
You are now trying to read from test1.txt
, but it is open for writing, and the current position is at the end of the file. 现在,您正在尝试从
test1.txt
读取数据,但是该文件可以进行写入操作,并且当前位置位于文件末尾。 If you want to read it, you will need to either close it and open it for reading, or open it read-write ( fopen(..., "rw")
) up above and then reset the current position to the beginning of the file before starting to read (it is undefined what will happen if you don't do that). 如果要阅读,则需要将其关闭并打开以进行阅读,或者将其向上以读写方式打开(
fopen(..., "rw")
),然后将当前位置重置为以下位置:开始读取之前的文件(如果不执行该操作,将会不确定)。
In fact, you shouldn't need to skip to a byte offset, copy the last line into a different file, and then read that new file. 实际上,您不需要跳过字节偏移量,将最后一行复制到另一个文件中,然后读取该新文件。 You can just read the last line directly from the original file.
您可以直接从原始文件中直接读取最后一行。 No need for that loop that reads from one file into another;
不需要从一个文件读入另一个文件的循环。 just run your
scanf()
on the original file. 只需在原始文件上运行
scanf()
。
Remember to check your function calls for errors. 请记住检查您的函数调用是否有错误。 The
scanf()
calls that you made probably returned an error. 您进行的
scanf()
调用可能返回了错误。 You can check for an error with ferror(file)
, and get the actual error message with strerror(errno)
. 您可以使用
ferror(file)
检查错误,并使用strerror(errno)
获取实际的错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.