Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6886741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:47:51+00:00 2026-05-27T05:47:51+00:00

I have a file as a resource. I can only use read() , write()

  • 0

I have a “file” as a resource. I can only use read(), write() and fstat() it. This file is a text file which I would like to parse.

Normally I use fgets() to read the text file line by line and parse it. How can I do this in this case?

FILE *fp;
char buffer[128];

fp = fopen( "/home/txtfile", "r" );
if (fp == NULL){
    perror("file missing");
}
while (fgets (buffer, sizeof(buffer), fp) != NULL) {
       //some code
}

How can I do the same with read() ?

Is this right?

int fd = open("/dev/file",O_RDONLY);
if (fd==-1) {
    printf("Failed to open file!!!\n");
}
while (fgets (buffer, sizeof (buffer), fd) != NULL) { 
    //some code 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:47:52+00:00Added an answer on May 27, 2026 at 5:47 am

    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.

    I’d do something like this:

    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, &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))) > 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)) > 0) {
        pos += strlen(line) + 1;
        // do stuff with line
    }
    
    return 0;
    

    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!

    Edit: 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added an image to my form's resource file, myForm.resx, and I would
I have a script that will convert a text file into a resource file,
If I have a Resource bundle property file: A.properties: thekey={0} This is a test
If I have a text stored in db, file, resource does not matter: <code><%=
Suppose I have a custom file format, which can be analogous to N tables.
In Delphi 7, an image editor program is included, which can read and write
I have a resource file named filetypes.resx. Some how I figured out to bind
I have a compiled .NET assembly with a specific resource file embedded (named 'Script.xml').
I have a .ico file that is embedded as a resource (build action set
I have an rc file that uses relative paths to locate the resource files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.