Reading specific numbers in a .txt file and record them in an array

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

Join Date: Apr 2009
Posts: 14
Reputation: amegahed3 is an unknown quantity at this point 
Solved Threads: 0
amegahed3 amegahed3 is offline Offline
Newbie Poster

Reading specific numbers in a .txt file and record them in an array

 
0
  #1
Apr 17th, 2009
Hi All,

I'm a C++ beginner, and I face the following problem:

There's a .txt file that has some number written successively in the 15th line of the file. Each number (that could be a one or 2 digits number) is separated by a space from the preceding and succeeding numbers.

What I want to do is to read each of those numbers (their total number is 333) and store each of them( in its specific order) in an array of integers. i.e. I need to have an array declared as int [333], and each of its entries is one of the 333 numbers in the same order they appear in the text file.

Suppose that the file is called input.txt and that its path is c:\input.txt.

Could you please provide me with a simple code for that?

Thanks a lot.

Aly
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Reading specific numbers in a .txt file and record them in an array

 
0
  #2
Apr 17th, 2009
What does "written successively in the 15th line" mean?
Do you mean starting at the 15th column?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: amegahed3 is an unknown quantity at this point 
Solved Threads: 0
amegahed3 amegahed3 is offline Offline
Newbie Poster

Re: Reading specific numbers in a .txt file and record them in an array

 
0
  #3
Apr 17th, 2009
Originally Posted by nucleon View Post
What does "written successively in the 15th line" mean?
Do you mean starting at the 15th column?

It means that line 15 (row 15) in the .txt file has the following structure for example:

2 23 434 22 12 4 7 9 0 1 4 8 9

And all what I need to do is to read those numbers (that are again in the 15th line of the .txt file, and store them in an integer array. So, for example, for the previous example, the array that I want should be like this:

A[1]=2
A[2]=23
A[3]=434
A[4]=22

And so on.

Hope it's clear now. Thanks for your help in advance.

Aly
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,833
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: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Reading specific numbers in a .txt file and record them in an array

 
0
  #4
Apr 18th, 2009
Declare your integer array and an ifstream:

  1. int A[333];
  2. ifstream input;

Skip the first 14 lines using getline to grab the line up to the newline and then do nothing with it. You've now reached the 15th line.

Set up a loop and read in the 333 integers:

  1. for (int i = 0; i < 333; i++)
  2. input >> A[i];

Close the ifstream.

Note that array indexes should start with 0, not 1.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: amegahed3 is an unknown quantity at this point 
Solved Threads: 0
amegahed3 amegahed3 is offline Offline
Newbie Poster

Re: Reading specific numbers in a .txt file and record them in an array

 
0
  #5
Apr 18th, 2009
Thanks a lot for your reply.

Actually, I tried the ideas you told me about. I think my problem is with getline. Here's my code, which compiles, but doesn't give any results (it takes forever at getline):

  1. #include <fstream>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <string>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. int main (void)
  9. {
  10. int A[333];
  11. char name[1000];
  12. ifstream filein;
  13. filein.open("input.txt");
  14. for(int h=0;h<9;h++){
  15. cin.getline(name,10);
  16. }
  17.  
  18. for (int i = 0; i < 6; i++)
  19. filein>> A[i];
  20.  
  21. filein.close();
  22. for(int yy=0;yy<7;yy++){
  23. cout<<A[yy];
  24. }
  25.  
  26. return 0;
  27. }

Could you please help me with my mistake?

Thanks.

Aly
Last edited by amegahed3; Apr 18th, 2009 at 4:01 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading specific numbers in a .txt file and record them in an array

 
0
  #6
Apr 18th, 2009
You opened filein and you're reading from cin.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: amegahed3 is an unknown quantity at this point 
Solved Threads: 0
amegahed3 amegahed3 is offline Offline
Newbie Poster

Re: Reading specific numbers in a .txt file and record them in an array

 
0
  #7
Apr 18th, 2009
Ooooh, what a stupid mistake.
Thanks a lot, man. It now works fine.

I just have one last question:

If that 15th line contains "value=" and then the numbers (with a space between each 2 numbers). How can I skip those initial characters, and begin reading and storing my numbers in A[]?

So, to make it more clear, in my .txt file, the 15th line looks like this for example:

value= 1 22 434 3 100

And I want to have an array of integers A[7], where:
A[1]=1
A[2]=22
A[3]=434
A[4]=3
A[5]=100

How can that be done, please?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,833
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: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Reading specific numbers in a .txt file and record them in an array

 
0
  #8
Apr 18th, 2009
Originally Posted by amegahed3 View Post
Ooooh, what a stupid mistake.
Thanks a lot, man. It now works fine.

I just have one last question:

If that 15th line contains "value=" and then the numbers (with a space between each 2 numbers). How can I skip those initial characters, and begin reading and storing my numbers in A[]?

So, to make it more clear, in my .txt file, the 15th line looks like this for example:

value= 1 22 434 3 100

And I want to have an array of integers A[7], where:
A[1]=1
A[2]=22
A[3]=434
A[4]=3
A[5]=100

How can that be done, please?

If you know FOR SURE that there is NO space between "value" and "=" and that there IS a space between "=" and the first number, just read "value=" into a string and throw it away before you read in the numbers:

  1. string throwAway;
  2. filein >> throwAway; // read in "value="
  3. for (int i = 0; i < 333; i++)
  4. filein >> A[i]; // read in numbers

Again, this only works if the assumptions listed above are valid.
Last edited by VernonDozier; Apr 18th, 2009 at 12:40 pm. Reason: screwed up on sample code
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: amegahed3 is an unknown quantity at this point 
Solved Threads: 0
amegahed3 amegahed3 is offline Offline
Newbie Poster

Re: Reading specific numbers in a .txt file and record them in an array

 
0
  #9
Apr 18th, 2009
Thanks a lot. That totally solves all my problems.

I really appreciate it. You guys are so helpful!

Aly
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: amegahed3 is an unknown quantity at this point 
Solved Threads: 0
amegahed3 amegahed3 is offline Offline
Newbie Poster

Re: Reading specific numbers in a .txt file and record them in an array

 
0
  #10
Apr 18th, 2009
I'm really sorry guys for the continuous disturbance, but on another thought, I found that my .txt file, its 15th line is in the form of:

value =1 22 434 3 100

Which means that there is a space between the word "value", and "=", while there is no space between "=" and the first number that I want to read.

Now, if I read 2 throwaway strings, the first number is read with the "=" in the second string, and so, the first number is not read in A[0]. How can that be solved in this case?

Thanks in advance.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC