用c读取txt文件的第一行 [英] reading first line of a txt file with c

查看:98
本文介绍了用c读取txt文件的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我试图通过fscanf使用格式化的

输入读取空格分隔文件。但令人惊讶的是,我能够读取除第一行之外的所有4999行/ b $ b行数据。它提供随机输出(即文件中不存在
)。代码如下(相关部分)。


来自数据文件的样本输入

01 48554.73828 504 30.48.37.001953 76.15.33.632813

2 48554.67969 504 30.48.36.722168 76.15.33.527344

3 48554.63281 504 30.48.36.442383 76.15.33.421875

4 48554.58594 504 30.48.36.162598 76.15.33.316406
5 48554.54297 504 30.48.35.882813 76.15.33.210938

输出(第一行)

18001844.273438 135633.742190 44.273438 30 5000 14
2 48554.67969 504 30.48.36.722168 76.15.33.527344

3 48554.63281 504 30.48.36.442383 76.15.33.421875

4 48554.58594 504 30.48.36.162598 76.15.33.316406

5 48554.54297 504 30.48.35.882813 76.15.33.210938

之前当我只读前两列

时我得到了这个问题但是你可能有在1解决之前注意到领先的''0'。

令人惊讶的是我仍然可以正确阅读前两列但

以后的那些是掌握的。那也只在第一个

行。休息4999行就读了就好了。我想不明白。 PLZ

help.its重要


shashank

(我使用的是borland c ++ 5.5,win2000)

_______________________________


fp = fopen(" TEMP31.TXT"," r");

if(fp == NULL)

{puts(无法打开文件);退出(1);

}

while((fgetc(fp))!= EOF)

{


fscanf(fp,"%d%lf%d%d。%d。%lf%d。%d。%lf" ,&安培; X [α],&安培; Y [A],&安培; fl_h并[a],&安培; latdeg并[a],&安培; latmin并[a],&安培; latsec并[a],&安培; longdeg并[a],

& longmin [a],& longsec [a]);


printf(" \ n%d%lf%d%d %lf%d%d%lf

%d",x [a],y [a],latdeg [a],latmin [a],latsec [a],longdeg [a], longmin [a],longsec [a],fl_h [a]); //(这里

当我打印它时会给出正确的值)


if ((a%100)== 0){getch();} else {}

a = a + 1;

}


fclose(fp);


getch();


for(i = 0; i< AR; i ++)

