使用read()系统调用

时间:2014-11-03 21:22:33

标签: c system-calls

对于课堂作业,我们的任务是使用read()函数来读取包含数字的文件。虽然我能够将数字读入缓冲区,但我无法将它们从缓冲区移动到char *数组中,以便可以轻松访问和排序它们。任何建议都表示赞赏。

int readNumbers(int hexI, int MAX_FILENAME_LEN, int **array, char* fname) {
    int numberRead = 0, cap = 2;
    *array = (int *)malloc(cap*sizeof(int));
    int n;
    int filedesc = open(fname, O_RDONLY, 0);
    if(filedesc < 0){
        printf("%s: %s\n", "COULD NOT OPEN", fname);
        return -1;
    }
    char * buff = malloc(512);
    buff[511] = '\0';
   while(n = read(filedesc, buff+totaln, 512 - totaln) > 0) //Appears to loop only once
            totaln += n;
    int len = strlen(buff);
    for (int a = 0; a < len; a++) {  //Dynamically allocates array according to input size
        if ((&buff[a] != " ") && (&buff[a] != '\n'))
            numberRead++;
        if (numberRead >= cap){
            cap = cap*2;
            *array = (int*)realloc(*array, cap*sizeof(int));
        }
    }
    int k = 0;
    while((int *)&buff[k]){  //attempts to assign contents of buff to array
        array[k] = (int *)&buff[k];
        k++;
    }
}

1 个答案:

答案 0 :(得分:4)

您使用read()是错误的。至少有两个严重的错误:

  1. 除了测试文件结尾外,您忽略了返回值。
  2. 您似乎认为read()将在读取的数据后附加一个空字节。甚至可能会用nul字节填充缓冲区。
  3. 如果要在read()返回后将更多数据读入同一缓冲区,而不覆盖已读取的内容,则必须将指针传递给缓冲区中的第一个可用位置。如果您想知道总共读取了多少字节,那么您需要添加返回值。通常的范例是这样的:

    /*
     * Read as many bytes as possible, up to buf_size bytes, from file descriptor fd
     * into buffer buf.  Return the number of bytes read, or an error code on
     * failure.
     */
    int read_full(int fd, char buf[], int buf_size) {
        int total_read = 0;
        int n_read;
    
        while ((n_read = read(fd, buf + total_read, buf_size - total_read)) > 0) {
            total_read += n_read;
        }
    
        return ((n_read < 0) ? n_read : total_read);        
    }
    

    在完成这些行并且没有收到错误之后,您可以确信read()没有修改缓冲区的任何元素超出buf[total_read - 1]。它当然没有用零填充缓冲区的其余部分。

    请注意,在缓冲区已满之前,并不总是需要或不需要读取;示例函数用于演示目的,因为它似乎是您想要的。

    完成后,请注意您正在尝试提取数字,就好像它们是以二进制形式记录在文件中一样。情况可能确实如此,但如果您正在阅读包含格式化数字的文本文件,则需要以不同方式提取数字。如果这是您之后的内容,那么在读取完最后一个字节后添加一个字符串终止符,并使用sscanf()来提取数字。