file i/o -- assignment suppression 
Author Message
 file i/o -- assignment suppression

Hello,

I have a file with 8 columns of numbers: 6 ints and 2 floats. (several
lines with this pattern) I want to read all of the last floats and sum
them. I have my format string of how I wrote the file, so I reverse that
and do an fscanf with assignment suppression. Well, it sounds trivial,
but the program just hangs at the point where it reads the first line.

"strace" indicates this:

read(3, "1998:208:3:2:50:29 901508570.029"..., 4096) = 224

the wierd thing is is that above you do not see the float I want..the
one after 901508570.029..

my format string is as follows:

fscanf(fp,"%*4d%*3d%*2d%*2d%*2d%*3d%*12.3f%12.4e", &num);
                                            ^
                                            |
                                        I want this one

my input file looks like this:

1998:208:3:2:50:29 <tab>  901508570.029 <tab>       1.0147e+04
1998:208:4:43:20:29 <tab> 01514600.029    <tab> 8.2280e+03
1998:208:5:27:30:29     <tab>   901517250.029     <tab> 5.3737e+03

(I put in the <tab>s to indicate they are there. In my format string
that I wrote the lines I have \t..do I need this when I READ the file as
well?)

Also..how can I open a file, skip two lines and then read from there. In
my above input file, there is a two line header that I have to skip
before I can read data...

Thank you for any suggestions. Much appreciated!

/Cody Caughlan

--
--------------------------
Cody Caughlan

--



Mon, 25 Mar 2002 03:00:00 GMT  
 file i/o -- assignment suppression

Quote:

>I have a file with 8 columns of numbers: 6 ints and 2 floats.
 [for example]
>1998:208:3:2:50:29 <tab>      901508570.029 <tab>       1.0147e+04
>my format string is as follows:

>   fscanf(fp,"%*4d%*3d%*2d%*2d%*2d%*3d%*12.3f%12.4e", &num);
>I have my format string of how I wrote the file, so I reverse that
>and do an fscanf with assignment suppression. Well, it sounds trivial,
>but the program just hangs at the point where it reads the first line.

Besides the insertion of the assignment-suppressing "*"s, there is
another difference between your fprintf format and your fscanf format.
If your fprintf format were:

        "%4d%3d%2d%2d%2d%3d%12.3f%12.4e"

the example line would be:

        1998208325029901508570.0291.0147e+04

To reverse the *actual* format (which includes colons) you will need
to include the colons in the fscanf directive.  You do not need the
"\t"s because scanf formats "d", "f", and "e" all begin by consuming
any whitespace in the input stream (including newlines).  Whitespace
as a directive simply means "consume whitespace in the input stream",
so "\t%f" means "skip all whitespace, then skip all whitespace, then
consume a floating-point number", which contains an obvious redundancy.

Some printf-family directives have *no* corresponding scanf-family
directives, and of course scanf is a complicated function that
often does something almost but not entirely unlike what you wanted
it to do.  In particular, the scanf family is a little like a
paper-shredder that "jams" upon reaching unexpected staples.  If
you give a format of "%*4d%*3d" and the first few bytes of input
are, say, "1998:", the "4d" part consumes the "1998", and the "3d"
part jams on the ":", because it is not a digit.  The ":" remains
unconsumed until you do something that can accept a ":".

If you put the colon back in, but the input happens to have been
changed slightly, so that it reads, say, "1998/208", a scanf expecting
a ":" will "jam" on the "/" in the same way.

Quote:
>Also..how can I open a file, skip two lines and then read from there.

Read two lines and discard them.

Standard C contains a function that reads lines into a line buffer:
fgets().  If you fgets() a complete line, you can then inspect it
or discard it; if you want to scanf it, you can call sscanf(), and
inspect the return value from sscanf().  If the return value is
not what you expected, you can inspect the buffer to see what is
there and why it is not what you expected -- or at the least, print
it out for some other agent (program or person) to inspect.  This
makes fgets-then-sscanf generally much superior to plain fscanf.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc


--



Mon, 25 Mar 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. "scanf" function with assignment-suppression

2. sscanf assignment suppression character

3. Windows Application Error Suppression?

4. HELP: Alignment suppression in C/UNIX

5. DocTemplate suppression

6. Suppression d'un DocTemplate

7. warning: ambiguous assignment: assignment op taken

8. FILE assignment

9. auto_ptr assignment broken in 7.1?

10. Icon assignment

11. HELP: Overloading Assignment Operators in MC++

12. Overloading Assignment Operators in MC++

 

 
Powered by phpBB® Forum Software