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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:16:59+00:00 2026-06-05T02:16:59+00:00

I’ve got homework, to take phone numbers from a text file, and put it

  • 0

I’ve got homework, to take phone numbers from a text file, and put it into an array…
here is what I wrote, though it puts nothing on it…
the loop is that as long as theres text in the .txt files, read it into the array…

void get_phones(int *phones)
{
    FILE *fp;
    fp = fopen("phones.txt", "rt");
    if (fp == NULL)
    {
        printf ("Error\n");
    }
    else
    {
        while (fscanf(fp, "%d\n", &phones) > 0)
        {
            fscanf(fp, "%d\n", &phones);
        }
    } 
}
  • 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-05T02:16:59+00:00Added an answer on June 5, 2026 at 2:16 am

    Lastest update:

    For testing:
    please put the following code in your main(),and change the number 10 to the # of lines you have in the file. To compute the upper bound automatically you can try to change the return type of get_phones to int and put a counter in the while-loop.

    int i=0;
    for(i=0;i<10;i++){
       printf("%lld\n",*(phones+i));
    }
    

    You need deference a pointer in order to print the actual value. Otherwise you are printing the address stored in the pointer. And phones, besides being the name of an array, it is a pointer pointing to the first element in the array in its nature. For more info on pointer and array, see this tutorial.

    Assume that you are using a 32-bit machine
    The phone numbers I put in my textfile is the following:
    22121222345
    678139199
    111111111

    Note that 22,121,222,345>=2,147,483,647, which is the maximum number an int can represent. (For unsigned int it is 2^32-1). Now if we try to run the following code:

    void get_phones(int *phones){
        FILE *fp;
        fp = fopen("phones.txt", "rt");
    
        if (fp == NULL)
            printf ("Error\n");
        else
            while (fscanf(fp, "%d\n", phones++) !=EOF){}
    
    }
    

    The list of number printed on terminal are:
    646385865
    678139199
    111111111

    The first number saved in the array is totally random! Why? because it is too large for an integer and it overflows.

    Now if we try the following version:

    void get_phones(long long* phones){
        FILE *fp;
        fp = fopen("phones.txt", "rt");
            if (fp == NULL)
                printf ("Error\n");
            else
                while (fscanf(fp, "%lld\n", phones++) !=EOF){}
    
    }
    

    The list of number printed on terminal are:
    22121222345
    678139199
    111111111

    Why does it work? Because the type long long can store number up to 2^63-1 = 9,223,372,036,854,775,807

    Tested working version. Change the data type to long long if you want.

    !! The problem with your code is that you are passing the wrong thing to the fscanf. phones is already a pointer — it is pointed to the starting point of the array. when you write phone++, it passes the current pointer to fscanf, and increase the pointer by one, which moves the pointer to the next slot in the integer array.

    Also, although fscanf will return the number of items it scanned, in your while loop condition, the scanning is already performed. So you don’t need to call it second time in the body of the while loop

    5195551234 this number is still too large for unsigned long, unfortunately. Because for 32-bit machine, both int and unsigned long has the max value $2^{32}-1$. Check out wikipedia if you need more information on this. Instead you need long long if you are running your code on 32-bit machine, otherwise it will overflow and store incorrect data.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
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

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.