tags:

views:

849

answers:

4

Hi,

im getting "Bus Error" trying to read stdin into a char* variable. I just want to read whole stuff coming over stdin and put it first into a variable, then continue working on the variable.

My Code is as follows:

char* content;
char* c;
while( scanf( "%c", c)) {
 strcat( content, c);
}

fprintf( stdout, "Size: %d", strlen( content));

But somehow i always get "Bus error" returned by calling "cat test.txt | myapp", where "myapp" is the compiled code above.

My question is how do i read stdin until EOF into a variable? As you see in the code, i just want to print the size of input coming over stdin, in this case it should be equal to the size of the file "test.txt".

I thought just using scanf would be enough, maybe buffered way to read stdin?

+6  A: 

Your problem is that you've never allocated c and content, so they're not pointing anywhere defined -- they're likely pointing to some unallocated memory, or something that doesn't exist at all. And then you're putting data into them. You need to allocate them first. (That's what a bus error typically means; you've tried to do a memory access that's not valid.)

(Alternately, since c is always holding just a single character, you can declare it as char c and pass &c to scanf. No need to declare a string of characters when one will do.)

Once you do that, you'll run into the issue of making sure that content is long enough to hold all the input. Either you need to have a guess of how much input you expect and allocate it at least that long (and then error out if you exceed that), or you need a strategy to reallocate it in a larger size if it's not long enough.

Oh, and you'll also run into the problem that strcat expects a string, not a single character. Even if you leave c as a char*, the scanf call doesn't make it a string. A single-character string is (in memory) a character followed by a null character to indicate the end of the string. scanf, when scanning for a single character, isn't going to put in the null character after it. As a result, strcpy isn't going to know where the end of the string is, and will go wandering off through memory looking for the null character.

Brooks Moses
They're not NULL, they have undefined values.
Matthew Flaschen
+1 @Matthew, unless they're globals.
Carl Norum
Carl Norum
Carl: Thanks, I was just adding that. Matthew, thanks for the comment; I'll edit to add that as well.
Brooks Moses
+4  A: 

Since you don't care about the actual content, why bother building a string? I'd also use getchar():

int    c;
size_t s = 0;

while ((c = getchar()) != EOF)
{
  s++;
}

printf("Size: %z\n", s);

This code will correctly handle cases where your file has '\0' characters in it.

Carl Norum
The OP said: "I just want to read whole stuff coming over stdin and put it first into a variable, then continue working on the variable." I think the length() call on it is just an example. But your comment about the file having '\0' characters is an important one -- their whole idea of putting it in a single variable may be a bit flawed there.
Brooks Moses
`c` should be declared as `int` to include the range of `char` plus `EOF`.
Jon Purdy
@Jon yup, good call. Typing without thinking.
Carl Norum
@Brooks, I must have missed that when reading. I think the answer stands as a reasonable example anyway.
Carl Norum
Carl: Agreed. (And upvoted.)
Brooks Moses
A: 

The problem here is that you are referencing a pointer variable that no memory allocated via malloc, hence the results would be undefined, and not alone that, by using strcat on a undefined pointer that could be pointing to anything, you ended up with a bus error!

This would be the fixed code required....

char* content = malloc (100 * sizeof(char));
char c;
if (content != NULL){
   content[0] = '\0'; // Thanks David!
   while ((c = getchar()) != EOF)
   {
       if (strlen(content) < 100){
           strcat(content, c);
           content[strlen(content)-1] = '\0';
       }
   }
}
/* When done with the variable */
free(content);

The code highlights the programmer's responsibility to manage the memory - for every malloc there's a free if not, you have a memory leak!

Edit: Thanks to David Gelhar for his point-out at my glitch! I have fixed up the code above to reflect the fixes...of course in a real-life situation, perhaps the fixed value of 100 could be changed to perhaps a #define to make it easy to expand the buffer by doubling over the amount of memory via realloc and trim it to size...

tommieb75
This code has a bug: the first time through the loop you're calling "strlen" on uninitialized data. (and of course you should be checking for buffer overruns if more than 100 chars of input are present)
David Gelhar
As Jon Purdy noted on one of the other posts, you need to declare c as int, not char, so that the possible EOF result will be in range. Otherwise you'll go into an infinite loop. Also, your strcat call expects two null-terminated strings, not a string and a single character. As written, it won't compile.
Brooks Moses
+2  A: 

First, you're passing uninitialized pointers, which means scanf and strcat will write memory you don't own. Second, strcat expects two null-terminated strings, while c is just a character. This will again cause it to read memory you don't own. You don't need scanf, because you're not doing any real processing. Finally, reading one character at a time is needlessly slow. Here's the beginning of a solution, using a resizable buffer for the final string, and a fixed buffer for the fgets call

#define BUF_SIZE 1024
char buffer[BUF_SIZE];
size_t contentSize = 1; // includes NULL
/* Preallocate space.  We could just allocate one char here, 
but that wouldn't be efficient. */
char *content = malloc(sizeof(char) * BUF_SIZE);
if(content == NULL)
{
    perror("Failed to allocate content");
    exit(1);
}
content[0] = '\0'; // make null-terminated
while(fgets(buffer, BUF_SIZE, stdin))
{
    char *old = content;
    contentSize += strlen(buffer);
    content = realloc(content, contentSize);
    if(content == NULL)
    {
        perror("Failed to reallocate content");
        free(old);
        exit(2);
    }
    strcat(content, buffer);
}

if(ferror(stdin))
{
    free(content);
    perror("Error reading from stdin.");
    exit(3);
}
Matthew Flaschen