{

lat [i] =(latsec [i] +(latmin [i] * 60)+(latdeg [i] * 3600));

lon [i] =(longsec [i] +(longmin [i] * 60)+(longdeg [i] * 3600));

printf(" \ n%lf%lf%lf%d%d

%d",lat [i],lon [i],latsec [i],latmin [i],latdeg [i],fl_h [i]); //从这里

如果((i%100)== 0){getch();}其他{}

}

getch();

_______________________________


这是起始码,即上面给出的代码前面的代码

段,初始化和所有(虽然我不认为它将是

必要但只是为了确定)


#include< stdio.h>

#include< conio.h>

#include< math.h>

#include< stdlib.h>

#define ARRAY 5000

#define AR 4999

#define PI 3.142857

#define CONV 30.8333

main ()

{

clrscr();

FILE * fp;

static int

X [A R],latdeg [AR],latmin [AR],longdeg [AR],longmin [AR],fl_h [AR];

静态双倍

y [AR], longsec [AR],latsec [AR],lat_p [100],long_p [100],lat_m [100];

静态双重

long_m [100],lat [ AR],lon [AR],dist_p [100],dist_m [100],p [100],m [100],hp [100],


hm [100], lata,longa,latb,longb,latp,longp,latm,longm,dist_ f,dist_i,pa,t,ta,tta,tta1,ttta,ttta1;

int a = 0,b = 0,c = 0,d = 0,e = 0,ca = 0,da = 0,f = 0,g = 0,i,j,z,sl = 0,s l1 = 0;

double

sa = 0.0,sa1 = 0,sb = 0.0,sc = 0.0,sd = 0.0,se = 0,sf = 0,sg = 0,s g1 = 0,sh = 0,sh1 = 0,xa = 0,xb = 0,xp = 0,xm = 0,ya,yb,yp = 0,ym = 0;

static float fl_p [100], fl_m [100],fl_hp,fl_hm,fl_ha,fl_hb;

_________________ *********** _______________

解决方案



对不起,正确的样品输出是

18001844.273438 135633.742190 44.273438 30 5000 14

110916.722168 274533.527344 36.722168 48 30 504

110916.442383 274533.42187 5 36.442383 48 30 504

110916.162598 274533.316406 36.162598 48 30 504

110915.882813 274533.210938 35.882813 48 30 504

110915.603027 274533.105469 35.603027 48 30 504

正如你所看到的那样,在代码中我打印的顺序不同于

输入,而且大多数都是用一些。消除的。格式化的scanf。

但是如果你匹配,除了第一行输出是可预测的。


希望我很清楚

shashank


hi

对不起,正确的样品输出代码是


18001844.273438 135633.742190 44.273438 30 5000 14

110916.722168 274533.527344 36.722168 48 30 504

110916.442383 274533.421875 36.442383 48 30 504

110916.162598 274533.316406 36.162598 48 30 504

110915.882813 274533.210938 35.882813 48 30 504

110915.603027 274533.105469 35.603027 48 30 504


你可以在我的代码中看到已经以不同的顺序打印了

而不是输入,并且用一些。(不要担心它的故意)取消了

但如果你匹配输出是可预测的,除了第一行,因为前面说的是


plz help

shashank


<一个href =mailto:sh ********* @ gmail.com> sh ********* @ gmail.com 写道:


.... snip ...
正如你所看到的那样,我在代码中打印的顺序不同于
输入,最重要的是用一些。消除了。在格式化的scanf中。
但如果你匹配除了第一行,输出是可预测的。

希望我很清楚




不,你不是。为什么你想让任何人都难过

愿意帮助你?汇总一个可编辑的完整源代码,

将行长限制为65或甚至72个字符,并使用适当的

缩进,以及明确的输入样本。所有在一个

消息。


并摆脱非标准部分,如getch。


然后有人可能决定剪切和粘贴并编译。


-

"如果你想通过groups.google.com发布一个后续网站,不要使用

破损的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson


hi all,
I,m trying to read a space delimited file with the use of formated
input through fscanf. But surprisingly i am able to read all the 4999
lines of data except the "first line" which is giving random output(i.e
not present in the file). the code is given below(the relevant part).

sample input from the data file
01 48554.73828 504 30.48.37.001953 76.15.33.632813
2 48554.67969 504 30.48.36.722168 76.15.33.527344
3 48554.63281 504 30.48.36.442383 76.15.33.421875
4 48554.58594 504 30.48.36.162598 76.15.33.316406
5 48554.54297 504 30.48.35.882813 76.15.33.210938

the output(the first line)
18001844.273438 135633.742190 44.273438 30 5000 14
2 48554.67969 504 30.48.36.722168 76.15.33.527344
3 48554.63281 504 30.48.36.442383 76.15.33.421875
4 48554.58594 504 30.48.36.162598 76.15.33.316406
5 48554.54297 504 30.48.35.882813 76.15.33.210938
I got this prob before when i was only reading the first two columns
but as you might have noticed the leading ''0'' before 1 solved it.
Surprisingly i can still read the first two columns correctly but the
later ones are the ones that get of of hand. That too only in the first
line. rest 4999 lines are read just fine. I cannot figure it out. plz
help.its important

shashank
(i am using borland c++ 5.5, win2000)
_______________________________

fp=fopen ("TEMP31.TXT","r");
if (fp==NULL)
{ puts("cannot open file");exit(1);
}
while ((fgetc(fp))!=EOF)
{

fscanf(fp,"%d%lf%d%d.%d.%lf%d.%d.%lf",&x[a],&y[a],&fl_h[a],&latdeg[a],&latmin[a],&latsec[a],&longdeg[a],
&longmin[a],&longsec[a]);

printf ("\n%d %lf %d %d %lf %d %d %lf
%d",x[a],y[a],latdeg[a],latmin[a],latsec[a],longdeg[a],longmin[a],longsec[a],fl_h[a]);//(here
when i print it gives the correct values)

if ((a%100)==0) {getch();} else{}
a=a+1;
}

fclose(fp);

getch();

for (i=0;i<AR;i++)
{
lat[i]=(latsec[i]+(latmin[i]*60)+(latdeg[i]*3600));
lon[i]=(longsec[i]+(longmin[i]*60)+(longdeg[i]*3600));
printf ("\n%lf %lf %lf %d %d
%d",lat[i],lon[i],latsec[i],latmin[i],latdeg[i],fl_h[i]);// from here
on i get junk value

if ((i%100)==0) {getch();} else{}
}
getch();
_______________________________

this is the starting code i.e the code before the above given prog
segment, the initialisations and all (though i dont think it will be
neccessary but just to be sure)

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define ARRAY 5000
#define AR 4999
#define PI 3.142857
#define CONV 30.8333
main()
{
clrscr();
FILE *fp;
static int
x[AR],latdeg[AR],latmin[AR],longdeg[AR],longmin[AR],fl_h[AR];
static double
y[AR],longsec[AR],latsec[AR],lat_p[100],long_p[100],lat_m[100];
static double
long_m[100],lat[AR],lon[AR],dist_p[100],dist_m[100],p[100],m[100],hp[100],

hm[100],lata,longa,latb,longb,latp,longp,latm,longm,dist_ f,dist_i,pa,t,ta,tta,tta1,ttta,ttta1;
int a=0,b=0,c=0,d=0,e=0,ca=0,da=0,f=0,g=0,i,j,z,sl=0,s l1=0;
double
sa=0.0,sa1=0,sb=0.0,sc=0.0,sd=0.0,se=0,sf=0,sg=0,s g1=0,sh=0,sh1=0,xa=0,xb=0,xp=0,xm=0,ya,yb,yp=0,ym= 0;
static float fl_p[100],fl_m[100],fl_hp,fl_hm,fl_ha,fl_hb;
_________________***********_______________

解决方案

hi,
i''m sorry, the correct sample output is
18001844.273438 135633.742190 44.273438 30 5000 14
110916.722168 274533.527344 36.722168 48 30 504
110916.442383 274533.421875 36.442383 48 30 504
110916.162598 274533.316406 36.162598 48 30 504
110915.882813 274533.210938 35.882813 48 30 504
110915.603027 274533.105469 35.603027 48 30 504

As you can see that in the code i have printed in different order than
the input and most of all done away with some "." in formatted scanf.
But still if you match except the first line the output is predictable.

hope i am clear
shashank


hi
i''m sorry, the correct sample output code is

18001844.273438 135633.742190 44.273438 30 5000 14
110916.722168 274533.527344 36.722168 48 30 504
110916.442383 274533.421875 36.442383 48 30 504
110916.162598 274533.316406 36.162598 48 30 504
110915.882813 274533.210938 35.882813 48 30 504
110915.603027 274533.105469 35.603027 48 30 504

as you can see in the code that i have printed it in a different order
than input and done away with some "."s(don''t worry its intentional)
but if u match the output is predictable except the first line as i
said earlier.
plz help
shashank


sh*********@gmail.com wrote:

.... snip ...
As you can see that in the code i have printed in different order than
the input and most of all done away with some "." in formatted scanf.
But still if you match except the first line the output is predictable.

hope i am clear



No, you are not. Why do you want to make it hard for anyone
willing to help you? Put together a compilable complete source,
limiting line length to 65 or even 72 chars, with proper
indentation, together with a clear sample of the input. all in one
message.

And get rid of the non-standard portions, such as getch.

Then someone may decide to cut and paste and compile.

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


这篇关于用c读取txt文件的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