need help reading from a file.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 1,164
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 145
firstPerson's Avatar
firstPerson firstPerson is online now Online
Veteran Poster

need help reading from a file.

 
0
  #1
Dec 31st, 2008
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 :

  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. int main(){
  6.  
  7. ifstream num("num.txt");
  8.  
  9. __int64 arry[25000]={0};
  10.  
  11. for(int i=0; (i<2500 && (!num.eof())) ; i++)
  12. {
  13. num >> arry[i];
  14.  
  15.  
  16. }
  17.  
  18.  
  19. return 0;
  20. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: need help reading from a file.

 
0
  #2
Dec 31st, 2008
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

  1. char line[2500];
  2. ifstream num("num.txt");
  3. num.getline(line,2500); // get the line
  4. num.close();
  5. int array[2500];
  6. for(int i = 0; i < (int)strlen(line); i++)
  7. array[i] = (int)line[i]-48; // -48 because '0' = 48, '1' = 49, it gives normal int...
Last edited by u8sand; Dec 31st, 2008 at 12:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 951
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: need help reading from a file.

 
0
  #3
Jan 1st, 2009
Or just:
  1. ifstream file("name");
  2. sting str;
  3.  
  4. if(file.is_open())
  5. {
  6. file >> str;
  7.  
  8. file.close();
  9. }

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().
Last edited by MosaicFuneral; Jan 1st, 2009 at 12:18 am.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,164
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 145
firstPerson's Avatar
firstPerson firstPerson is online now Online
Veteran Poster

Re: need help reading from a file.

 
0
  #4
Jan 1st, 2009
Originally Posted by u8sand View Post
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

  1. char line[2500];
  2. ifstream num("num.txt");
  3. num.getline(line,2500); // get the line
  4. num.close();
  5. int array[2500];
  6. for(int i = 0; i < (int)strlen(line); i++)
  7. 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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help reading from a file.

 
0
  #5
Jan 2nd, 2009
Originally Posted by firstPerson View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC