我很喜欢的一段程序——fgets()与strtol()的结合使用

 

 /*
* start_id_from_0.c
*  In the asm_45.Arc, the vertex id is started from 1 and ended at 45083421(include the skipped vertices), but in GPS,
*the vertex id shoule be started from 0. And the problem is I just modify the first column to let the vertex id start
*from 0, but I don't modify the trailing vertex id either, so this is this program function---Reserve the first column
*and the other vertex id minus 1.
*
* Copyright@Wang Jiawei, Shenzhen BGI, China, 2012-11-26
* The latest revision@Wang Jiawei, Shenzhen BGI, China, 2012-11-26
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_LINE_LEN    1024

int start_id_from_0(char* filename)
{
         FILE    *fin, *fout;
         long    original_id;
         char    restarted_filename[100]; 

         char    line[MAX_LINE_LEN];

         strcpy(restarted_filename, filename);
         strcat(restarted_filename, ".restarted");

         printf("The restarted_filename=%s\n", restarted_filename);



         fin = fopen(filename, "r");
         if (fin == NULL) {
                 printf("Can not open the file %s\n", filename);
         }
         fout = fopen(restarted_filename, "w+");
         if (fout == NULL) {
                 printf("Can not open the file %s\n", restarted_filename);
         }

         memset(line, 0, MAX_LINE_LEN);
         char    *source_str, *stopped_str;


         long i = 0;
         long j = 0;
         printf("------------0--------------\n");
         while(fgets(line, MAX_LINE_LEN, fin) != NULL) {
                 source_str = line;
                 stopped_str = line;

                 i++;
                 j = 0;

                 char    *temp;
                 while(1)  {
                         temp = stopped_str;
                         original_id = strtol(source_str, &stopped_str, 10);
                         if (temp == stopped_str)
                                 break;
                         if (j == 0) {

                                 if (j == 0) {

                         } 

                        else {
                                 fprintf(fout, "%ld  ", original_id - 1);
                         }
                         j++;
                        source_str = stopped_str;

                 }
                fprintf(fout, "\n");
                memset(line, 0, MAX_LINE_LEN);
         }
         fclose(fin);
         fclose(fout);
         printf("the total line = %ld\n", i);
         return 0;
}

int main()
{
         start_id_from_0("new_data6.Arc.del_wgt");

         return 0;
}


 

 

 



 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值