简单文本处理

我这次要处理的文本内容为:

T= 6

1 3 3 4 5 6

处理后期望为:

T= 6

0 2 2 3 4 5 

简单来说,就是数据个数T不改变,但是对应内容作出改变~

 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 int main()

 4 {

 5     FILE *fstart;

 6     FILE *fend;

 7     fstart=fopen("test.txt","r");

 8     if(fstart==NULL)

 9     {

10         perror("OPEN TEST ERROR");

11         exit(EXIT_FAILURE);

12     }

13     fend=fopen("end_test.txt","w");

14     if(fend==NULL)

15     {

16         perror("OPEN END_TEST ERROR");

17         exit(EXIT_FAILURE);

18     }

19     //定位到文件头

20     rewind(fstart);

21     rewind(fend);

22 

23     int temp;

24     //处理前半部分

25     fscanf(fstart,"T= %d\n",&temp);

26     fprintf(fend,"T= %d\n",temp);

27     //处理数字部分

28     while(feof(fstart)==0) //未到文件尾,则返回0

29     {

30         fscanf(fstart,"%d",&temp);

31         fprintf(fend,"%d ",temp-1);

32     }

33     fclose(fstart);

34     fclose(fend);

35     return 0;

36 }

 

你可能感兴趣的:(处理)