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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:35:35+00:00 2026-05-30T00:35:35+00:00

I am having some trouble with file descriptors. The standard fd table on a

  • 0

I am having some trouble with file descriptors. The standard fd table on a POSIX-compliant system should, for each process, have stdin as 0, stdout as 1, and stderr as 2. Thereafter, file descriptors should be issued sequentially, so a request to open() a new file should return the file descriptor 3.

This does not happen on my system. Code which was working a few hours ago has ceased to do so. Because it is very long and messy code, I have written a quick example of the sort of thing I am seeing.

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

void main(int argc, char *argv[])

{
    int fToWrite;
    char *outFileName = "chuckles.txt";

    if (fToWrite = open(outFileName,O_WRONLY|O_CREAT|O_TRUNC) < 0) {
            fprintf(stderr,"error during open!: %s\n",strerror(errno));
            exit(EXIT_FAILURE);
    }

    write(fToWrite,"kekeke",6);
    fprintf(stderr,"fToWrite = %i\n",fToWrite);
    close(fToWrite);
}

On my machine, after running this code, chuckles.txt is created, but nothing is written to it. To the console is printed:

kekekefToWrite = 0

Note that kekeke was not written to chuckles.txt, and that fToWrite, the file descriptor of the file I open()ed, is 0. But 0 is reserved for stdin. Likewise, if I try to fprintf to STDOUT_FILENO (which is the int 1) or STDERR_FILENO (which is 2), I get a segfault. In the main program I am trying to write, the program segfaults when I try to write() to STDOUT_FILENO. A few hours ago, the same code snippets would happily put letters on the console; I don’t know what changed. I’ve rebooted, but I don’t know what else to do about it.

Strangely, printf still works. I was under the impression that printf just wrote to whatever was in the fd table’s #2 entry.

  • 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-30T00:35:37+00:00Added an answer on May 30, 2026 at 12:35 am

    You need an extra pair of parentheses:

    if ((fToWrite = open(outFileName,O_WRONLY|O_CREAT|O_TRUNC)) < 0) {
    

    That’s why fToWrite gets the value 0.

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

Sidebar

Related Questions

I'm having some trouble with an xml file, I have found all sorts of
I'm having some trouble setting up a htaccess file. Currently I have 15 domains
I'm having some trouble with C language.. I have one file txt with various
I'm having some trouble reading data from a file into a vector of Orders.
I am having some trouble trying to print from a file. Any ideas? Thanks
I'm having some trouble with replacing a portion of a file in binary mode.
I'm having some trouble with some struct typedef declarations in a header file not
Having some trouble with what should be a very simple scenario. For example purposes,
I'm having some trouble opening an existing blackberry eclipse project I have to work
I'm having some trouble sending a file by ftp from my BizTalk 2006RC. The

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.