Hello I have created a sucsessful prime number finder however I wish to retrive the start number from a file the last line shows how I tried to do this however it failed any solutions ?

/* By Kelechi Nze 
	My first prime project*/
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#define NEWLINE "\n"
using namespace std;
struct primes //My Prime number structure
	{
		fstream startnumb; //The starting number file
		ofstream passnumb; //Passnumb: where primes are stored
		int number, factor, divider;	/*Number: The actulal prime number "finder" 
											factor: What's a factor
											Divider: What to divide the prime.number with to find if there is a prime there*/
	}prime;	//Only one member in the structure ? Yes !
	prime.passnumb.open("UserStorage.txt");
	prime.startnumb.open("startnumber.ini")
        prime.number = prime.startnumb;

Dani AI

Generated

A few practical points to get this working reliably.

You cannot assign a stream to an int (the line prime.number = prime.startnumb; will not work), and there is a missing semicolon after prime.startnumb.open(...) in the snippet — both will prevent compilation. As hinted, read from the file and parse the text into an integer, and always check that the file actually opened. Keep file I/O separate from your data structure: store the number (or the filename) in the struct, but open streams only when you need to read/write.

If you want the last valid integer in the file (common when storing an evolving "start number"), a robust pattern is to read the file line by line, trim the line, try to parse it, and keep the most recent successful parse. The example below uses a portable C-style parse (no exceptions) and skips non-numeric lines:

#include <fstream>
#include <string>
#include <cstdlib> // strtol

bool readLastInt(const std::string& filename, int& out)
{
    std::ifstream f(filename.c_str());
    if (!f) return false;
    std::string line;
    bool found = false;
    while (std::getline(f, line)) {
        size_t a = line.find_first_not_of(" \t\r\n");
        if (a == std::string::npos) continue;
        size_t b = line.find_last_not_of(" \t\r\n");
        std::string tok = line.substr(a, b - a + 1);
        char* endptr = 0;
        long val = std::strtol(tok.c_str(), &endptr, 10);
        if (endptr != tok.c_str() && *endptr == '\0') { out = static_cast<int>(val); found = true; }
    }
    return found;
}

Extra tips: use std::ofstream with trunc to overwrite the file when saving a new start number; let stream destructors close files (or call close explicitly if you prefer); avoid keeping open streams as struct members; and remove unnecessary headers like <windows.h> unless you actually need them.

Recommended Answers

All 6 Replies

Change line 19 to: prime.startnumb >> prime.number;

It's maybe better when you change line 11 to: ifstream startnumb; You shouldn't forget to close the files when you don't need them anymore ...

BTW, on line 7 you're using #define to declare a constant, it's better to use const instead of #define ...

It's maybe better when you change line 11 to: ifstream startnumb; You shouldn't forget to close the files when you don't need them anymore ...

Thank You !
I'm going to change the number using fstream later and also thats not all of my code I closed the file later on. Someone close this thread.

It's maybe better when you change line 11 to: ifstream startnumb; You shouldn't forget to close the files when you don't need them anymore ...

BTW, on line 7 you're using #define to declare a constant, it's better to use const instead of #define ...

Why ?

Why ?

Consider the following program using #define :

#include <iostream>

using namespace std;

#define BEGIN 20
#define END 60
#define LENGTH END-BEGIN

int main()
{
	cout << "LENGTH = " << LENGTH << endl;
	cout << "2*LENGTH = " << 2*LENGTH << endl;
	return 0;
}

Well, it doesn't return the result you expected ...
You probably expected it would return 80 ...

You don't have this problem using const :

#include <iostream>

using namespace std;

int main()
{
	const int BEGIN = 20;
	const int END = 60;
	const int LENGTH = END-BEGIN;
	
	cout << "LENGTH = " << LENGTH << endl;
	cout << "2*LENGTH = " << 2*LENGTH << endl;
	return 0;
}

Hope this helps !

BTW, const is invented to avoid the use of #define , so why would you continue using #define if you can use a better (and more C++ like instruction) ??

Thanks again +rep

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.