473,698 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading first line of a txt file with c

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))!=E OF)
{

fscanf(fp,"%d%l f%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,lat b,longb,latp,lo ngp,latm,longm, dist_f,dist_i,p a,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,s l=0,sl1=0;
double
sa=0.0,sa1=0,sb =0.0,sc=0.0,sd= 0.0,se=0,sf=0,s g=0,sg1=0,sh=0, sh1=0,xa=0,xb=0 ,xp=0,xm=0,ya,y b,yp=0,ym=0;
static float fl_p[100],fl_m[100],fl_hp,fl_hm,fl _ha,fl_hb;
_______________ __***********__ _____________

Nov 14 '05 #1
16 2183
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

Nov 14 '05 #2
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

Nov 14 '05 #3
sh*********@gma il.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.c om, 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
Nov 14 '05 #4
sh*********@gma il.com wrote:
#define PI 3.142857


#define PI 3.14159265

I consider most mnemonics for PI digits, to be idiotic.

shekharpati is using twenty two sevenths.
To use that, you have to remember a two digit number,
a one digit number, an operation, and the fact that it
only gets you three significant figures.

--
pete
Nov 14 '05 #5
On 9 Jun 2005 04:41:22 -0700, sh*********@gma il.com wrote:
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


I don't get it. What kind of number is 30.48.35.882813 ? Without
proper spacing, it becomes a tedious exercise to guess what you want.
--

Best wishes,

Bob
Nov 14 '05 #6
pete wrote:
sh*********@gma il.com wrote:
#define PI 3.142857


#define PI 3.14159265

I consider most mnemonics for PI digits, to be idiotic.

shekharpati is using twenty two sevenths.
To use that, you have to remember a two digit number,
a one digit number, an operation, and the fact that it
only gets you three significant figures.


355/113 gives you seven sig. digits. Some other interesting nums:

312689 / 99532 = 3.1415926536189 36 error 0.0000000000291 44
833719 / 265381 = 3.1415926535810 78 error 0.0000000000087 15
1146408 / 364913 = 3.1415926535914 04 error 0.0000000000016 11
4272943 / 1360120 = 3.1415926535893 89 error 0.0000000000004 04
5419351 / 1725033 = 3.1415926535898 15 error 0.0000000000000 22
58466453 / 18610450 = 3.1415926535897 84 error 0.0000000000000 09
74724506 / 23785549 = 3.1415926535897 91 error 0.0000000000000 02
80143857 / 25510582 = 3.1415926535897 93 error 0.0000000000000 00

Output from ratpi.c, following:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>

/* Public domain by C.B Falconer, 2003-06-04 */
int main(int argc, char **argv)
{
int num, approx, bestnum, bestdenom;
int lastnum = 500;
double error, leasterr, pi, criterion;

pi = 4 * atan(1.0);
criterion = 2 * pi * DBL_EPSILON;
if (argc > 1) lastnum = strtol(argv[1], NULL, 10);
if (lastnum <= 0) lastnum = 500;
printf("Usage: ratpi [maxnumerator]\n"
"Rational approximation to PI = %.*f\n", DBL_DIG, pi);
for (leasterr = pi, num = 3; num < lastnum; num++) {
approx = (int)(num / pi + 0.5);
error = fabs((double)nu m / approx - pi);
if (error < leasterr) {
bestnum = num;
bestdenom = approx;
leasterr = error;
printf("%8d / %-8d = %.*f error %.*f\n",
bestnum, bestdenom,
DBL_DIG, (double)bestnum / bestdenom,
DBL_DIG, leasterr);
if (leasterr <= criterion) break;
}
}
return 0;
} /* main */

My machine takes 25 sec. to evaluate ratpi 99999999. Probably a
useful benchmark. Here it is virtually immune to gcc optimization,
except -Os which bumps it to 31 sec.

--
"If you want to post a followup via groups.google.c om, 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
Nov 14 '05 #7
CBFalconer wrote:

pete wrote:
sh*********@gma il.com wrote:
#define PI 3.142857
#define PI 3.14159265

I consider most mnemonics for PI digits, to be idiotic.

shekharpati is using twenty two sevenths.
To use that, you have to remember a two digit number,
a one digit number, an operation, and the fact that it
only gets you three significant figures.


355/113 gives you seven sig. digits. Some other interesting nums:


But knowing 3.141593 is easier;
you don't have to remember that it yields seven digits,
you can just look at it.
312689 / 99532 = 3.1415926536189 36 error 0.0000000000291 44
833719 / 265381 = 3.1415926535810 78 error 0.0000000000087 15
1146408 / 364913 = 3.1415926535914 04 error 0.0000000000016 11
4272943 / 1360120 = 3.1415926535893 89 error 0.0000000000004 04
5419351 / 1725033 = 3.1415926535898 15 error 0.0000000000000 22
58466453 / 18610450 = 3.1415926535897 84 error 0.0000000000000 09
74724506 / 23785549 = 3.1415926535897 91 error 0.0000000000000 02
80143857 / 25510582 = 3.1415926535897 93 error 0.0000000000000 00


Those are all good examples to make my point.
Each formula requires memorizing as many or more digits,
than it yields,
plus it also requires memorizing how many digits are yielded.

--
pete
Nov 14 '05 #8

On Fri, 10 Jun 2005, pete wrote:
sh*********@gma il.com wrote:
#define PI 3.142857


#define PI 3.14159265

I consider most mnemonics for PI digits, to be idiotic.

shekharpati is using twenty two sevenths.
To use that, you have to remember a two digit number,
a one digit number, an operation, and the fact that it
only gets you three significant figures.


1. Go to: http://www.gutenberg.org/

2. Follow the link labelled "Advanced Search".

3. In the form provided, enter "50" (without the quotes) in the
"EText-No.:" field and submit a search with the "Go!" button:

EText-No.: 50____ Go!

4. From the page you get, download the text (about 1.4MB) or zipped
(819200 bytes) copy of pimil10.txt and then ...

5. Enjoy the luxury of having Pi available to more than a million
decimal places.

--
Can you Change: *alchemy to alchemy* (* == Unicorn)
mindworks mindworks
in 103 moves? Try http://www.chebucto.ns.ca/~af380/AMPuzzle.html
(Requires Firefox or a browser with a compatable version of JavaScript)

Nov 14 '05 #9
Norman L. DeForest wrote:
5. Enjoy the luxury of having Pi available to more than a million
decimal places.


I like Ooura's pi_fftc.c program.

http://momonga.t.u-tokyo.ac.jp/~ooura/pi_fft.html

--
pete
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3060
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
7
2787
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 Col3 Col4 A0012430 REKAL TVĂ„TTMEDEL EKOMAX 0,5L ST 75.9000
2
10691
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It appears to be in the OLEDB driver also. My connection was: conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";" & "Extended Properties='Text;HDR=NO;FMT=Delimited'"
6
3790
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple coding convention for reading in a flatfile - one record at a time?? SCANF does not work because of...
4
5987
by: Ameya | last post by:
i want to read from a trace file ....but my problem is i am interested only in first 6 columns of my 10 - 12 wide text file. Each column is separated by a space (the number of columns vary on each row) for. e.g an entry looks this way r 10.33011 _0_ AGT 0 ACK 48 i only want to read till 48. I am able to read all the variables for the first line, but after processing first line, i want to scan from
40
4521
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one line at a time and print it out. How can I do this? I wrote the code below but it's not correct since the fscanf reads one word (terminating in whitespace or newline) at a time, instead of reading the whole line. #include <stdio.h> void...
4
12792
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
2
1836
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got deleted. The content of the external file , which is in .cvs format is: -------------------------------------------------------------------
21
3054
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z 00000000_00 0 000 000 000 0000 000 I need to read the file line by line and eventually parse out each piece of the file and store in arrays that correspond to the specific
3
4751
by: xyz | last post by:
Hi, I have a text file around 7GB includes 100 million lines... I want to read the data line by line when I approach my module.. ie., when i read for the first time , my program shuld read only one line and when i read for the next time , my program shuld read only second line discarding first line.. i did it with ignore function but it is taking lot of time to reach
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.