How can I read and store strings in C from stdin until a blank line is entered?

  • Thread starter Thread starter dwx1
  • Start date Start date
  • Tags Tags
    Reading Strings
Click For Summary

Discussion Overview

The discussion centers around reading and storing strings from standard input in C until a blank line is entered. Participants explore various coding approaches, potential pitfalls, and corrections related to string handling in C, including memory management and input functions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents an initial code attempt to read strings using scanf and a while loop, but encounters issues.
  • Another participant questions the method of checking for a blank line, suggesting that using "" may not be the correct termination.
  • A later reply points out that comparing strings in C requires the strcmp function, as using buffer != "" compares pointers instead of string content.
  • Concerns are raised about the way strings are stored, noting that assigning buffer to strings merely points to the same memory location, which can lead to overwriting issues.
  • Participants discuss the need for proper memory allocation for strings and suggest using fgets instead of scanf to handle input more effectively.
  • One participant expresses confusion over implementing the suggestions, particularly with using strcmp and copying strings correctly.
  • Another participant clarifies that scanf discards whitespace, which affects how blank lines are processed.
  • It is suggested that strcpy should be used to copy buffer contents into the allocated memory for strings, but memory allocation must be handled first.
  • Ultimately, one participant reports success in getting their code to work after the discussion.

Areas of Agreement / Disagreement

Participants express various viewpoints on the correct methods for reading and storing strings, with no consensus reached on a single approach. Multiple competing views on string handling and memory management remain evident throughout the discussion.

Contextual Notes

Limitations include unresolved issues regarding memory allocation and the handling of input functions, as well as the potential for confusion around string comparison and copying methods.

Who May Find This Useful

This discussion may be useful for programmers learning C, particularly those interested in string manipulation and input handling techniques.

dwx1
Messages
6
Reaction score
0
I have to read a list of strings in from stdin and store them, stopping when a blank line is entered. Here's what I tried...
Code:
        const int LINE_CAP = 100;
        const int MAX_LENGTH = 80;

        char *strings[LINE_CAP];
        int strings_index = 0;
        char buffer[MAX_LENGTH];

        printf("%s", "Enter words:\n");
        scanf("%s", buffer);
        while(buffer != "")
        {
                        strings[strings_index] = buffer;
                        strings_index++;
                        scanf("%s", buffer);
        }

No luck. Does anyone know how to do it?
Thanks.
 
Technology news on Phys.org
is that the proper way to declare an array of strings? if it is then "" may not be the correct termination for a blank.
 
There's quite a few problems with this code.

First of all, you can't do things like buffer != "" in C. To compare strings you'll need to use the strcmp function. Doing buffer != "" just compares two pointers, which is not at all what you want.

Second, when you do strings[string_index] = buffer, it does not copy the contents of buffer into strings[string_index]. It merely sets strings[string_index] to point to the same location in memory as buffer. That's certainly not what you want to do. Buffer gets overwritten every pass through the loop.

Third, there's no guarantee that the lines you read in will fit into buffer. You need to use a formatting option like %79s in scanf to ensure that it doesn't try to read data in past the end of the buffer (and you'll have to make sure that the last character in the string is a NUL, or else functions like strcpy and strcmp and printf will not work). You may want to consider using fgets instead. In fact, since you want to read in lines, you may want to use fgets anyway; scanf will stop when it hits whitespace, not when it hits the end of a line.
 
Ok, I thought I got what you were saying, but I seem to be confused...

So I tried using while(strcmp(buffer, "")), but that doesn't stop it when I
enter a blank line.

And I tried doing *strings[strings_index] = buffer, that didn't work either.

I'm kind of lost again.
 
Last edited:
dwx1 said:
Ok, I thought I got what you were saying, but I seem to be confused...
So I tried using while(strcmp(buffer, "")), but that doesn't stop it when I
enter a blank line.
And I tried doing *strings[strings_index] = buffer, that didn't work either.
I'm kind of lost again.

scanf discards whitespace. If you enter a blank line it skips over it and keeps waiting for you to enter data.

To copy buffer into strings[strings_index] you can use strcpy, as in strcpy(strings[string_index], buffer). But that won't work unless you allocate memory for the string first. strings[string_index] is just a pointer, and since you don't initialize it, it could be pointing anywhere. Either use malloc() to allocate memory for it first, or declare strings using "char strings[LINE_CAP][MAX_LENGTH];" instead.
 
Ok, I got it working! Thanks so much!
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
2K
Replies
10
Views
2K
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
27K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
5K
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K