C언어 파일처리 관련 문제 질문 드립니다.
조회수 519회
replit에서 코딩을 하는데 text 문서를 만들고 그 문서의 내용을 읽어와서 다른 text문서에 거꾸로 저장하는 코드를 어떻게 작성해야 할지 몰라서 질문드립니다.
int main(void) {
float f[3];
scanf("%f %f %f", f, f+1, f+2);
FILE *w_file;
w_file = fopen("text.tx", "w");
// w write text 파일 크기 0으로 초기화
fprintf(w_file, "%f %f %f\n", f[0], f[1], f[2]);
fclose(w_file);
FILE *a_file;
a_file = fopen("text.tx", "a");
//a append 추가 / 파일의 끝에서 시작
fprintf(a_file, "a for append.\n");
fclose(a_file);
FILE *fgetc_file, *fputc_file;
fgetc_file = fopen("text.tx", "r");
fputc_file = fopen("fputc.tx", "w");
char ch;
ch = fgetc(fgetc_file); // 1byte씩 읽어옴
while(ch != EOF){ // End Of File
printf("%c", ch);
fputc(ch, fputc_file);
ch = fgetc(fgetc_file);
}
fclose(fgetc_file);
fclose(fputc_file);
// fputc.tx를 읽어와서, text 값을 back.tx에 거꾸로 저장하는 코드 작성
FILE *b_file;
b_file = fopen("back.tx", "a+");
fclose(b_file);
return 0;
}
입력예시
text 파일에
1.000000 2.000000 3.000000
a for append.
이런 식으로 저장되어 있을 때
출력예시
.dneppa rof a
000000.3 000000.2 000000.1
이렇게 나오도록 하고 싶은데 for 문을 써서 만들어 지나요? 아님 다른 함수를 써야하는 건가요?
댓글 입력