DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   need help reading from a file. (http://www.daniweb.com/forums/thread165246.html)

firstPerson Dec 31st, 2008 11:41 am
need help reading from a file.
 
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;
}

u8sand Dec 31st, 2008 11:56 am
Re: need help reading from a file.
 
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...

MosaicFuneral Jan 1st, 2009 12:00 am
Re: need help reading from a file.
 
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().

firstPerson Jan 1st, 2009 8:28 pm
Re: need help reading from a file.
 
Quote:

Originally Posted by u8sand (Post 768041)
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?

VernonDozier Jan 2nd, 2009 12:49 am
Re: need help reading from a file.
 
Quote:

Originally Posted by firstPerson (Post 768896)
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:

Quote:

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.


All times are GMT -4. The time now is 2:56 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC