c언어에서 jpg파일복사하려고 합니다.

익명 사용자의 이미지

FILE *rfile = fopen("file.JPG","rb");
이 코드로 파일을 읽은 뒤
while(!feof(rfile))
{
A = fgets(strtemp,sizeof(strtmpe)) // 파일 내용을 받아서 A에 저장
strcat(strsum,A) // strsum에 모든 내용을 붙여서 저장
}

이런 코드로 이미지를 읽어서 새로운 파일을 w모드로 오픈해서 strsum내용을복붙했으나
이미지가 만들어지지 않네요.

제가 잘못한 방법이라도 있는 건가요??

라스코니의 이미지

fread(), fwrite()를 쓰셔야 겠죠.

jick의 이미지

C 입문서에서 문자열에 대한 부분을 차근차근 잘 읽어보시고요, NUL 문자('\0')가 들어있는 데이터에 대고 strcat을 하면 무슨 일이 일어날지 생각해 보세요.

Jpeg은 바이너리 데이터기 때문에 당연히 NUL 문자가 들어있습니다. (주: NULL과는 전혀 다른 거니까 혼동 주의.)

다음 예제의 결과가 어떻게 될지 생각을 해보시는 것도 좋겠습니다.

#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char s[20] = "abc\0!!!";
    strcat(s, "hello");
    printf("%s\n", s);
    return 0;
}
김정균의 이미지

이런 문제 때문에, thread safe 처럼 binary safe 라는 용어도 있습니다.
보통 binary safe api 들은 function argument 에 length 를 꼭 가지고 있죠.

raymundo의 이미지

위에 다른 분들이 말씀해주신 것 외에도...

fgets 는 파일의 끝에서는 NULL을 반환하는데 그게 strcat의 두번째 인자로 들어가면 문제가 될 거고요,

feof()는 파일의 끝에서 파일을 더 읽으려다가 실패한 "이후"에 참이 됩니다. 즉 실패한 이유가 파일의 끝에 도달했기 때문인지를 확인할 때 쓸 수 있지, 읽기 "전"에 더 읽을 수 있는지 확인하는 용도로 쓸 수 없습니다.

좋은 하루 되세요!

lalupo20의 이미지

질문자의 이미지

다들 신경써주셔서 정말 감사드립니다 ㅜㅜ 과제하고 학교다니느라 시간이 늦어서 답변이 늦었습니다.
답변해주신 모든 분들 정말 감사드리고 답변해주신대로 잘 해결했습니다!!
정말 감사드립니다!!

댓글 달기

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