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 7631477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:21:49+00:00 2026-05-31T06:21:49+00:00

What I only want written to the file descriptor is the input string. However,

  • 0

What I only want written to the file descriptor is the input string. However, it also writes the error messages passed in fprintf. Can someone explain why it is behaving like this? This small program shows the behavior.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
    int fd;
    mode_t mode = S_IRUSR | S_IWUSR;
    char *filename = "file.txt";
    char *input = "hello world";
    char output[20];

    // create and close
    if((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, mode)) == -1)
    {
        fprintf(stderr, "Error.\n");
        return -1;
    }
    close(fd);

    // just open
    if((fd = open(filename, O_RDWR, mode)) == -1)
    {
        fprintf(stderr, "Error.\n");
        return -1;
    }

    if(write(fd, input, 200) == -1)
    {
        fprintf(stderr, "Error.\n");
        return -1;
    }

    // move back to beginning
    if(lseek(fd, 0, SEEK_SET) == -1)
    { 
        return -1;
    }

    if(read(fd, &output, 200) == -1)
    {
        fprintf(stderr, "Error.\n");
        return -1;
    }

    printf("%s\n", output);

    return 0;
}
  • 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-31T06:21:50+00:00Added an answer on May 31, 2026 at 6:21 am
    write(fd, input, 200)
    
    //...
    
    read(fd, &output, 200)
    

    You’re writing and reading well past the arrays that contain valid data – neither input nor output are anywhere close to 200 bytes. So who knows what’s going to end up in the file (and I’m not sure why the read() doesn’t crash your program)?

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

Sidebar

Related Questions

I'm using Linq to write XML to a file and I want only sections
When one does process > file Standard output gets written to a file only
I've written a daemon that writes the word Beat to a file, followed up
How can I read any type of file from a location and access only
I only want a list of files that have been added (not ones that
I only want log4net to keep let's say 10 days-worth of log files as
I only want this function to run if .toolbar li does not have the
I only want a simple Splash Screen Example. Get the Code, Insert my picture,
We only want one instance of our app running at any one time. So
I only want a method to activate if the pixel that is clicked is

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.