c - 逐行读取文件并将逗号分隔的每个字符扫描到C中的单独变量中

标签 c file-io

必须读取格式如下的file.txt: 12AA,abc12\n 4CCC,cde15\n

我想逐行读取文件并将逗号分隔值存储在单独的变量中。

fp = fopen("file.txt", "r");

while(fgets(buffer, 255, (FILE*) fp)) 
{
    fscanf(fp, "%s %s", acc_no, package);
    printf("%s\n", acc_no);
    printf("%s\n", package);
}

fclose(fp);

我不仅希望读取和打印变量,还希望将它们存储在单独的变量中。关于如何做到这一点的建议?

最佳答案

strchr() 可以帮助:

while (fgets(str, sizeof str, fp)) {
    char *arr[2], *ptr;

    arr[0] = str;
    if ((ptr = strchr(str, ','))) {
        arr[1] = ptr + 1;
        *ptr = '\0';
    } else {
        exit(EXIT_FAILURE);
    }
    printf("<%s> <%s>\n", arr[0], arr[1]);
}

请注意,您可能需要去除 fgets() 留下的尾随换行符或省略 printf

中的 \n

如果您需要将这些字符串存储在新内存中,那么需要做更多的工作,您可以 realloc() 或使用链表(当您不这样做时,总是首选链表事先知道行数):

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

struct node {
    void *data;
    struct node *next;
};

/* Returns a pointer to an allocated string */
extern char *strdup(const char *);

/* Insert a node and returns a pointer to the data */
static void *enqueue(struct node **root, void *data)
{
    struct node *node;

    if (root == NULL) {
        return NULL;
    }
    node = malloc(sizeof *node);
    if (node == NULL) {
        return NULL;
    }
    if (*root == NULL) {
        node->next = node;
    } else {
        node->next = (*root)->next;
        (*root)->next = node;
    }
    node->data = data;
    *root = node;
    return data;
}

/* Delete a node and returns a pointer to the data */
static void *dequeue(struct node **root)
{
    struct node *node;
    void *data = NULL;

    if (root == NULL) {
        return NULL;
    }
    node = *root;
    if (node != NULL) {
        node = node->next;
        data = node->data;
        if (*root == node) {
            *root = NULL;
        } else {
            (*root)->next = node->next;
        }
        free(node);
    }
    return data;
}

int main(void)
{
    struct node *head = NULL;
    char str[256];
    char **arr;
    char *ptr;

    /* While we don't hit EOF */
    while (fgets(str, sizeof str, stdin)) {
        /* Reserve space for 2 pointers */
        arr = malloc(sizeof(*arr) * 2);
        if (arr == NULL) {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
        /* If we find a comma */
        if ((ptr = strchr(str, ','))) {
            /* Store the second string */
            arr[1] = strdup(ptr + 1);
            if (arr[1] == NULL) {
                perror("strdup");
                exit(EXIT_FAILURE);
            }
            /* Strip the string */
            *ptr = '\0';
        /* If we don't find a comma */
        } else {
            exit(EXIT_FAILURE);
        }
        /* Store the first string */
        arr[0] = strdup(str);
        if (arr[0] == NULL) {
            perror("strdup");
            exit(EXIT_FAILURE);
        }
        /* Add a node to the queue*/
        if (enqueue(&head, arr) == NULL) {
            perror("enqueue");
            exit(EXIT_FAILURE);
        }
    }
    /* While nodes in queue show the data and free */
    while ((arr = dequeue(&head))) {
        printf("%s %s", arr[0], arr[1]);
        free(arr[0]);
        free(arr[1]);
        free(arr);
    }
    return 0;
}

关于c - 逐行读取文件并将逗号分隔的每个字符扫描到C中的单独变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53348989/

相关文章:

c++ - 将 C++ 函数指针转换为 c 函数指针

ruby - 未定义方法 `split' 为 nil :NilClass (NoMethodError) for an array

java - 想要在 Java 的属性文件中定义进程的状态序列

python - 在 Python 中关闭文件

c++ - C/C++远程消息队列的推荐

命令提示符与通过调试传递命令参数

c - 函数错误的隐式声明

c - 字符串数组计数

java - Java 同时读取多个文件

vb.net - 如何将“浏览到文件”对话框添加到VB.NET应用程序