读取文件时,如何跳过第N行
In file reading, how to skip the Nth first lines
我编写了一个程序来从文件 (input.xyz) 中读取分子的 X、Y 和 Z 坐标并执行一些任务。然而,
我希望我的程序跳过前两行作为我的输入文件
具有以下结构:
3
water
O -0.73692879 -1.68212007 -0.00000000
H 0.03427635 -1.68212007 -0.59075946
H -1.50813393 -1.68212007 -0.59075946
我在我的代码中使用了以下部分
fptr = fopen(filename, "r");
fseek(fptr,3,SEEK_SET);
for(i=0;i<Atom_num;i++)
{
X[i] = Y[i] = Z[i] = 0;
fscanf(fptr,"%2s%lf%lf%lf",Atom[i].symbol,&X[i],&Y[i],&Z[i]);
printf("%2s\t%lf\t%lf\t%lf\n",Atom[i].symbol,X[i],Y[i],Z[i]);
}
fclose(fptr);
其中Atom_num是input.xyz的第一行
然而,printf 显示如下输出
at 0.000000 0.000000 0.000000
er 0.000000 0.000000 0.000000
O -0.736929 -1.682120 -0.000000
我不知道为什么 fseek() 不工作。
谁能帮我解决这个问题?
查看fseek()的签名:
int fseek(FILE *stream, long int offset, int whence)
尤其是在 offset
的定义中
offset − This is the number of bytes to offset from whence.
所以当你这样做时:
fseek(fptr,3,SEEK_SET);
您只是跳过了输入文件中的 3 个字节。
你想做的是这样的:
char line[256]; /* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL) /* read a line */
{
if (count == lineNumber)
{
//Your code regarding this line and on,
or maybe just exit this while loop and
continue reading the file from this point.
}
else
{
count++;
}
}
-- fgets() 方法--
这可以用 fgets 来完成。
来自 man7.org:请参阅此处 fgets
fgets()
函数应从 stream
读取 bytes
到数组中
由 s
指向,在我们的例子中由 line
指向,直到 n−1 bytes
被读取,或者 <newline>
被读取并传输到 line
,或者 end-of-file, EOF
遇到条件。然后字符串以 null
字节终止。
#include <stdio.h>
#define LINES_TO_SKIP 3
#define MAX_LINE_LENGTH 120
int main () {
FILE *fp;
char line[MAX_LINE_LENGTH]; /*assuming that your longest line doesnt exceeds MAX_LINE_LENGTH */
int line_c = 0;
/* opening file for reading */
fp = fopen("file.txt" , "r");
if(fp == NULL)
{
perror("Error opening file");
return(-1);
}
while(( fgets (line, MAX_LINE_LENGTH, fp)!=NULL )
{
if(line_c < LINES_TO_SKIP)
{
++line_c;
puts("Skiped line");
}
else
{
/*
... PROCESS The lines...
*/
}
}
fclose(fp);
return(0);
}
我编写了一个程序来从文件 (input.xyz) 中读取分子的 X、Y 和 Z 坐标并执行一些任务。然而, 我希望我的程序跳过前两行作为我的输入文件 具有以下结构:
3
water
O -0.73692879 -1.68212007 -0.00000000
H 0.03427635 -1.68212007 -0.59075946
H -1.50813393 -1.68212007 -0.59075946
我在我的代码中使用了以下部分
fptr = fopen(filename, "r");
fseek(fptr,3,SEEK_SET);
for(i=0;i<Atom_num;i++)
{
X[i] = Y[i] = Z[i] = 0;
fscanf(fptr,"%2s%lf%lf%lf",Atom[i].symbol,&X[i],&Y[i],&Z[i]);
printf("%2s\t%lf\t%lf\t%lf\n",Atom[i].symbol,X[i],Y[i],Z[i]);
}
fclose(fptr);
其中Atom_num是input.xyz的第一行
然而,printf 显示如下输出
at 0.000000 0.000000 0.000000
er 0.000000 0.000000 0.000000
O -0.736929 -1.682120 -0.000000
我不知道为什么 fseek() 不工作。 谁能帮我解决这个问题?
查看fseek()的签名:
int fseek(FILE *stream, long int offset, int whence)
尤其是在 offset
的定义中offset − This is the number of bytes to offset from whence.
所以当你这样做时:
fseek(fptr,3,SEEK_SET);
您只是跳过了输入文件中的 3 个字节。
你想做的是这样的:
char line[256]; /* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL) /* read a line */
{
if (count == lineNumber)
{
//Your code regarding this line and on,
or maybe just exit this while loop and
continue reading the file from this point.
}
else
{
count++;
}
}
-- fgets() 方法--
这可以用 fgets 来完成。 来自 man7.org:请参阅此处 fgets
fgets()
函数应从 stream
读取 bytes
到数组中
由 s
指向,在我们的例子中由 line
指向,直到 n−1 bytes
被读取,或者 <newline>
被读取并传输到 line
,或者 end-of-file, EOF
遇到条件。然后字符串以 null
字节终止。
#include <stdio.h>
#define LINES_TO_SKIP 3
#define MAX_LINE_LENGTH 120
int main () {
FILE *fp;
char line[MAX_LINE_LENGTH]; /*assuming that your longest line doesnt exceeds MAX_LINE_LENGTH */
int line_c = 0;
/* opening file for reading */
fp = fopen("file.txt" , "r");
if(fp == NULL)
{
perror("Error opening file");
return(-1);
}
while(( fgets (line, MAX_LINE_LENGTH, fp)!=NULL )
{
if(line_c < LINES_TO_SKIP)
{
++line_c;
puts("Skiped line");
}
else
{
/*
... PROCESS The lines...
*/
}
}
fclose(fp);
return(0);
}