c语言数据读入---sscanf、fscanf

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>

using namespace std;

const int Max = 10000;
char str[Max], name[Max], f1[Max], f2[Max], idNum[Max];

int main()
{
    FILE *fd;
    if((fd = fopen("C:\Users\501-PC\Desktop\station2.txt", "r")) == NULL){
        printf("Cant open txt");
        exit(0);
    }
    while(fscanf(fd, "%s", str) != EOF){
        int ret = sscanf(str, "%[^^]^%[^^]^%[^^]^%s", idNum, name, f1, f2);
        int id = atoi(idNum);
        double f = atof(f1), ff = atof(f2);
        cout<<id<<" "<<name<<" "<<f<<" "<<ff<<endl;
        memset(str, 0, sizeof str);
        memset(name, 0, sizeof name);
        memset(f1, 0, sizeof f1);
        memset(f2, 0, sizeof f2);
        memset(idNum, 0, sizeof idNum);
    }
    return 0;
}

/*
input file format

0^红庙路口东^116.487676^39.921222
1^红庙路口西^116.483319^39.921494
2^小庄路口东^116.477741^39.922015
3^呼家楼西^116.466469^39.925372
...
%[]   指定字符集,即加了字符规则的'%s
^     取反
%[^,] 取非','的内容,直到遇到','

*/
原文地址:https://www.cnblogs.com/luntai/p/6699435.html