home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!acorn!eoe!pwestlak
- From: pwestlak@eoe.co.uk (Peter Westlake)
- Newsgroups: comp.lang.c
- Subject: Re: How to fprintf two columns?
- Message-ID: <1386@eouk20.eoe.co.uk>
- Date: 27 Aug 92 17:47:57 GMT
- References: <1684FBD10.KSZ@VM.NRC.CA>
- Organization: EO Europe Limited, Cambridge, UK
- Lines: 58
- X-Newsreader: Tin 1.1 PL3
-
- KSZ@VM.NRC.CA writes:
- : I need the following output to a file:
- : 1.2 2.3
- : 1.3 2.4
- : 1.4 2.5
- : ....
- : which contains two columns. They are not generated at the same time, i.e.
- : at the first the first column is generated and then write to a file. then
- : the file is rewinded and the second column is generated. Now I need write
- : the second column to that particular place, say begin with 10th column.
- : I have tried fseek() but it seems to me not working. (?)
- :
- The way to do this is to think of the file as a stream of bytes:
-
- sp sp 1 . 2 sp sp 2 . 3\n sp sp 1 . 3 sp sp 2 . 4 \n ......
-
- Leave a gap after each number in the first column, wide enough for
- the second column entry. For instance:
-
- printf("%5.1f .....\n", first);
-
- Then fseek to each gap (bytes 7, 19, 32... in this example layout) and
- write the second column:
-
- printf("%5.1f", second);
-
- For the numbers of the second column you have to fseek between each one.
- For the first you can just write enough extra characters to take you to
- the right place, which is what all the dots are doing in the first printf.
-
- Here is a complete example, which I have just run (as befits one who
- tests software for a living! :-)
-
- #include <stdio.h>
-
- main()
- {
- long i;
- float f;
-
- f = 1.2;
- for (i = 0; i < 3; i++) {
- printf("%5.1f .....\n", f);
- f += 0.1;
- }
- f = 2.3;
- for (i = 0; i < 3; i++) {
- fseek(stdout, 13*i+7, 0);
- printf("%5.1f\n", f);
- f += 0.1;
- }
- exit(0);
- }
- ------------------------------------------------------------------------------
- Peter M Westlake EO Computer Ltd.,
- Tel: +223-843131 Abberley House, Granhams Road,
- Fax: +223-843032 Great Shelford,
- email: pwestlak@eoe.co.uk Cambridge CB2 5LQ, England
-