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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:17:29+00:00 2026-06-08T00:17:29+00:00

I’d like your help with understanding how should I do the following: I have

  • 0

I’d like your help with understanding how should I do the following:

I have a file that contains integers separated by spaces ‘ ‘. I need to read all integers, sort them and write them as a strings to another file. I wrote a code, but I read char by char, put the word in an char sub_arr [Max_Int] and when I met ‘ ‘, I put these chars, now one string, after atoi-ing it into another Main int array,until reaching the end of the file, string by string, and then I sorted and itoa-ing them and wrote them in another file.

But then I remembered that there’s a fscanf function:I read about it and still I didn’t understand completely what does it do and how to use it.

In my case, where all integers separated by space, can I write fscanf(myFile,"%s",word)? would it know not to consider ‘ ‘ and stop at the end of the specific string?! How?

More than that, Can I write fscanf(myFile,"%d",number) and it would give me the next number itself? (I must have misunderstood it. Feels like magic).

  • 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-08T00:17:30+00:00Added an answer on June 8, 2026 at 12:17 am

    You are right, fscanf can give you the next integer. However, you need to provide it with a pointer. Therefore, you need an & behind number:

    fscanf(myFile, "%d", &number);
    

    *scanf family of functions also automatically skip whitespace (except when given %c, %[ or %n).

    Your loop with reading file will eventually look like this:

    while (you_have_space_in_your_array_or_whatever)
    {
        int number;
        if (fscanf(myFile, "%d", &number) != 1)
            break;        // file finished or there was an error
        add_to_your_array(number);
    }
    

    Side note: you may think of writing like this:

    while (!feof(myFile))
    {
        int number;
        fscanf(myFile, "%d", &number);
        add_to_your_array(number);
    }
    

    This, although looks nice, has a problem. If you are indeed reaching the end of file, you will have read a garbage number and added to your data before testing the end of file. That is why you should use the while loop I mentioned first.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT

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.