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

来源:互联网 发布:qq农场刷经验软件 编辑:程序博客网 时间:2024/06/05 23:58

 

 /** 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    1024int 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;}