| | |
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:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2009
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
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
•
•
Join Date: Jan 2008
Posts: 3,833
Reputation:
Solved Threads: 503
Declare your integer array and an ifstream:
Skip the first 14 lines using
Set up a loop and read in the 333 integers:
Close the ifstream.
Note that array indexes should start with 0, not 1.
C++ Syntax (Toggle Plain Text)
int A[333]; 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:
C++ Syntax (Toggle Plain Text)
for (int i = 0; i < 333; i++) input >> A[i];
Close the ifstream.
Note that array indexes should start with 0, not 1.
•
•
Join Date: Apr 2009
Posts: 14
Reputation:
Solved Threads: 0
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):
Could you please help me with my mistake?
Thanks.
Aly
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):
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <stdio.h> #include <string> #include <cstdlib> using namespace std; int main (void) { int A[333]; char name[1000]; ifstream filein; filein.open("input.txt"); for(int h=0;h<9;h++){ cin.getline(name,10); } for (int i = 0; i < 6; i++) filein>> A[i]; filein.close(); for(int yy=0;yy<7;yy++){ cout<<A[yy]; } return 0; }
Could you please help me with my mistake?
Thanks.
Aly
Last edited by amegahed3; Apr 18th, 2009 at 4:01 am.
•
•
Join Date: Apr 2009
Posts: 14
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jan 2008
Posts: 3,833
Reputation:
Solved Threads: 503
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
string throwAway; filein >> throwAway; // read in "value=" for (int i = 0; i < 333; i++) 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
•
•
Join Date: Apr 2009
Posts: 14
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- fstream Tutorial (C++)
Other Threads in the C++ Forum
- Previous Thread: Need advice about list of class of classes.
- Next Thread: multiple definition of `funny_words()'
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






