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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:44:01+00:00 2026-05-26T06:44:01+00:00

I am new to C programming, so I am having difficulties with the problem

  • 0

I am new to C programming, so I am having difficulties with the problem below.

I have a text file inp.txt which contains information like the following:

400;499;FIRST;
500;599;SECOND;
670;679;THIRD;

I need to type a number and my program needs to compare it with numbers from the inp.txt file.

For example, if I type 450, it’s between 400 and 499, so I need write to the word FIRST to the file out.txt

I have no idea how to convert a character array to an int.

  • 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-26T06:44:02+00:00Added an answer on May 26, 2026 at 6:44 am

    I think you’ll want these general steps in your program (but I’ll leave it to you to figure out how you want to do it exactly)

    1. Load each of the ranges and the text “FIRST”, “SECOND”, etc. from the file inp.txt, into an array, or several arrays, or similar. As I said in the comment above, fscanf might be handy. This page describes how to use it – the page is about C++, but using it in C should be the same http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/. Roughly speaking, the idea is that you give fscanf a format specifier for what you want to extract from a line in a file, and it puts the bits it finds into the variables you specify)
    2. Prompt the user to enter a number.
    3. Look through the array(s) to work out which range the number fits into, and therefore which text to output

    Edit: I’ll put some more detail in, as asker requested. This is still a kind of skeleton to give you some ideas.
    Use the fopen function, something like this (declare a pointer FILE* input_file):

    input_file = fopen("c:\\test\\inp.txt", "r") /* "r" opens inp.txt for reading */
    

    Then, it’s good to check that the file was successfully opened, by checking if input_file == NULL.

    Then use fscanf to read details from one line of the file. Loop through the lines of the file until you’ve read the whole thing. You give fscanf pointers to the variables you want it to put the information from each line of the file into. (It’s a bit like a printf formatting specifier in reverse).

    So, you could declare int range_start, range_end, and char range_name[20]. (To make things simple, let’s assume that all the words are at most 20 characters long. This might not be a good plan in the long-run though).

    while (!feof(input_file)) { /* check for end-of-file */
    
       if(fscanf(input_file, "%d;%d;%s", &range_start, &range_end, range_name) != 3) {
          break; /* Something weird happened on this line, so let's give up */
       else {
          printf("I got the following numbers: %d, %d, %s\n", range_start, range_end, range_name);
       }
    }
    

    Hopefully that gives you a few ideas. I’ve tried running this code and it did seem to work. However, worth saying that fscanf has some drawbacks (see e.g. http://mrx.net/c/readfunctions.html), so another approach is to use fgets to get each line (the advantage of fgets is that you get to specify a maximum number of characters to read, so there’s no danger of overrunning a string buffer length) and then sscanf to read from the string into your integer variables. I haven’t tried this way though.

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

Sidebar

Related Questions

I am new to programming and having some problem figuring out nested loops. I
I'm new to programming and having a problem with the following code: private string
I am new to Network Programming, and I am having a problem with some
I'm fairly new to C programming and having some difficulties wrapping my brain around
Ok, so I'm REALLY new to programming and javascript, but I'm having a problem
I am new to web programming and am having a problem calling a JavaScript
I am new to programming and am having difficulty with understanding object lifetime /
I'm relatively new to Python and am having problems programming with Scapy, the Python
I'm fairly new at programming, but I've wondered how shell text editors such as
Intro I am new to GTK+ programming and am having difficulty understanding why the

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.