[완료] fgets가 이상합니다. (제가 이상할지도 ㅠ)
#include
#include
#include //exit()
#define BUF 100
void error_handling(char *message);
int main(void)
{
FILE *fp = NULL;
char buf[BUF];
char *ret = NULL;
int fputs_ret = 0;
memset((void*)buf, 0, sizeof(buf)*sizeof(char));
fp=fopen("C:\\fgets_test.txt","at");
if(fp==NULL)
{
error_handling("fopen error\n");
}
printf("Before fputs fp : %d\n",ftell(fp));
fputs_ret=fputs("KAAAAAAAAAA\n", fp);
if(fputs_ret<0)
{
error_handling("fputs error\n");
}
printf("After fputs fp : %d\n",ftell(fp));
printf("=================================\n");
printf("fputs ret %%d : %d\n",fputs_ret);
printf("fputs ret %%c : %d\n",fputs_ret);
printf("=================================\n");
printf("Before fgets fp : %d\n",ftell(fp));
ret=fgets(buf, sizeof(buf), fp);
if(ret==NULL)
{
error_handling("fgets ret is NULL\n");
}
printf("After fgets fp : %d\n",ftell(fp));
printf("=================================\n");
printf("ret %%p : %p\n", ret);
printf("buf %%s : %s\n", buf);
fclose(fp);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
=====================================================================================
fgets를 연습해보고 있는데요
왜 파일과 콘솔창에 이상한 한글이 출력될까요?
뭐가 잘못된 거죠?
전 아무리 봐도 잘 몰라서 질문 드립니다. ㅠ
질문하신 내용과는
질문하신 내용과는 다르지만...
fopen 사용시에 쓰신 "at" 에서 t 모드는 어떤거죠? ;; window에서 코딩 해본 경험이 없어서.. 혹시나 해서 여쭤봅니다...
man page 에 나와 있는 모드는...
r Open text file for reading. The stream is positioned at the beginning of the file.
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file,
가 다인데...
올려주신 코드로 테스트 해보면 fgets() 수행 이후 mode가 read에 대한 부분이 없기에... 위의 man page를 보시면 "a" 모드에서 지원하는건 writing at end of file입니다.. 그런 이유로 Bad File Description 에 해당하는 EBADF 가 발생하는군요... fopen의 mode를 한번 확인해보시기를 바랍니다.
이렇게 답변 달아주셔서 감사합니다.
우선 이렇게 답변 달아주셔서 감사합니다.
방금 막 해결을 하고 돌아왔습니다. 어제부터 뚫어져라 쳐다보니 오늘 뚫리는군요 ㅠ
(예상하신대로 window에서 작업을 하고 있습니다.)
r 모드로 바꾸니 제대로 동작을 하는군요.
fgets는 a mode나 w mode는 동작 하지 않는 거 같더라고요. 이유는 모르겠어요.
그래서 fputs에는 a mode로 열린 fp를 주고, fgets에는 r mode로 열린 fp를 줘서 성공 했습니다 !
참, 그리고 t mode는 txt 파일을 다루겠다라는 겁니다.
답변 주셔서 감사합니다. 오늘 하루도 좋은 하루 보내세요^^
윗분이 맨페이지에서
윗분이 맨페이지에서 가져와주신대로
r은 읽기전용, w는 쓰기전용, a도 쓰기전용입니다. w나 a에선 읽기(fgets())가 불가능하죠.
읽기와 쓰기를 동시에 하려면 r+나 w+, a+ 를 쓰시면 됩니다.
(셋의 차이는 현재 파일의 존재 유무에 따른 처리와, 파일 내용의 어디서부터 쓰거나 읽기 시작할지의 차이입니다.)
텍스트 모드 t와 상대되는 옵션으로는 바이너리 모드 b가 있습니다.
아하 그렇군요!!
a+ mode로 당장 다시 한번 시도해봐야겠습니다.
(멍청하게 fp를 두개나 할당하다니 ㅠㅠ)
답변 감사합니다.!!
좋은 하루 보내세요!!!
저도 덕분에 text
저도 덕분에 text mode와 binary mode가 있다는걸 알게 됐네요 감사합니다 ^^;
댓글 달기