How to read the standard input into string variable until EOF in C?

后端 未结 5 2138
北恋
北恋 2020-12-05 08:44

I am getting \"Bus Error\" trying to read stdin into a char* variable. I just want to read whole stuff coming over stdin and put it fi

相关标签:
5条回答
  • 2020-12-05 09:13

    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.

    0 讨论(0)
  • 2020-12-05 09:20

    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...

    0 讨论(0)
  • 2020-12-05 09:21

    Assuming that you want to get (shorter than MAXL-1 chars) strings and not to process your file char by char, I did as follows:

    #include <stdio.h>
    #include <string.h>
    #define MAXL 256
    
    main(){
      char s[MAXL];
      s[0]=0;
      scanf("%s",s);
      while(strlen(s)>0){
        printf("Size of %s : %d\n",s,strlen(s));
        s[0]=0;
        scanf("%s",s);
      };
    }
    
    0 讨论(0)
  • 2020-12-05 09:26

    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);
    }
    

    EDIT: As Wolfer alluded to, a NULL in your input will cause the string to be terminated prematurely when using fgets. getline is a better choice if available, since it handles memory allocation and does not have issues with NUL input.

    0 讨论(0)
  • 2020-12-05 09:31

    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.

    0 讨论(0)
提交回复
热议问题