从标准输入中读取字符串

帕布罗德·阿塞罗

我正在尝试从stdin读取以下格式的行:

Boston "New York" "San Francisco" Memphis(请注意,其中带有空格的字符串在方括号之间。还请注意,每个城市名称都由一个空格分隔。)我试图一次用scanf读取此内容,先获取整行,然后标记化,但是使用不好的结果。我假装将所有内容存储在多维char数组中,以备后用。

关于如何解决这个问题有什么建议吗?先感谢您!

乌鸦

您可以阅读整行并自己轻松地对其进行解析。如果您遇到的第一个非空白字符不是“,则请阅读直到下一个空格。如果是,请阅读直到下一个“”,前提是您不必担心转义的引号。

这是一个简单的实现:

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

#define MAX_BUFFER 100
#define MAX_STRINGS 10

int main(void) {
    char buffer[MAX_BUFFER];

    if ( !fgets(buffer, MAX_BUFFER, stdin) ) {
        fprintf(stderr, "Couldn't get input.\n");
        return EXIT_FAILURE;
    }
    else {

        /*  Remove trailing newline, if present  */

        size_t length = strlen(buffer);
        if ( length && buffer[length - 1] == '\n' ) {
            buffer[length - 1] = '\0';
        }
    }

    char *my_strings[MAX_STRINGS + 1] = {NULL};
    int read_strings = 0;
    char *buf_ptr = buffer;

    while ( *buf_ptr && read_strings < MAX_STRINGS ) {
        char temp_buf[MAX_BUFFER] = {0};
        char *temp_ptr = temp_buf;

        /*  Skip leading whitespace  */

        while ( *buf_ptr && isspace(*buf_ptr) ) {
            ++buf_ptr;
        }

        if ( *buf_ptr ) {
            if ( *buf_ptr == '"' ) {

                /*  If starts with '"', read to next '"'...  */

                ++buf_ptr;      /*  Skip first "  */
                while ( *buf_ptr && *buf_ptr != '"' ) {
                    *temp_ptr++ = *buf_ptr++;
                }

                if ( *buf_ptr ) {
                    ++buf_ptr;  /*  Skip second "  */
                }
            }
            else {

                /*  ...otherwise, read to next whitespace  */

                while ( *buf_ptr && !isspace(*buf_ptr) ) {
                    *temp_ptr++ = *buf_ptr++;
                }
            }

            /*  Copy substring into string array  */

            my_strings[read_strings] = malloc(strlen(temp_buf) + 1);
            if ( !my_strings[read_strings] ) {
                fprintf(stderr, "Couldn't allocate memory.\n");
                return EXIT_FAILURE;
            }
            strcpy(my_strings[read_strings++], temp_buf);
        }
    }

    for ( size_t i = 0; my_strings[i]; ++i ) {
        printf("String %zu: %s\n", i + 1, my_strings[i]);
        free(my_strings[i]);
    }

    return 0;
}

样本输出:

paul@MacBook:~/Documents/src/scratch$ ./ql
Boston "New York" "San Francisco" Memphis
String 1: Boston
String 2: New York
String 3: San Francisco
String 4: Memphis
paul@MacBook:~/Documents/src/scratch$ ./ql
a quoted "word" and "some quoted words" and an "unclosed quoted string
String 1: a
String 2: quoted
String 3: word
String 4: and
String 5: some quoted words
String 6: and
String 7: an
String 8: unclosed quoted string
paul@MacBook:~/Documents/src/scratch$ 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从标准输入中读取字符串

来自分类Dev

在C中从文件读取字符串输入

来自分类Dev

在Haskell中读取字符串

来自分类Dev

在C ++中读取字符串

来自分类Dev

在Haskell中读取字符串

来自分类Dev

在C ++中读取字符串

来自分类Dev

从文件中读取字符串

来自分类Dev

如何从扫描仪的单行输入中读取字符串和输入

来自分类Dev

如何读取字符串中的字符并根据输入返回?

来自分类Dev

使用scanf读取字符串作为输入

来自分类Dev

从输入文件SAS读取字符串

来自分类Dev

从输入文件SAS读取字符串

来自分类Dev

标准C ++中的字符串输入

来自分类Dev

如何在GNU Guile中读取字符串以获取用户输入?

来自分类Dev

从标准输入读取所有文本到字符串

来自分类Dev

如何匹配从标准输入读取的字符串?

来自分类Dev

在C中逐字符读取字符串

来自分类Dev

ifstream错误从字符串中读取字符

来自分类Dev

在C中逐字符读取字符串

来自分类Dev

如何从标准输入中读取单个字符串?

来自分类Dev

如何从Java中的标准输入读取python二进制字符串

来自分类Dev

如何从输入字符串中获取字符数?

来自分类Dev

如何从char **中读取字符串?

来自分类Dev

从共享内存C ++ POSIX中读取字符串

来自分类Dev

在NetLogo中读取字符串变量“ NaN”

来自分类Dev

如何从HBase结果中读取字符串?

来自分类Dev

Java按元素读取字符串中的元素

来自分类Dev

在Python中从字符串读取字节

来自分类Dev

Qt在Linux中无法正确读取字符串