在C语言中,可以使用fscanf函数来读取txt文档中用空格符分开的数据。下面是一个示例代码:

#include <stdio.h>

int main() {
    FILE *file;
    int num1, num2, num3;
    
    // 打开txt文档
    file = fopen("data.txt", "r");
    
    if (file == NULL) {
        printf("无法打开文件。\n");
        return 1;
    }
    
    // 读取数据
    fscanf(file, "%d %d %d", &num1, &num2, &num3);
    
    // 输出数据
    printf("读取到的数据为:%d, %d, %d\n", num1, num2, num3);
    
    // 关闭文件
    fclose(file);
    
    return 0;
}

上述代码假设txt文档中有三个整数,每个整数用空格符分开。代码首先打开txt文档,然后使用fscanf函数读取三个整数,然后输出这些数据,最后关闭文件。请注意,代码中的"data.txt"应替换为实际的txt文档路径

怎样将txt文档中用空格符分开的数据分别读取出来C语言

原文地址: https://www.cveoy.top/t/topic/hKTV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录