问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

C语言中如何输入若干行的数据

创作时间:
作者:
@小白创作中心

C语言中如何输入若干行的数据

引用
1
来源
1.
https://docs.pingcode.com/baike/1298004

在C语言中输入多行数据是编程中的常见需求,无论是处理用户输入还是读取文件内容。本文将详细介绍如何通过循环控制输入、数组存储数据以及使用字符串处理函数等方法来实现多行数据的输入,并结合实例进行说明。

一、使用循环控制输入

使用循环控制输入是处理多行数据输入的基本方法。通过设定循环次数,可以实现对每行数据的读取。

1.1 使用for循环

在C语言中,for循环是最常用的循环控制结构之一。可以通过设定循环次数来控制输入的行数。

#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of lines: ");
    scanf("%d", &n);
    getchar();  // To consume the newline character left by scanf
    char line[100];
    for (int i = 0; i < n; i++) {
        printf("Enter line %d: ", i + 1);
        fgets(line, sizeof(line), stdin);
        printf("You entered: %s", line);
    }
    return 0;
}

在这个例子中,for循环控制了输入的行数,并使用fgets()函数读取每行数据。

1.2 使用while循环

while循环是另一种常见的循环控制结构,通过条件控制循环的执行。

#include <stdio.h>

int main() {
    int n, i = 0;
    printf("Enter the number of lines: ");
    scanf("%d", &n);
    getchar();  // To consume the newline character left by scanf
    char line[100];
    while (i < n) {
        printf("Enter line %d: ", i + 1);
        fgets(line, sizeof(line), stdin);
        printf("You entered: %s", line);
        i++;
    }
    return 0;
}

二、使用数组存储数据

使用数组存储数据可以方便后续的处理和操作。可以定义一个二维字符数组来存储多行数据。

2.1 二维字符数组

二维字符数组是一种常见的数据结构,用于存储多行字符串。

#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of lines: ");
    scanf("%d", &n);
    getchar();  // To consume the newline character left by scanf
    char lines[n][100];
    for (int i = 0; i < n; i++) {
        printf("Enter line %d: ", i + 1);
        fgets(lines[i], sizeof(lines[i]), stdin);
    }
    printf("\nYou entered:\n");
    for (int i = 0; i < n; i++) {
        printf("%s", lines[i]);
    }
    return 0;
}

在这个例子中,二维字符数组lines存储了多行输入的数据,方便后续的输出和处理。

三、使用字符串处理函数

C语言提供了丰富的字符串处理函数,可以方便地处理多行输入的数据。

3.1 使用fgets()函数

fgets()函数用于读取一行字符串,包括空格和换行符。

#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of lines: ");
    scanf("%d", &n);
    getchar();  // To consume the newline character left by scanf
    char line[100];
    for (int i = 0; i < n; i++) {
        printf("Enter line %d: ", i + 1);
        fgets(line, sizeof(line), stdin);
        printf("You entered: %s", line);
    }
    return 0;
}

3.2 使用scanf()函数

scanf()函数也可以用于读取多行数据,但需要处理换行符。

#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of lines: ");
    scanf("%d", &n);
    getchar();  // To consume the newline character left by scanf
    char line[100];
    for (int i = 0; i < n; i++) {
        printf("Enter line %d: ", i + 1);
        scanf("%[^n]%*c", line);  // Read until newline
        printf("You entered: %sn", line);
    }
    return 0;
}

四、处理输入的边界情况

在实际应用中,处理输入的边界情况非常重要,如输入行数为0、大于预期的行数等。

4.1 输入行数为0

当输入行数为0时,程序应直接退出或提示用户重新输入。

#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of lines: ");
    scanf("%d", &n);
    getchar();  // To consume the newline character left by scanf
    if (n <= 0) {
        printf("Number of lines must be greater than 0.\n");
        return 1;
    }
    char line[100];
    for (int i = 0; i < n; i++) {
        printf("Enter line %d: ", i + 1);
        fgets(line, sizeof(line), stdin);
        printf("You entered: %s", line);
    }
    return 0;
}

4.2 输入行数大于预期

当输入行数大于预期时,可以动态分配内存来存储数据。

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

int main() {
    int n;
    printf("Enter the number of lines: ");
    scanf("%d", &n);
    getchar();  // To consume the newline character left by scanf
    if (n <= 0) {
        printf("Number of lines must be greater than 0.\n");
        return 1;
    }
    char **lines = (char **)malloc(n * sizeof(char *));
    for (int i = 0; i < n; i++) {
        lines[i] = (char *)malloc(100 * sizeof(char));
        printf("Enter line %d: ", i + 1);
        fgets(lines[i], 100, stdin);
    }
    printf("\nYou entered:\n");
    for (int i = 0; i < n; i++) {
        printf("%s", lines[i]);
        free(lines[i]);
    }
    free(lines);
    return 0;
}

五、实际应用中的输入处理

在实际应用中,多行输入数据的处理通常涉及文件读取、数据解析等操作。

5.1 从文件读取数据

从文件读取多行数据是实际应用中常见的需求。可以使用fopen()fgets()等函数读取文件中的多行数据。

#include <stdio.h>

int main() {
    FILE *file = fopen("data.txt", "r");
    if (file == NULL) {
        printf("Could not open file.\n");
        return 1;
    }
    char line[100];
    while (fgets(line, sizeof(line), file) != NULL) {
        printf("%s", line);
    }
    fclose(file);
    return 0;
}

5.2 数据解析与处理

在读取数据后,通常需要解析和处理数据,可以使用字符串处理函数如strtok()sscanf()等。

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

int main() {
    char input[] = "123,456,789\nabc,def,ghi";
    char *line = strtok(input, "\n");
    while (line != NULL) {
        char *token = strtok(line, ",");
        while (token != NULL) {
            printf("%s ", token);
            token = strtok(NULL, ",");
        }
        printf("\n");
        line = strtok(NULL, "\n");
    }
    return 0;
}

六、总结

在C语言中输入若干行的数据,可以通过多种方法实现,如使用循环控制输入、使用数组存储数据、使用字符串处理函数等。实际应用中,还需要考虑输入的边界情况和数据解析处理。通过合理使用这些方法,可以高效地处理多行输入数据。

无论是初学者还是有经验的开发者,都可以根据具体需求选择合适的方法来实现多行数据的输入和处理。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号