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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:53:05+00:00 2026-06-08T10:53:05+00:00

Basically what i’m trying to do is read a number from a file, increment

  • 0

Basically what i’m trying to do is read a number from a file, increment the value by one, and then write the number back to the same file. Using fork() is supposed to have both processes accessing the file but using locks so they take turns. I keep getting a segmentation fault.

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

void appendValue(FILE *, int *);
int readValue(FILE *, int *);
void lockFile(FILE *);
void unlockFile(FILE *);
void whatProcess(pid_t *pID);

int main(void) {
    pid_t pID;
    pID = fork();
    int value = 0, counter = 0;
    int *valPtr = &value;
    pid_t *pidPtr = &pID;
    FILE *file = fopen("output.txt", "a+");
    lockFile(file);

    while(counter < 1000) {
        whatProcess(pidPtr);
        value = readValue(file, valPtr);
        value++;
        appendValue(file, valPtr);
        rewind(file);
        counter++;
    }
    unlockFile(file);       
    fclose(file);
    printf("\n");
    return 0;
}

void whatProcess(pid_t *pID) {
    if(*pID > 0) {
        printf("\n --- In Parent ---");
    } else if(*pID == 0) {
        printf("\n --- In Child ---");
    } else {
            printf("\n --- fork() Failed ---");
    }
}

void lockFile(FILE *file) {
    int lock;
    lock = lockf(fileno(file), F_LOCK, 0);
        while(lock != 0) {}
        if(lock == 0) {
            printf("\nPID %d: Lock Successful", getpid());
        } else {
            printf("\nPID %d: Lock Unsuccessful", getpid());
        }   
}

void unlockFile(FILE *file) {
    int lock;
    lock = lockf(fileno(file), F_LOCK, 0);
        while(lock != 0) {}
        if(lock == 0) {
            printf("\nPID %d: Unlock Successful", getpid());
        } else {
            printf("\nPID %d: Unlock Unsuccessful", getpid());
        }   
}

void appendValue(FILE *file, int *value) {
    fprintf(file, "%d\n", *value);
}

int readValue(FILE *file, int *value) {
    while(!feof(file)) {
        fscanf(file, "%d", value);
    }
    return *value;
}
  • 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-08T10:53:06+00:00Added an answer on June 8, 2026 at 10:53 am

    The fscanf in readValue is writing to a non-allocated location. You are passing value in as a pointer so there is no need to use the address of operator.

    int readValue(FILE *file, int *value) {
        fscanf(file, "%d", value);
        printf("\nreadValue(): %d", *value);
        return *value;
    }
    

    Or, even better:

    int readValue(FILE *file, int *value) {
        if (fscanf(file, "%d", value) == 1) {
            printf("\nreadValue(): %d", *value);
            return 0;
        }
        return -1;
    }
    

    Your current function does not indicate whether it has succeeded or failed. Either add a status return value (zero, -1 are pretty common) or omit error checking and do this instead:

    int readValue(FILE *file) {
        int buf;
        if (fscanf(file, "%d", &buf) == 1) {
            printf("\nreadValue(): %d", buf);
        } else {
            perror("readValue(): fscanf failed");
        }
        return buf; /* could be a garbage value, use at own risk */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I am writing a simple bit of code to increment every one second
Basically i'd like to allow an arbitrary (but not empty ) number of key-value
Basically I am trying to restart a service from a php web page. Here
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
Basically I need to get older version of a file in the repository without
Basically from C++ FAQ I learned that: A virtual function allows derived classes to
Basically, I have a list of delivery checkboxes one for deliver to this address
Basically, what I'm trying to create is a page of div tags, each has
Basically from a database I am getting data that is formatted like this nameofproject101
Basically there is a file called 8puzzle.py and I want to import the file

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.