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

  • Home
  • SEARCH
  • 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 8774061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:27:55+00:00 2026-06-13T18:27:55+00:00

Using fscanf I want to process text that contains two floats, an arbitrary amount

  • 0

Using fscanf I want to process text that contains two floats, an arbitrary amount of whitespace before, between, or after the floats (including newlines/returns), and a newline character at the very end. If there are more/fewer than two numbers, then I want to detect that and report an error.

This seems to work for handling the whitespace, but it won’t detect if there’s more than two floats present:

fscanf(f, "%f %f", &sx, &sy);

And this seems to work just as well:

fscanf(f, "%f %f%*[ \n\t\r\f\v]\n", &sx, &sy);

Is there a better way to handle 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-06-13T18:27:57+00:00Added an answer on June 13, 2026 at 6:27 pm

    I don’t know how to do it with a single *scanf, I don’t think it’s possible due to the last part of your request, *scanf can’t read an arbitrary length character sequence matching a simple regex and ending with a specified character.

    int character;
    bool trailingNewLine;
    
    if (fscanf(f, "%f%f", &sx, &sy) < 2)
        // not matching "whitespace-float-whitespace-float"
        // being more accurate could be painful...
    
    // read arbitrary quantity of white space ending with '\n'
    while (isspace(character = getc(f)))
        trailingNewLine = (character == '\n');
    
    // the last one wasn't white space, doesn't belong to this one
    ungetc(character, f);
    
    if (!trailingNewLine)
        // missing newline at the end
    
    // OK!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using fscanf in C++ as follows: Here is my text file: abcd
I am attempting to parse a text (CSS) file using fscanf and pull out
I've already got some code to read a text file using fscanf() , and
I am trying to read a text file using fscanf.I am working in eclipse
I want to parse an ip from file using fscanf (C code using gcc).
I am using fscanf to read a file which has lines like Number <-whitespace->
I'm using C and I want to read from a binaryFile. I know that
I am parsing a text (css) file using fscanf. The basic goal is simple;
I have difficulty using malloc and fscanf . I just want to read a
I am trying to read in a text file using fscanf. The input 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.