read함수가 파일을 전부 다 못 읽는 문제와 저장되어 있던 배열이 없어지는 문제 ㅡㅡ;;;

kywpcm의 이미지

우선 초보입니다. ㅜㅜ

int fd;
char buffer[10000];

fd = open("/proc/1/smaps", O_RDONLY)) ;
read(fd, buffer, 10000);

이렇게 간단하게 파일을 열고 읽는 것인데, smaps파일을 전부 다 읽어오지 못합니다..
아시다시피 smaps파일은 그 프로세스의 정보를 담고있는데요, 한 2/3가량의 정보만 buffer에 저장됩니다.
버퍼의 size는 충분히 크게 한 것입니다;;;

그리구~

계속 쌓이는 배열에, 낮은 인덱스의 배열값들이 사라질 수 있나요?? ㅡㅡ;;
높은 인덱스의 배열은 살아있구요..말이 좀 이상하네요 ㅎㅎ

이것은 밑에 코드의 일부 중 발생하는 문제입니다.
주석은 제가 혼자 코드 짤 때 단것이라 반말입니다 ㅡㅡ; 양해 좀 ㅎㅎ
도와주십시오.고수님들.

int fd, nread;
char buffer[10000]; //크기를 꼭 고정 해야 하나? smaps크기만큼만 잡을 순 없나?

//토큰단위로 자르는 기능에서 필요한 변수.
char* sep_string = NULL;
char* token = "\n\t :";
char* sep_array[] = {NULL};

//파일 열고 읽기.
if( (fd = open("/proc/1/smaps", O_RDONLY)) < 0) //나는 /proc/pid/의 모든 smaps를 열어야 한다.
{
printf("file open fail!!!\n");
exit(1);
}

if( (nread = read(fd, buffer, 10000)) < 0)
{
printf("fail to call read\n");
exit(1);
} //중요한 것은 smaps의 내용을 읽다가 만다. 무슨 이유인지..

//smaps파일 내용을 토큰 단위로 쪼개는 기능. 함수로 구현하면 제대로 동작하지 않는다. 조금 힘들다.
sep_string = strtok(buffer, token);
int index = 0;
sep_array

    = sep_string;

    while(sep_string != NULL)
    {
    printf("array index : %3d\t%s\n", index, sep_array

      );
      sep_string = strtok(NULL, token);
      if(sep_string == NULL)
      break;
      index++;
      sep_array
        = sep_string;
        }

        바로 이 부분입니다!!!!!!!!!!!!!!!
        //printf("%s\n", sep_array[181]); //sep_array[0]~[180]은 참조 하여 출력하면 글씨가 깨져서 나온다. 메모리 문제인듯?

        empty2fill의 이미지

        ssize_t read(int fd, void *buf, size_t count);

        read 함수의 리턴 값은 fd에서 읽은 바이트 수 입니다.

        그 값은 count 보다 작을 수 있습니다.

        => nread 값을 확인해보세요.

        => buffer 내용이 '\0'으로 끝나지 않을 수 있습니다.

        => '\0'로 끝나지 않는 배열에 strtok을 사용하는 것은 문제가 있습니다.

        ——
        ———
        Life is a tragedy when seen in close-up, but a comedy in long-shot. - Chaplin, Charlie -

        댓글 달기

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