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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:53:31+00:00 2026-05-26T23:53:31+00:00

I have an assignment where I need to read a certain number of Foo

  • 0

I have an assignment where I need to read a certain number of Foos from a file. In case the data inside the file is ill-formed for one of the Foos, what are some common ways to signal (to stderr, for example) exactly which of the Foos should be corrected?

My code looks something like this:

Foo foos[50];
FILE *file = fopen(...);
int i;

for (i = 0; i < sizeof(foos); ++i)
{
    if (readFoo(&foos[i], file) != 1)
        break;
} 

The readFoo function will return 1 if read is successful; if not, it will return a negative number and print an error to stderr which specifies what’s wrong but doesn’t say where the error is. How should I implement this (cleanely) so that I also tell the user the value of i?

In C++ I would do this with exceptions. In C I thought of doing something like this (readFoo would set error accordingly whenever something doesn’t go right):

/* These go in a source file */
const char *error = NULL;
const char error1[] = "Error 1 occurred.";
const char error2[] = "Error 2 occurred."; 

const char *getLastError() {
    return error;
}

What other ways are there to solve this?

  • 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-26T23:53:31+00:00Added an answer on May 26, 2026 at 11:53 pm

    The “clean” way to do this is to do the error reporting at the call site. That is, do something like:

    if( readFoo( foos + i, file, errbuf, errbuf_size ) != 1 ) {
        fprintf( stderr, "%d: %s\n", i, errbuf );
        ...    
    }
    

    That is, have readFoo populate a buffer with the error message rather than writing it to stderr.

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

Sidebar

Related Questions

I have a homework assignment where I need to take input from a file
Hello I have a homework assignment where I need to read two matrix .txt
Hello is have a question for a school assignment i need to : Read
I need to write a function that can read a file, and add all
I have an assignment in which I need to create a function that tells
I need to save some data in a file and when loading the page
I have a large text file I need to sort in Java. The format
As the title says, I need to write a small program to read data
I need to retrieve the PIN from a notepad file (below) and check it
Is it possible to have assignment in a condition? For ex. if (a=some_func()): #

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.