C实现:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
void main()
{
char* poem="\n program is fun\n when the work is down\n if you wanna make your work also fun:\nuse c++!";
FILE* fp=fopen("zhj.txt","w");
fprintf(fp,"%s",poem);
fclose(fp);
char *str=(char*)malloc(256*sizeof(char));
char* ch=(char*)malloc(256*sizeof(char));
memset(str,'\0',256*sizeof(char));
memset(ch,'\0',256*sizeof(char));
int count=0;
printf("start\n");
FILE* fp1;
fp1=fopen("zhj.txt","r");
while(!feof(fp1))
{
fgets(str,256,fp1);
//fscanf(fp1,"%s",&str);
printf("%s\n",str);
count=count+1;
if(count>1&&count<5)
strcat(ch,str);
//fseek(fp1,1L,SEEK_CUR);
}
printf("end read,\nch=%s\n",ch);
fclose(fp1);
printf("end\n");
FILE* fp2=fopen("res.txt","w");
fprintf(fp2,"%s",ch);
fclose(fp2);
if(str) free(str);
if(ch) free(ch);
}
Python实现:
import sys
import string
poem='''\
Program is fun
when the work is done
if you wanna make your work also fun:
use python!
'''
f=file('poem.txt','w')
f.write(poem)
f.close()
f=file('poem.txt')
count=1
str1=' '
while True:
line = f.readline()
if len(line)==0:
break
elif count>1 and count<4:
str1 += line
count=count+1
print line
f.close()
f1=file('res.txt','w')
f1.write(str1)
f1.close()