zoukankan      html  css  js  c++  java
  • 文件读格式字符串【CSDN】

    View Code
    //in.txt:
    //File Flag 2DPatch
    //1 3216
    //2 3216 (2548-3216 x 4)
    //3 3216 (2552-3216 x 4)
    //4 3212 (2556-3212 x 4)
    //5 3208 (2564-3216 x 4)
    //6 3204 (2572-3216 x 4)
    //7 3196 (2580-3216 x 4)
    //8 3192 (2588-3216 x 4)
    //9 3188 (2596-3216 x 4)
    //10 3184 (2548-3216 x 4)
    //11 3180 (2548-3216 x 4)
    //文档就是这样的格式,怎么才能提取出第一列,第二列,和第三列中的第一部分呢?
    //结果应该是这样子的:
    //out.txt
    //1 3216
    //2 3216 2548
    //3 3216 2552
    //4 3212 2556
    //5 3208 2564
    //6 3204 2572
    //7 3196 2580
    //8 3192 2588
    //9 3188 2596
    //10 3184 2548
    //11 3180 2548
    #include <stdio.h>
    FILE *fi,*fo;
    char ln[80]={0};
    int a,b,c;
    void main()
    {
    fi=fopen("in.txt","r");
    if (NULL==fi)
    {
    printf("Can not open file in.txt!\n");
    return;
    }
    fo=fopen("out.txt","w");
    if (NULL==fo)
    {
    fclose(fi);
    printf("Can not open file out.txt!\n");
    return;
    }
    while (1)
    {
    if (NULL==fgets(ln,80,fi)) break;
    if (3==sscanf(ln,"%d%d (%d",&a,&b,&c))
    {
    fprintf(fo,"%d %d %d\n",a,b,c);
    }
    else if (2==sscanf(ln,"%d%d",&a,&b))
    {
    fprintf(fo,"%d %d\n",a,b);
    }
    }
    fclose(fo);
    fclose(fi);
    }
  • 相关阅读:
    Jquery
    JavaScript
    poj--2115 C Looooops
    poj--3970 party
    poj 1061 青蛙的约会
    hdu1250--Hat's Fibonacci
    2318--TOYS
    扩展欧几里得--让你一次刷个够
    关于大数加法的解法
    有关环形数组的约瑟夫问题
  • 原文地址:https://www.cnblogs.com/guyan/p/2277933.html
Copyright © 2011-2022 走看看