일부 내용을 파일에 인쇄하기 위해 C로 프로그램을 작성하고 다음을 인쇄할 수 있습니다. -
- 파일에 입력된 문자 수.
- 파일에 입력한 문자를 뒤집습니다.
먼저 쓰기 모드에서 파일을 열어 파일에 문자 수를 저장해 봅니다.
파일에 데이터를 입력하기 위해 아래에 언급된 논리를 사용합니다 -
while ((ch = getchar( ))!=EOF) {//after enter data press cntrl+Z to terminate
fputc(ch, fp);
} ftell, rewind, fseek 함수의 도움으로 이미 파일에 입력한 내용을 되돌릴 수 있습니다.
예시
다음은 일부 내용을 파일에 인쇄하고 문자 수를 인쇄하고 파일에 입력된 문자를 반전시키는 C 프로그램입니다 -
#include<stdio.h>
int main( ){
FILE *fp;
char ch;
int n,i=0;
fp = fopen ("reverse.txt", "w");
printf ("enter text press ctrl+z of the end");
while ((ch = getchar( ))!=EOF){
fputc(ch, fp);
}
n = ftell(fp);
printf ( "No. of characters entered = %d\n", n);
rewind (fp);
n = ftell (fp);
printf ("fp value after rewind = %d\n",n);
fclose (fp);
fp = fopen ("reverse.txt", "r");
fseek(fp,0,SEEK_END);
n = ftell(fp);
printf ("reversed content is\n");
while(i<n){
i++;
fseek(fp,-i,SEEK_END);
printf("%c",fgetc(fp));
}
fclose (fp);
return 0;
} 출력
위의 프로그램이 실행되면 다음과 같은 결과가 생성됩니다 -
enter text press ctrl+z of the end TutorialsPoint ^Z No. of characters entered = 18 fp value after rewind = 0 reversed content is tnioPslairotuT