FILE에 관련된 건데요..

toold의 이미지

아래코드는 어떤 함수에 들어있습니다..
그리고 그 함수는 main()함수에 있는 while문안에 있습니다...
while문 안에서 이 함수로 contents의 내용을 계속 바꾸어서 보내주고요..
그래서 아래 코드로 하려는것은 계속 바뀌는 contents의 내용을 md5_siteaddr변수란 이름을 같는 파일안에 계속해서 이어서 저장하려는건데요..
이상하게 저장이 잘 안됩니다...
코드상에 문제가 있나요..?

	if((fp=fopen(md5_siteaddr, "a")) != NULL){	
		write(fileno(fp), contents, sizeof(contents));
		fclose(fp);	
	}else printf("파일 생성 실패\n");

전체적으로 보면요...
대충 아래와 같은 모양이거든요
int main()
{
	while(계속 반복)
	{
		......
		......
		xxx(md5_siteaddr, contents);
		......
		......
	}

	return 0;
}

void xxx(char *md5_siteaddr, char *contents)
{
		......
		......
	if((fp=fopen(md5_siteaddr, "a")) != NULL){	
		write(fileno(fp), contents, sizeof(contents));
		fclose(fp);	
	}else printf("파일 생성 실패\n");
		......
		......

}
서지훈의 이미지

if((fp=fopen(md5_siteaddr, "a")) != NULL){ 이 부분을 if((fp=fopen(md5_siteaddr, "a+")) != NULL){ 이렇게 바꿔 보시길...

지금 리눅머신이 없어서 man 을 못봐 답답한데...
man fopen() 도 함 읽어 보시길...

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

alwaysrainy의 이미지

write(fileno(fp), contents, sizeof(contents));

이 부분을

write(fileno(fp), contents, strlen(contents));

해주시면 될 듯 ^^
참고로 fopen 함수 man 페이지 자료 덧붙입니다.

Quote:
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 stream is positioned at the end of the file.

부디 성공하시기를요 ^^

---------------------------------------
세계는 넓고, 할일은 많다.

서지훈의 이미지

write(fileno(fp), contents, sizeof(contents)); 이 코드를 아래와 같이 바꿔서 해보시길... write(fileno(fp), contents, strlen(contents)); 아님 간단하게... fprintf(fp, "%s", contents); 이렇게 해보시덩가...

제가 자세히 다시보니...
sizeof() 이걸 잘못 사용해서 그렇네요...
이넘은 contents의 pointer 크기(4: 주소체계가 4bytes 인지라)가 4인지라...
4개만 항상 찍힐 듯...
그런가요?

걍... 결과랑 같이 보내 주셨다면 더 싶게 답변이 갔을것을...-_-ㅋ

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

추신_정말 머리 속에서 컴파일 하는건 오류가 넘많아...-_-ㅋ

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

inu111의 이미지

제 생각에도 sizeof(contents) 대신 strlen(contents)가 될 것 같은데요...

도움 되시길^^;;

ASVU-forever

toold의 이미지

서지훈 wrote:
write(fileno(fp), contents, sizeof(contents)); 
제가 자세히 다시보니...
sizeof() 이걸 잘못 사용해서 그렇네요...
이넘은 contents의 pointer 크기(4: 주소체계가 4bytes 인지라)가 4인지라...
4개만 항상 찍힐 듯...
그런가요?
</blockquote></div>
네...4글자만 계속해서 저장됐었죠..^^...
답변 주신분들 감사드립니다..
cinsk의 이미지

참고로, 웬만하면 low level I/O인 read(2), write(2) 함수를 stream I/O인 FILE, fXXX 계열의 함수와 섞어 쓰시 마시길 바랍니다.

write(2)로 쓸거라면 fopen(3) 대신 open(2)을 쓰시고, fopen(3)을 쓸거라면 write(2) 대신 fwrite(3)나 fputs(3)를 쓰시길 바랍니다.

호환성 떨어집니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.