C语言 读取文件中特定数据

//读取文件数据

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

struct jiang{
    char one[50];
    char two[50];
}str[13];


void main(){
    //定义数据
    int arr[5] = { 0 };
    //定义文件路径
    char path[40] = "E:\Look\b.txt";
    //定义文件指针
    FILE *pf=NULL;
    //打开读写文件
    pf = fopen(path, "r");
    //判断是否打开文件
    if (pf==NULL)
    {
        printf("文件打开失败!");
        return;
    }
    int index = 0;
    //读文件
    while (!feof(pf)){
        //	就是文本中的空格,不管有多少个空格 都是一个	
        fscanf(pf, "%s	%s
", str[index].one, str[index].two);
        index++;
    }
    //关闭文件指针
    if (pf!=NULL)
    {
        fclose(pf);
    }
    for (int i = 0; i < 13; i++)
    {
        printf("%s,%s
", str[i].one, str[i].two);
    }
    system("pause");
}

原文地址:https://www.cnblogs.com/zhanggaofeng/p/5163532.html