hi,
I am curious, how can I read a int from a text file( ex. num.txt).
but this text file contains numbers without spacing
(ex.1212132132313...
23156897984969..
583852935792...)

The problem is that this file has no spaces between numbers and when i try to read it into an arry, the result is zero because it cant hold such huge --over 1000 integer.

here is my code :

#include<iostream>
#include<fstream>
using namespace std;

int main(){

	ifstream num("num.txt");
	
	__int64 arry[25000]={0};

	for(int i=0; (i<2500 && (!num.eof())) ; i++)
	{
		num >> arry[i];

	
	}


		return 0;
}

do you want all the numbers together? or only 1 at a time and get it into an array...
Here's what i did
get it as a string

char line[2500];
ifstream num("num.txt");
num.getline(line,2500); // get the line
num.close();
int array[2500];
for(int i = 0; i < (int)strlen(line); i++)
    array[i] = (int)line[i]-48; // -48 because '0' = 48, '1' = 49, it gives normal int...

Or just:

ifstream file("name");
sting      str;

if(file.is_open())
{
    file >> str;

    file.close();
}

If you're parsing a string, then possibly substring it and use istringstream to place it into an int.

You'd probably also have to check the string size(counting till non-num found), and compare it to string.max_size().

do you want all the numbers together? or only 1 at a time and get it into an array...
Here's what i did
get it as a string

char line[2500];
ifstream num("num.txt");
num.getline(line,2500); // get the line
num.close();
int array[2500];
for(int i = 0; i < (int)strlen(line); i++)
    array[i] = (int)line[i]-48; // -48 because '0' = 48, '1' = 49, it gives normal int...

I get it, but it seems that your code gets only a line. Where as I need about a hundred of line worth of numbers. Ant clue on how to tell the compiler that if it reaches a number < 0 , then skip it go to new line?

I get it, but it seems that your code gets only a line. Where as I need about a hundred of line worth of numbers. Ant clue on how to tell the compiler that if it reaches a number < 0 , then skip it go to new line?

What do you mean by "numbers". You want the individual digits? I'm not sure what you mean by this:

tell the compiler that if it reaches a number < 0 , then skip it go to new line

Are you referring to negative numbers, the negative sign, white space, what? If all you care about is the digits, just read in a character at a time using get . Use isdigit from the cctype library to check if the character is a digit. If it is, convert the character to an integer and add it to the array. If not, throw the character away. I'm not 100% sure this is what you want.

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.