scanf() multiple words in C

2019-06-14 20:59发布

问题:

I need a simple function that can read multiple words into a string in C, kind of like Scanner.nextLine() in Java or Console.ReadLine() in C#, but I can't seem to find a simple method anywhere, I tried all sorts of things, but none of them seem to work 100% of the time.

回答1:

You can use fgets

char *fgets(char *str, int n, FILE *stream)

Be aware that fgets reads until reach the size n, or a newline or an EOF. This is made to avoid buffer overflow, so you need to make sure that your buffer is big enough to store the string.



回答2:

scanf is what you want:

#include <stdio.h>

int main(void)
{
    char string[50];
    scanf(" %49[^\n]s",string);

    printf("you entered: %s\n",string);
}


回答3:

you can follow this:

char str[100];// as length of the total words neded
    fgets(str,100,stdin)

        printf("output: %s",str);

    return 0;


回答4:

Use fgets to parse an input string into a char buffer.

char * fgets ( char * str, int num, FILE * stream );

Here is the link for a better description

Then use sscanf to dissect this string to however you need it.

int sscanf ( const char * s, const char * format, ...);

Here is the link for a better description



回答5:

This removes the '\n' character that is included in the string returned by fgets, thus making the function practically equivalent to the functions present in popular managed languages:

//gets a line from the specified stream
int getline(char* charArray, int maxLength, FILE* stream) {
    int i;
    if (fgets(charArray, maxLength, stream) == NULL)
        return 1;  //some error occurred
    for (i = 0; i < maxLength; i++) {
        if (charArray[i] == '\n') {
            if (i != 0 && charArray[i - 1] == '\r') //cater for windows line endings
                i--;
            charArray[i] = '\0';
            return 0;  //all's well that ends well
        } else if (charArray[i] == '\0')
            return 0;  //smooth sailing fam
    }
    return 2;  //there was no string terminator
}


回答6:

Here you go:

#include "stdio.h"

//gets a line from the specified stream
int getline(char* charArray, int maxLength, FILE* stream) {
    int i;
    if (fgets(charArray, maxLength, stream) == NULL)
        return 1;  //some error occurred
    for (i = 0; i < maxLength; i++) {
        if (charArray[i] == '\n') {
            if (i != 0 && charArray[i - 1] == '\r') //cater for windows line endings
                i--;
            charArray[i] = '\0';
            return 0;  //all's well that ends well
        } else if (charArray[i] == '\0')
            return 0;  //smooth sailing fam
    }
    return 2;  //there was no string terminator
}

int main() {
    char money[4];
    printf("How much money do you have on you? ");
    getline(money, 4, stdin);
    printf("Oh really? $%d? Good. :)\n", atoi(money));
    char string[4];
    printf("Where is our next lesson?\n");
    getline(string, 4, stdin);
    printf("%s", string);
    return 0;
}


标签: c scanf