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)){
        //\t就是文本中的空格,不管有多少个空格 都是一个\t
        fscanf(pf, "%s\t%s\n", str[index].one, str[index].two);
        index++;
    }
    //关闭文件指针
    if (pf!=NULL)
    {
        fclose(pf);
    }
    for (int i = 0; i < 13; i++)
    {
        printf("%s,%s\n", str[i].one, str[i].two);
    }
    system("pause");
}

 

 

posted on 2016-01-27 15:51  寒魔影  阅读(3613)  评论(0编辑  收藏  举报

导航