파일 open질문..

soylove의 이미지

#include <stdio.h>
#include <sys/file.h>
#include <errno.h>
main()
{
int fd;
fd = open("nonsexist.txt", O_RDONLY); /* 시스템 호출 */
if (fd==-1)
{
printf("1:errno = %d\n", errno); /* errno: 전역변수 */
perror("1:main");
} /* perror() : 표준 함수 */
fd = open("nonsexist.txt", O_RDONLY | O_CREAT, 0644);
printf("3:errno = %d\n", errno);
perror("3:main");
}

간단한 거 같은데 도대체 알수가 없어서 질문드립니다.
위 소스 3:errno 에서 오류가 납니다. errno=29 illegal seek이랍니다
printf("3:errno = %d\n", errno);
perror("3:main");
위 두 줄을 if(fd==-1)로 묶었을 때는 당연히 에러가 안납니다. 파일이 없으면 생성해주기로 했으니까..
근데 if문을 없애고 그냥 실행하면 에러번호 29번이 나는데.. 왜 그런건지.. 그리고 에러번호 29번이 의미하는게 뭔지 좀 알려주세요..
여기저기 찾아봐도 29번 에러에 대한 설명은 찾기가 힘드네용;;;
짐작에는 위에 발생됐던 에러번호가 영향을 미친거 같기도 한데;;

ixevexi의 이미지

테스트는 안해봤지만 fclose도 안해주시고다시 여셨네요
두번째 fd는 아마 -1일겁니다 ^^

C++, 그리고 C++....
죽어도 C++

soylove의 이미지

우선 이 코드는 nonsexist.txt가 없는 상황에서 시작했기 때문에 첫번째에서는 당연히 에러가 납니다. 또 nonsexist.txt가 만들어지지 않았구요
문제는 두번째에서 파일이 없으면 만들라고 옵션을 붙였을때 왜 errno=29가 뜨는지 모르겠습니다.
fd는 -1이 아닙니다. if문으로 묶으면 에러가 나지 않으니까요.

익명 사용자의 이미지

두번째 open 하실때 flag 를 read only 로 하시고 mode flag 에는 0644 로 쓰기 모드를 줘서 위와 같은 에러가 발생하는 듯 합니다.
제가 뒤에 mode 를 0444 로 해서 테스트 해보니 에러는 발생하지 않습니다.

jiee의 이미지

#include <stdio.h>
#include <sys/file.h>
#include <errno.h>
main()
{
        int fd;
        fd = open("nonsexist.txt", O_RDONLY);   /* 시스템 호출 */
        if (fd==-1)
        {
                printf("1:errno = %d\n", errno);        /* errno: 전역변수 */
                perror("1:main");
        }  /* perror() : 표준 함수 */
         fd = open("nonsexist.txt", O_RDONLY | O_CREAT, 0644);
                printf("3:errno = %d\n", errno);
                perror("3:main");
}

Quote:
근데 if문을 없애고 그냥 실행하면 에러번호 29번이 나는데.. 왜 그런건지.. 그리고 에러번호 29번이 의미하는게 뭔지 좀 알려주세요..

"Illegal seek" means that you are trying to move the read/write pointer on a device where it doesn't make sense, like a pipe or a serial line, instead of a regular file

fd = open("nonsexist.txt", O_RDONLY | O_CREAT, 0644);
                printf("3:errno = %d\n", errno);
                perror("3:main");

코드에서 open 콜 자체는 문제가 없습니다. 리눅스 구현차원에서 그 밑에 errno가 29번으로 셋되는 것 같습니다. open은 오류가 발생하면 -1을 리턴하면서 errno을 셋하는데 위의 open은 문제가 없이 실행되는 구문입니다. 그러므로 errno은 위의 open과 무관하게 셋된다고 생각됩니다.

아래 소스를 함 실행해보면,
초기에는 errno값이 0이지만, 일단 open콜에서 에러가 발생한 다음에는 errno값을 0으로 초기화에도 다시 errno값이 29로 셋되는 것을 확인 할 수 있습니다.

     int main()
     {
              printf("errno = %d\n", errno);
              perror("main");
     
              int fd;
              fd = open("nonexit.txt", O_RDONLY);
              if( fd == -1 ) {
                      printf("errno = %d\n", errno);
                      perror("main");
              }
              fd = open("/", O_WRONLY);
              if( fd == -1 ) {
                      printf("errno = %d\n", errno);
                      perror("main");
              }
              errno = 0;
              perror("main");
     
              printf("errno = %d\n", errno);
              perror("main");
     
              errno = 0;
              perror("main");
     
              return 0;
     }

ps. 수고하세요..^^

토나오게...

댓글 달기

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