scanf() multiple words in C

2019-06-14 20:51发布

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.

标签: c scanf
6条回答
淡お忘
2楼-- · 2019-06-14 20:55

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
}
查看更多
老娘就宠你
3楼-- · 2019-06-14 20:57

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);
}
查看更多
冷血范
4楼-- · 2019-06-14 20:59

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.

查看更多
▲ chillily
5楼-- · 2019-06-14 21:12

you can follow this:

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

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

    return 0;
查看更多
\"骚年 ilove
6楼-- · 2019-06-14 21:14

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;
}
查看更多
地球回转人心会变
7楼-- · 2019-06-14 21:15

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

查看更多
登录 后发表回答