C 使用read()系统调用

C 使用read()系统调用,c,system-calls,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)); in

对于课堂作业,我们的任务是使用
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++;
    }
}
int readNumbers(int hexI,int MAX\u FILENAME\u LEN,int**array,char*fname){
int numberRead=0,cap=2;
*数组=(int*)malloc(cap*sizeof(int));
int n;
int filedesc=open(fname,ordonly,0);
if(filedesc<0){
printf(“%s:%s\n”,“无法打开”,fname);
返回-1;
}
char*buff=malloc(512);
buff[511]='\0';
而(n=read(filedesc,buff+totaln,512-totaln)>0)//只循环一次
总n+=n;
int len=strlen(浅黄色);
对于(inta=0;a=上限){
cap=cap*2;
*数组=(int*)realloc(*数组,cap*sizeof(int));
}
}
int k=0;
而((int*)&buff[k]){//尝试将buff的内容分配给数组
数组[k]=(int*)和buff[k];
k++;
}
}
您使用的
read()
是错误的。至少存在两个严重错误:

  • 您忽略返回值,除了测试文件结尾
  • 您似乎假设
    read()
    将在读取的数据之后附加一个nul字节。甚至可能它会用nul字节填充缓冲区
  • 如果要在
    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);        
    }
    
    /*
    *从文件描述符fd中读取尽可能多的字节,最大为buf_大小的字节
    *进入缓冲区buf。返回读取的字节数,或返回上的错误代码
    *失败。
    */
    整数读取完整(整数fd,字符大小[],整数大小){
    int total_read=0;
    int n_-read;
    而((n_read=read(fd,buf+total_read,buf_size-total_read))>0){
    总读取次数+=n次读取;
    }
    返回((n\u读取<0)?n\u读取:总读取);
    }
    
    在沿着这些线路做了一些事情并且没有收到错误后,您可以确信
    read()
    没有修改超出
    buf[total\u read-1]
    的缓冲区的任何元素。它肯定没有用零填充缓冲区的其余部分

    请注意,在缓冲区已满之前并不总是需要或希望读取;示例函数这样做是为了演示,因为它似乎是您想要的

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

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

  • 您忽略返回值,除了测试文件结尾
  • 您似乎假设
    read()
    将在读取的数据之后附加一个nul字节。甚至可能它会用nul字节填充缓冲区
  • 如果要在
    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);        
    }
    
    /*
    *从文件描述符fd中读取尽可能多的字节,最大为buf_大小的字节
    *进入缓冲区buf。返回读取的字节数,或返回上的错误代码
    *失败。
    */
    整数读取完整(整数fd,字符大小[],整数大小){
    int total_read=0;
    int n_-read;
    而((n_read=read(fd,buf+total_read,buf_size-total_read))>0){
    总读取次数+=n次读取;
    }
    返回((n\u读取<0)?n\u读取:总读取);
    }
    
    在沿着这些线路做了一些事情并且没有收到错误后,您可以确信
    read()
    没有修改超出
    buf[total\u read-1]
    的缓冲区的任何元素。它肯定没有用零填充缓冲区的其余部分

    请注意,在缓冲区已满之前并不总是需要或希望读取;示例函数这样做是为了演示,因为它似乎是您想要的


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

    为什么要将读取的数据写入char*buff[512]而不是单个char*buff=malloc(MAX\u FILENAME\u LEN)?buff保存的是文件的内容,而不是文件名。但是,我确实将char*buff[512]更改为char*buff=malloc(512)。512是教授推荐的。为什么要将读取的数据写入char*buff[512]而不是单个char*buff=malloc(MAX\u FILENAME\u LEN)?buff保存的是文件的内容,而不是文件名。但是,我确实将char*buff[512]更改为char*buff=malloc(512)。512是由教授推荐的。谢谢,我有点理解你在做什么,但我仍然缺少核心概念。我真的不知道如何存储读取的字节