Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > segmentation fault with gets()

Reply
Thread Tools

segmentation fault with gets()

 
 
Chris Torek
Guest
Posts: n/a
 
      10-20-2003
In article <J%ykb.198649$ko%(E-Mail Removed). rogers.com>
Peter Dragun <(E-Mail Removed)_NOSPAM> writes:
>... I have to create a simple program, that takes the following
>information from a file using redirection:
>
>4 4 4 4 4 4 5 5 5 5 5 5 3 3 3 3 3 3 3 3 2 2 2 10 10 10 1 1
>3 3 3 3 3 3 3 3 3 2 2 2 12 12 12 1 7 7 7 7 4
>12 13 13 5 5 5 3 3 9 7 7 7 5 4 4 4 3 3 3 3
>
>read each line, and "compress the information" ...


[in short, using a form of Run-Length Encoding; the output is
(N,value) pairs representing N instances of the given value].

Without answering questions about gets() (others have done that
already), I will note that the first thing I thought about when
I read the above is:

What if the input has the same integer value split across
several lines?

For instance, if the input is:

4
4
4 5
5 101 7

should the output be:

1 4
1 4
1 4
1 5
1 101
1 7

or should it be:

3 4
2 5
1 101
1 7

?

Later, you note:

>the instructions told us we could only use scanf ...


which suggests (but does not say for certain) that the answer is
the latter. In particular, if you use scanf's "%d" format, the
scanf engine -- the code shared by scanf, fscanf, sscanf, and in
C99 the various vscanf routines -- will skip "white space", and
newlines are considered white space. Hence a loop of the form:

while (scanf("%d", &var) == 1)

will eat right through those newlines, producing the second kind
of output. (Note that scanf() will "jam" if it hits a non-numeric,
non-white-space character, so this kind of loop is certainly not
"robust" against malformed input. As a result you might actually
do better, in the end, using fgets(), unless of course you are Dan
Pop. )

(The reason I say "does not say for certain" is that if you use
scanf with "%c" formats, and "manual" conversion of integer values
one digit at a time, you will be able to "see" the newlines in the
input stream. This again gives you the choice of whether to consider
newlines significant. Note, however, that the first form of output
does *not* allow you to reconstruct the original set of newlines.
If the RLE-output says "N1 X" followed by "N2 X" -- i.e., if the
value being run-length encoded repeats -- then you know for certain
there was a newline between these, but if the value after N2 is
not X, you do not know whether there was a newline there. That
makes it hard for me to imagine why anyone would *want* the first
form of output.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments