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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:48:25+00:00 2026-06-01T02:48:25+00:00

I have the following code to open a input and output file: if ((source_file_ptr

  • 0

I have the following code to open a input and output file:

if ((source_file_ptr = fopen(source_filename, "rb")) == NULL) {
  error("unable to open input file");
}
if ((output_file_ptr = fopen(output_filename, "wb")) == NULL) {
  error("unable to open output file");
}

This is goood simple way to catch errors in opening the files.

However after some editing overall to my program, the program now crashes instead of catching an invalid output file.

I tried a several things without success but interestingly when I try this:

if ((output_file_ptr = fopen(output_filename, "wb")) == NULL) printf("fail");
exit(0);
if ((source_file_ptr = fopen(source_filename, "rb")) == NULL) {
  error("unable to open input file");
}
if ((output_file_ptr = fopen(output_filename, "wb")) == NULL) {
  error("unable to open output file");
}

It will print “fail” (and obviously exit without much more insight).

But if I comment out the exit(0) line, it will again exhibit the same crash behaviour without printing “fail” nor catching the error.

I can’t explain why this is… I suspected a dangling pointer but the only preceding lines of code within the function are already enclosed in if-else if-else all with braces. There are only a few other functions delcared above but I’ve checked and ensured they’re all enclosed in braces.

I’m still learning C, any thoughts on what’s going on here?
Thanks as a lot!

Note: enclosing printf(“fail”) with braces doesn’t alter the behaviour I observe.

EDIT: additional code that follows the above:

if (fread(&file_struct, sizeof(file_struct), 1, source_file_ptr) < 1) {
   error("unable to read %s", source_filename);
}
else {
   error_check(file_struct);

  if (fwrite(&file_struct, sizeof(file_struct), 1, output_file_ptr) < 1) {
     error("unable to write file header");
  }
}
  • 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-01T02:48:27+00:00Added an answer on June 1, 2026 at 2:48 am

    The first thing I’d check is that source_filename was a valid C string. I’d then check output_filename in the same way.

    If it turns out your changes have somehow corrupted either or both of them, you’re into undefined territory behaviour.

    The use of a debugger would be ideal here since you could set a breakpoint at the start of that code then single-step through, checking variables that you’re about to use.

    The other thing you should check is whether your calls to error actually return (rather than, for example, call exit).

    If they do then, even though the fopen calls returned NULL, you’re still going to pass that to fread and/or fwrite, a definite no-no.

    Since your second case prints "fail", it’s a given that you have some problem opening the output file. Using the NULL handle from that would not be a good thing.

    If that function does return, then you should be using something like:

    if ((source_file_ptr = fopen(source_filename, "rb")) == NULL) {
      error("unable to open input file");
      return;
    }
    if ((output_file_ptr = fopen(output_filename, "wb")) == NULL) {
      fclose (source_file_ptr);
      error("unable to open output file");
      return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: using (BinaryReader br = new BinaryReader( File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
I have the following code: ifstream initFile; initFile.open(D:\\InitTLM.csv); if(initFile.is_open()) { // Process file }
Suppose I have some Python code like the following: input = open(input.txt) x =
I have the following code to open google maps: NSString *urlString = [NSString stringWithFormat:@http://maps.google.com/maps?q=%@,
I have the following code: f = open(path, 'r') html = f.read() # no
I have the following code; // open the modal when an element with a
I have a unicode filename that I would like to open. The following code:
I have the following code: /** Open a browser on the URL specified in
I have the following code to hide an essay question or open text. I'd
I have the following C++ code. #include <iostream> using namespace std; int testfunction(int input)

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.