C语言 使用 read() 函数从文件中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19769542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:52:45  来源:igfitidea点击:

Reading from file using read() function

cfile

提问by Wit

I have to write a program that reads numbers in separate lines each. Is it possible to read only one line of the file and then read an intfrom the buffer, and so on until the end of file? It is really hard to find good examples of using read and write. My example is kind of working but I'd like to read until it detects '\n'char and then convert buffer into int.

我必须编写一个程序,每行读取数字。是否可以只读取文件的一行,然后int从缓冲区读取一个,依此类推直到文件结束?很难找到使用读写的好例子。我的例子有点工作,但我想阅读,直到它检测到'\n'字符,然后将缓冲区转换为 int。

#include <fcntl.h> 
#include <stdio.h> 
#include <string.h>
#include <unistd.h> 
#include <iostream>

int fd[2];

void readFile(int fd) {
    unsigned char buffer[10];
    int bytes_read;
    int k=0;
    do {
        bytes_read = read(fd, buffer, 10); 
        k++;
            for(int i=0; i<10; i++) {
            printf("%c", buffer[i]);
        }
    }
    while (bytes_read != 0); 
}

int tab[50];

int main(int argc, char *argv[]) {
    if(argv[1] == NULL || argv[2] == NULL)   {
            printf("Function requires two arguments!\nGood bye...\n");
            return -1;
    }
    else {
        if (access(argv[1], F_OK) == 0) {
            fd[0] = open(argv[1], O_RDONLY);
        }
        else { 
            fd[0] = open(argv[1], O_WRONLY|O_CREAT|O_SYNC, 0700);
            const int size = 50;
                for(int i=0; i<size; i++) {
                    char buf[10];   
                    sprintf(buf, "%d\n", i+1);
                    write(fd[0], buf, strlen(buf));
                }
                close(fd[0]);
            fd[0] = open(argv[1], O_RDONLY);
            if (access(argv[2], F_OK) == 0) 
                fd[1] = open(argv[2], O_WRONLY);
            else 
                fd[1] = open(argv[2], O_WRONLY|O_CREAT, 0700);
        }
    }

    if (access(argv[2], F_OK) == 0) 
        fd[1] = open(argv[2], O_WRONLY); 
    else 
        fd[1] = open(argv[2], O_WRONLY|O_CREAT, 0700);


    readFile(fd[0]);
    close(fd[0]);
    close(fd[1]);
}

Revised code:

修改后的代码:

void readFile(int fd) {
    char buffer[10];
    int bytes_read;
    int k = 0;
    do {
        char t = 0;
        bytes_read = read(fd, &t, 1); 
        buffer[k++] = t;    
        printf("%c", t);
        if(t == '\n' && t == '
char c;
read(fd,&c,1);
') { printf("%d", atoi(buffer)); for(int i=0; i<10; i++) buffer[i]='
int readline(FILE *f, char *buffer, size_t len)
{
   char c; 
   int i;

   memset(buffer, 0, len);

   for (i = 0; i < len; i++)
   {   
      int c = fgetc(f); 

      if (!feof(f)) 
      {   
         if (c == '\r')
            buffer[i] = 0;
         else if (c == '\n')
         {   
            buffer[i] = 0;

            return i+1;
         }   
         else
            buffer[i] = c; 
      }   
      else
      {   
         //fprintf(stderr, "read_line(): recv returned %d\n", c);
         return -1; 
      }   
   }   

   return -1; 
}
'; k = 0; } } while (bytes_read != 0); }

回答by Gangadhar

Read Byte by Byte and check that each byte against '\n'if it is not, then store it into buffer
if it is '\n'add '\0'to buffer and then use atoi()

读逐字节,并检查每个字节反对'\n',如果不是的话,那么它存储到buffer
如果是'\n'添加'\0'到缓冲区,然后使用atoi()

You can read a single byte like this

您可以像这样读取单个字节

##代码##

See read()

read()

回答by Gaurav

fgets would work for you. here is very good documentation on this :-
http://www.cplusplus.com/reference/cstdio/fgets/

fgets 会为你工作。这是非常好的文档:-
http://www.cplusplus.com/reference/cstdio/fgets/

If you don't want to use fgets, following method will work for you :-

如果您不想使用 fgets,以下方法对您有用:-

##代码##