1

I am working on a C program that will read user input as Strings separated by spaces. I wanted a way to read these strings up until the blank spaces. I know that scanf does this with ease, but I was wondering if it was possible to do the same with gets() or sscanf() or some combination of the 2. I am very new to C, only having just started it and I come from a Java background. Thank you all in advance, I really appreciate any and all help!!!! Find below the code I have so far as well as well as a sample input (***The wanted functionality is achieved if the scanf() portion is uncommented, but the goal is achieve the same functionality as scanf() only using gets() and sscanf()).

int main()
{
    printf("Enter words seperated by spaces:(. or EOF to stop):\n");

    do
    {
        char s[255];
        //scanf("%s",s);
        gets(s);
        if(strcmp(s,".")==0 || strcmp(s,"EOF")==0)
        {
            insert_dictionary_order(s);//adding to list
            break;
        }
        else
        {
            insert_dictionary_order(s);//adding to list
        }
    }
    while(1);
    //now printing list
    print_list();
    return 0;
} 
This is a sample text.
The file will be terminated by a single dot: .
The program continues processing the lines because the dot (.)
did not appear at the beginning.
. even though this line starts with a dot, it is not a single dot.
The program stops processing lines right here.
.
You won't be able to feed any more lines to the program.

Edit: The answer MUST implement either get() or sscanf() or both.

abdcg
  • 137
  • 1
  • 11

2 Answers2

1

First, Never, Ever, Ever use gets(). It is so insecure and prone to exploit by buffer overflow it has been completely removed from the c-library beginning with C11. See Why gets() is so dangerous it should never be used! (using scanf() with the "%s" or "%[..]" conversion specifiers without the optional field-width modifier are just a bad)

fgets() is what you are looking for. To read your text file, stopping when '.' appears alone as the first character in a line with nothing following (aside from the '\n' included in the buffer by fgets()), you simply read each line into a sufficiently sized buffer (character array) and check the first character in the line to see if it is a '.' and check the second character to see if it is '\n', and if so, exit your read-loop.

For example:

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

#define MAXC 1024       /* if you need a constant, #define one (or more) */

int main (void) {
    
    char line[MAXC];    /* buffer to hold line */
    
    while (fgets (line, MAXC, stdin)) {         /* read each line */
        /* if 1st char '.' and either EOF or next is '\n', done */
        if (*line == '.' && (!line[1] || line[1] == '\n'))
            break;
        line[strcspn (line, "\n")] = 0;         /* trim \n from end of line */
        puts (line);                            /* output line */
    }
}

(note: above strcspn (line, "\n") returns the number of characters in line up to the '\n' allowing you to overwrite the '\n' with the nul-termianting character to remove it)

Generally checking that the second character in the line beginning '.' is a newline would be sufficient, but there are a number of editors that do not write POSIX compliant files (meaning no '\n' after the final line). If you didn't check against EOF, then for files with non-POSIX file endings, you would output the last '.' if it appeared on the final line.

Example Use/Output

With your example input in dat/stop_on_dot.txt redirected to the program on stdin as input, you would receive the following:

$ ./bin/stop_on_dot < dat/stop_on_dot.txt
This is a sample text.
The file will be terminated by a single dot: .
The program continues processing the lines because the dot (.)
did not appear at the beginning.
. even though this line starts with a dot, it is not a single dot.
The program stops processing lines right here.

Look things over and let me know if you have any questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

Never use gets, there is no check for boundary there.

As for your concern using just getchar() is enough by checking the input is space character( , \t,\n) I have just written a small program to take input till the space. we can write it more robustly, let me know if this solves your problem.

#include <stdio.h>

int main()
{
    char ch;
    while(ch = getchar())
    {
        if(ch != ' ' && ch != '\t' && ch != '\n')
            putchar(ch);
        else
            break;
    }
    return 0;
}
csavvy
  • 761
  • 4
  • 13