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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:01:23+00:00 2026-06-07T18:01:23+00:00

Brand new to C here. The program is supposed to read in a file

  • 0

Brand new to C here. The program is supposed to read in a file of fragments on a single line, separated by a “#” before and after them. For example #fragment1##fragment2##fragment3#

The two errors I want to check for are that a fragment is not over 1000 characters, and that the file follows the correct format of “#” on either side of fragments. I don’t really get how fscanf syntax works but I think the following would check for the errors:

  char buffer[MAX_FRAG_LEN+1];
  if (fscanf(fp, "#%1000[^#]#", buffer) == 1) {
    return strdup(buffer);
  } else {
    fprintf(stderr, "Error! Incorrect format.\n");
  }

However, I want to separate the errors so I can specifically deliver a message of which of the two it was. How can I make the checks individually? Much appreciated!

  • 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-07T18:01:25+00:00Added an answer on June 7, 2026 at 6:01 pm

    To make your technique work, you should manually check if the next character after the buffer is a '#' after fscanf returns. This allows you to distinguish a too long string error from a missing "##" error.

      char buffer[MAX_FRAG_LEN+1];
      if (fscanf(fp, "#%1000[^#]", buffer) == 1) {
        int c = fgetc(fp);
        if (c == '#') return strdup(buffer);
        if (c != EOF) {
          ungetc(c, fp);
          fprintf(stderr, "Error! More than 1000 characters.\n");
        } else {
          fprintf(stderr, "Error! Last fragment did not end with '#'.\n");
        }
      } else {
        fprintf(stderr, "Error! Fragment did not start with '#'.\n");
      }
    

    However, using fscanf for parsing is generally tricky. If the input is in a totally unexpected format, it can be difficult to recover from them correctly. So, it is usually easier to handle errors by reading in the entire line in a single buffer, and then parse that line instead, as suggested by Duck.

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

Sidebar

Related Questions

Brand new poster here on StackOverflow. I've been reading and learning here for some
It's a brand new project. Here's the exact commands I've run: rails new MyProject
I'm basically brand new to LINQ. I've looked around a lot on here and
OK. So, here's some details on my situation : I've got a brand-new iPhone
I'm brand spankin' new to Javascript. Here's what I want to do. I want
I have just downloaded jQuery 1.7 for a brand new project I'm starting. After
I'm brand new to ruby and Watir, here's my issue... I have a MySQL
Here is the scenario: I have a brand new database, with the tables with
Here's the log from the build. This is a brand new project in Xcode
I am brand new to Ruby, and have just now touched upon associations. Here

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.