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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:21:27+00:00 2026-06-18T10:21:27+00:00

I am doing a program that outputs a list of prime numbers with fstream.

  • 0

I am doing a program that outputs a list of prime numbers with fstream.

I have this so far:

int export_list (int lim = 50)
{
    int x;
    last_in_txt = ????????????; // assigns last number on txt

    ofstream file ("Primes.txt" , ios::app);

    if (file.is_open()) // if it opens correctly
    {
        for (x = last_in_txt ; x < lim ; x++)
        {
            if (check_prime (x)) // returns 1 when x is prime, returns 0 when not
            {
                file<< x << " ";
            }
        }
        cout << "Done!" << endl << pressenter;
        cin.get();
    }
    else
    {
        cout << "Unable to open file" << endl << pressenter;
        cin.get();
    }
    return(0);
}

So, as you can see, this should append a list of prime numbers to Primes.txt, starting with the prime 1234547.

Primes.txt looks like this:

2 3 5 7 11 13 17 19 23 29 31 37 (...) 1234543 1234547 

My question is how do I assign 1234547 (which is the last number of the txt) to the variable last_in_txt?

Other (not so important) question:
Should I save the numbers the way I’m currently doing, or should I store each number in a separate line?

  • 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-18T10:21:28+00:00Added an answer on June 18, 2026 at 10:21 am

    One simple way: keep reading and assign until the whole file is read.

    For example,

    int last_in_txt = 0;
    {
        ifstream infile("Prime.txt");
        int k;
        while(infile >> k) {
            last_in_txt = k;
        }
    }
    // Now last_in_txt is assigned properly, and Prime.txt is closed
    

    This works well no matter the numbers in Prime.txt are separated by space characters (' ') or by newline characters ('\n').

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

Sidebar

Related Questions

I am basically doing a tutorial but my program seems to have errors that
I have a program for the iPhone that is supposed to be doing intelligent
I have a problem for a university lab; Write a short program that outputs
I have a program that's creating a secure directory for user output. This is
/*This program will ask a user to enter a song list. By doing this
I'm sure that I'm doing something wrong, I have a Go program that parses
I'm doing a code testing for a program that calculate the results for a
I'm doing a theatrical performance, and I need a program that can read images
I'm working on a database program and I noticed that when I'm doing queries
I am doing a minesweeper program for school, but i keep getting this error

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.