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");
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM