같은 파일을 다른 모드로 열 때 다른 한쪽이 제대로 모드대로 작동돼지 않습니다.

dltkddyd의 이미지

#include <cstdio>
#include <iostream>
using namespace std;
#include <clocale>
 
void openfile() {
	cout<<"openfile() begins."<<endl;
	FILE* fp=fopen("testdoc12.cc","r");
	int ch=0;
cout<<fseek(fp, 0,SEEK_SET)<<endl;
	while(  (ch=fgetc(fp))!=EOF  ) {
		cout<<(char)ch;
	}
	cout<<endl;
	fclose(fp);
	cout<<"openfile() ends."<<endl;
 
}
 
int main() {
 
	int ch='a';
	FILE* fp2=fopen("testdoc12.cc","w");
	while(  ch<('z'+1)) {
		fputc(ch, fp2);
		ch++;
	}
	cout<<"main"<<endl;//test
 
 
	//fclose(fp2);//fclose()가 여기 있을 때에는 main에서 쓰기와 openfile 함수에서 읽기가 제대로 됩니다.
	openfile();
	fclose(fp2);//fclose 함수가 여기 있을 때에는 main에서의 쓰기는 제대로 될지라도, openfile 함수에서의 읽기가 제대로 되지 않습니다.
 
	return 0;
}

main에서 testdoc12.cc 파일을 쓰기 모드로 열어 쓰고, 그 파일을 닫지 않은 상태에서 openfile을 호출합니다. 그리고 이 openfile 함수는 testdoc12.cc 파일을 읽기 모드로 열어 main 함수에서 기록된 것을 읽으려 했는데, 기록된 것이 제대로 읽히지 않습니다. 혹시 openfile에서 파일스트림이 제대로 설정돼지 않았는가 해서 NULL 여부를 확인해보니 NULL 상태는 아니었습니다. 이 문제를 어떻게 해결해야 하나요? 반드시 호출원에서 쓰고 피호출부에서는 그 파일을 읽어야 하는데요. 모드가 다를 때에 저런 문제가 생깁니다.

bushi의 이미지

fflush()

dltkddyd의 이미지

감사합니다. 답변 덕분에 문제가 해결됐습니다. 기록돼지 못한 데이터가 있었다는 것이군요. fflush에 대해 검색해봤더니 다음과 같이 설명돼 있네요.

int fflush(FILE* stream)
주어진 스트림이 쓰기용으로 열려있다면, 출력 버퍼에서 기록돼지 않은 어떤 데이터라도 파일에 기록된다.

스트림이 널포인터이면, 그러한 모든 스트림들이 확 쏟아져 나온다.

모든 다른 경우에서 그 동작은 특정 라이브러리 구현에 의존적이다. 몇 가지 구현에서 읽기를 위해 열린 스트림을 비우는 일은 입력 버퍼가 비워지도록 한다. 그 스트림은 이 호출을 위해 열린 채로 남아있다.

파일이 닫힐 때에는 fclose 함수의 호출 때문에 또는 그 프로그램의 종결 때문에 그것과 관련된 모든 버퍼는 자동으로 비워진다.

반환되는 0은 성공을 의미한다. 오류가 발생하면, EOF가 반환되고 오류지정자가 설정된다.

참조사이트 http://www.cplusplus.com

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.