943,920 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 546
  • C++ RSS
Dec 31st, 2008
0

need help reading from a file.

Expand Post »
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 :

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Dec 31st, 2008
0

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jan 1st, 2009
0

Re: need help reading from a file.

Or just:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 1st, 2009
0

Re: need help reading from a file.

Click to Expand / Collapse  Quote originally posted by u8sand ...
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

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Jan 2nd, 2009
0

Re: need help reading from a file.

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: variables scope
Next Thread in C++ Forum Timeline: simple visual studio express problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC