472,985 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 software developers and data experts.

segmentation fault with gets()

I am generally new to programming under Unix, but know how to code under
Windows. 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" to the following out print to
be printed on screen:
6 4
6 5
8 3
3 2
3 10
2 1
9 3
3 2
3 12
4 7
1 4
1 12
2 13
3 5
2 3
1 9
3 7
1 5
3 4
4 3

So the first column counts the occurences, and the second column in the
integer occurence.
I keep on getting a segmentation fault when I run my program, here is the
source code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* NUMBER, and COUNT are used in numbers array, indicating what is stored
*where
*/
#define NUMBER 0
#define COUNT 1

/* boolean definitions*/
#define TRUE 1
#define FALSE 0

/*flag to make sure first entry is processes properly*/
int first_run = TRUE;

/* next=store the next value;
* prev= stores the previous value from the stream;
* count=keep track how many times the same number is in sequence;
* incr_x = keeps track where to store the numbers in the numbers array;
* incr = used to display the numbers array, when encoding is complete.
*/
int next, prev, count, incr_x, incr;

/* Stream string to store input to be encoded*/
char stream[900] = "4 4 4 4 4 2 2 6 6 6 6 6 6 6 6 6 4 4 4 4 4 10 10 10 7 7 7
7 7 4 4";

/* Special delimiter to differient between values, this array is used with
* strtok function, the delimiter is set to whitespaces, so when a
whitespace
* is found, it indicates that the next element is a number.
*/
char delimiters[2] = " ";

/* Token pointer stores the outcome created by strtok*/
char *token;

/* The numbers array stores the numbers and count of them
* A number is stored using numbers[incr][NUMBERS]=n, and the count of that
* number found in the sequence i stored by numbers[incr][COUNT]=c
*/
int numbers[100][2];

int main() {
/* Standard input, sending data to the steam array for processing.*/

// This is where i get the fault, the program works, if I take out
the while, and just use the gets, but it only gets one line from the
// file.

while( gets( stream ) != NULL ) {
/* Get the first element that is delimited by whitespace. */
token = strtok( stream, delimiters );

do {
next = atoi( token );

/* If it is a first run, their is not previous value, so we
* assign prev with next.
*/
if( first_run ) {
prev = next;
first_run = FALSE;
}

/* If next is equal to the previous value, we increment the
count.*/
if( next == prev ) {
count++;

/* If the next value does not equal the previous,
that indicates a new
* number was found in the stream, so we insert the previous
value
* into the numbers array, and store how many times it was found
* sequentially, and then we reset the count, and increment
the numbers
* array for the next value.
*/
} else {
numbers[incr_x][NUMBER] = prev;
numbers[incr_x][COUNT] = count;
count = 1;
incr_x++;
}
prev = next;
/* We stop when the stream is finished.*/
} while( ( token = strtok( NULL,delimiters ) ) != NULL );

/* Since the strtok ends the do.while loop, before we can
process the
* last number found, we do it here.
*/
numbers[incr_x][NUMBER] = next;
numbers[incr_x][COUNT] = count;
incr_x ++;
}
/* Print out our results. */
for( incr=0; incr < incr_x; incr++ ) {
printf( "%d %d\n", numbers[incr][COUNT],
numbers[incr][NUMBER] );
}
return 0;
}

I wrote a comment where, I think the fault is occurring. Essentially, the
instructions told us we could only use scanf and printf for our I/O, but I
couldn't see how to use scanf to get what I want, so I decided to side line
one spec, and use gets(). With that in mind, I am limited to the use of
functions, and do not want to stray further from using gets or scanf.

Thanks

--
Peter Dragun
Faculty of Computer Science 2nd year student
Western University, London, Ontario, Canada
Nov 13 '05 #1
10 7176
# /* Standard input, sending data to the steam array for processing.*/
#
# // This is where i get the fault, the program works, if I take out
# the while, and just use the gets, but it only gets one line from the
# // file.
#
# while( gets( stream ) != NULL ) {

Have you verified the input doesn't overflow the buffer? There's a reason
most people used fgets instead.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Mention something out of a Charleton Heston movie, and suddenly
everybody's a theology scholar.
Nov 13 '05 #2
I have been reading upon, how dangerous gets() is, could you help me provide
an alternative using scanf or fgets(without reading a file, but still using
the stdin)?

"Derk Gwen" <de******@HotPOP.com> wrote in message
news:vp************@corp.supernews.com...
# /* Standard input, sending data to the steam array for processing.*/ #
# // This is where i get the fault, the program works, if I take out # the while, and just use the gets, but it only gets one line from the
# // file.
#
# while( gets( stream ) != NULL ) {

Have you verified the input doesn't overflow the buffer? There's a reason
most people used fgets instead.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Mention something out of a Charleton Heston movie, and suddenly
everybody's a theology scholar.

Nov 13 '05 #3
"Peter Dragun" <pd*****@cogeco.ca_NOSPAM> wrote:
I have been reading upon, how dangerous gets() is, could you help me provide
an alternative using scanf or fgets(without reading a file, but still using
the stdin)?


<snip>

....
#define BUFLEN 900
....
char stream[BUFLEN];
....
while( fgets( stream, BUFLEN, stdin ) {
....

HTH

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #4
Peter Dragun wrote:
I have been reading upon, how dangerous gets() is, could you help me
provide an alternative using scanf or fgets(without reading a file, but
still using the stdin)?


/* untested code - beware! */

#include <stdio.h>
#include <string.h>

int chop(char *s)
{
int chopped = 0;
char *p = strchr(s, '\n');
if(p != NULL)
{
*p = '\0';
chopped = 1;
}
return chopped;
}

#define SOME_SIZE_OR_OTHER 32

int main(void)
{
char buf[SOME_SIZE_OR_OTHER] = {0};

if(fgets(buf, sizeof buf, stdin) != NULL)
{
if(chop(buf))
{
printf("The string is [%s]\n", buf);
}
else
{
printf("The string was a tad long. Here's "
"some of it: [%s]\n", buf);
if(fgets(buf, sizeof buf, stdin) != NULL)
{
printf("Here's some more: %s\n", buf);
}
}
}
else
{
printf("EOF or error encountered.\n");
if(ferror(stdin))
{
printf("Error.\n");
}
else
{
printf("EOF.\n");
}
}
return 0;
}

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #5
Irrwahn Grausewitz wrote:
...
#define BUFLEN 900
...
char stream[BUFLEN];
...
while( fgets( stream, BUFLEN, stdin ) {


Better than BUFLEN: sizeof stream

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6
Richard Heathfield <do******@address.co.uk.invalid> wrote:
Irrwahn Grausewitz wrote:
...
#define BUFLEN 900
...
char stream[BUFLEN];
...
while( fgets( stream, BUFLEN, stdin ) {


Better than BUFLEN: sizeof stream


Indeed.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #7
"Peter Dragun" <pd*****@cogeco.ca_NOSPAM> wrote:
I am generally new to programming under Unix, but know how to code under
Windows. I have to create a simple program, that takes the following
information from a file using redirection:
<snip>read each line, and "compress the information" to the following out print to
be printed on screen:

<snip>

/* If this is homework, don't read any further!!!!
*
*
*
*
*
*
*
*
*
*
*
*
*
*/

#include <stdio.h>

int main( void )
{
int n = 0,
nn = 0,
cnt = 0,
run = 1;

while( run )
{
if ( scanf( "%d", &n ) != 1 )
run = 0;

if ( n != nn || !run )
{
if ( cnt )
printf( "%d %d\n", cnt, nn );
nn = n;
cnt = 1;
}
else
++cnt;
}
return 0;
}

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #8
Irrwahn Grausewitz wrote:

Richard Heathfield <do******@address.co.uk.invalid> wrote:
Irrwahn Grausewitz wrote:
...
#define BUFLEN 900
...
char stream[BUFLEN];
...
while( fgets( stream, BUFLEN, stdin ) {


Better than BUFLEN: sizeof stream

Also better would be ...
while( fgets( stream, sizeof stream, stdin )) {}
so that the right paren doesn't get lonely. :-)
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #9
Peter Dragun wrote:

I am generally new to programming under Unix, but know how to
code under Windows. 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" to the following
out print to be printed on screen:
6 4
6 5
8 3
3 2
3 10
2 1
8 3
.... snip ...

So the first column counts the occurences, and the second column
in the integer occurence.
I keep on getting a segmentation fault when I run my program,
here is the source code:
.... snip code using gets ...
I wrote a comment where, I think the fault is occurring.
Essentially, the instructions told us we could only use scanf
and printf for our I/O, but I couldn't see how to use scanf to
get what I want, so I decided to side line one spec, and use
gets(). With that in mind, I am limited to the use of functions,
and do not want to stray further from using gets or scanf.


Bad decision. Your instructor was leading you in the right
direction. Think about what scanf does ... it extracts a number
(or other things) from an incoming stream. It requires no extra
buffering of the incoming stream, and will skip all white space
while extracting numbers. It also tells you if it succeeded. So,
the phrase:

int anint;
....
if (1 == scanf(stdin, "%d", &anint)) {
/* whatever */
}

will extract a number and tell you if it succeeded. You can also
modify that into a while statement. Now think about how you can
tell if this is the same number as the previous one, and what to
do if it is, or if it isn't. Then think about how to get the
process started. You may well find you need a few extra variables
and initialization.

When you are finished you will be astounded at how simple it is.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #10
In article <J%********************@news04.bloor.is.net.cable. rogers.com>
Peter Dragun <pd*****@cogeco.ca_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.
Nov 13 '05 #11

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

Similar topics

18
by: bumps | last post by:
Guys, I am getting segmentation fault while I am trying to open a file in a small function. int PingSamplesList::saveListElements (char *outputFileString) { PingSampleElement *tmpPtr; int...
3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
9
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the...
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
10
by: Linny | last post by:
Hi All, I am pasting a piece of code which executes fine on 32 bit system but fails with a segmentation fault when compiled 64 bit compiler.I am using a HP-UX C compiler on PA-RISC system. This...
8
by: priyanka | last post by:
Hi, I am getting segmentation fault in a very simple thing. But I am not able to figure out waht is wrong. I should be missing something. char *kernel_rget_pt; char kernel_rec_buf = {'t',...
7
by: macaco | last post by:
Hi, I'm getting a segmentation fault on this code when it gets to fopen: tac.h: typedef struct { char* ventanaInspeccion; char* ventanaMemoria; FILE* archivo; } TAC;
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.