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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:09:09+00:00 2026-06-18T15:09:09+00:00

I am trying to spawn several threads and for each thread I write to

  • 0

I am trying to spawn several threads and for each thread I write to a different file (thread 1 writes to file 1, etc…). However, after the threads execute ferror() is set, preventing me from doing further file operations in the main process. I tried clearing the error, but it did not solve. This is the code I currently have:

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

void * bla (void *arg) {
    fprintf((FILE *) arg, "Hey, printing to file");
}

int main() {
    FILE *f1 = fopen("out0", "rw");
    FILE *f2 = fopen("out1", "rw");

    pthread_t t[2];
    pthread_create(&t[0], NULL, bla, f1);
    pthread_create(&t[1], NULL, bla, f2);

    pthread_join(t[0], NULL);
    pthread_join(t[1], NULL);
    printf("%d\n", ferror(f2));   // ERROR: ferror() is set to 1 here!

    //fseek(f1, 0, SEEK_END);
    fseek(f2, 0, SEEK_END);
    long pos = ftell(f2);         // This still works
    printf("%ld\n", pos);
    clearerr(f2);                 // Trying to clear the error, flag clears, but further operations fail
    char *bytes = malloc(pos);
    int err = fread(bytes, 1, 4, f2);  // fread returns 0
    printf("%d\n", ferror(f2));
    printf("%d\n", err);
    bytes[pos-1] = '\0';
    printf("%s", bytes);
    free (bytes);

    fclose(f1);
    fclose(f2);

    return 0;

Note that the file opened by the threads should not exist, and if it exists it should be cleared.
Any help would be greatly appreciated. Thanks!

  • 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-06-18T15:09:11+00:00Added an answer on June 18, 2026 at 3:09 pm

    mode argument for fopen should be "r+" (if the files should exist) or "w+" (or maybe even "a+") instead of "rw". The string "rw", which is not a valid mode, is probably interpreted as "r" mode, and you can’t fprintf to such a FILE*.

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

Sidebar

Related Questions

I am trying to spawn a process using Runtime.exec. I want to use my
I'm trying to make each span rotate between a random color on 3sec intervals.
So I have several DIVs called achievement, and each one contains a span called
I am trying to simulate a a network consisting of several clients and servers.
I have read several posts across stackoverflow trying to determine the best route to
I'm trying to get several inline and inline-block components aligned vertically in a div
I am trying to make a site which uses different Fusion Tables. There is
I am trying to manage several cisco switches in a network I control. I
I'm trying to add several tags to div[id='head'] at once in a BeautifulSoup with
Based on several other threads here I think what I'm looking for is json_encode(),

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.