Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unless your file is huge, if you're using read(), it would be easier to read in the entire file, then operate on the memory buffer, rather than in discrete chunks. That is, unless each line is of a fixed length.</p> <p>I'd do something like this:</p> <pre><code>int rc; int fd = open("data", O_RDONLY); // open the file for reading if (fd == -1) { // error } // to be thorough, do a stat() here to find how big to make the buffer struct stat sb; rc = fstat(fd, &amp;sb); if (rc == -1) { // error } char *buffer = calloc(1, sb.st_size); int bytes_read = 0; // read in entire file; each read() can be incomplete, hence why it's in a loop, // and reading from/writing to increasing sections of the memory and file while ((rc = read(fd, (buffer + bytes_read), (sb.st_size - bytes_read))) &gt; 0) { if (rc == -1) { // error } bytes_read += rc; } close(fd); // Now, to read it line-by-line... char line[128]; // 128 is arbitrary int pos = 0; while ((rc = sscanf(buffer + pos, "%127[^\n]\n", line)) &gt; 0) { pos += strlen(line) + 1; // do stuff with line } return 0; </code></pre> <p>Then you can operate on your memory buffer line-by-line by scanning for newlines, or using sscanf(). Also make sure to free() your buffer!</p> <p><strong>Edit:</strong> I've added some example code for using sscanf() to handle your buffer. If you know the format of the lines (you say you're parsing them) you might be able to make better use of sscanf() by using the format specifiers. All of this is untested, by the way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload