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

Recommended Answers

All 11 Replies

What does "written successively in the 15th line" mean?
Do you mean starting at the 15th column?

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

Declare your integer array and an ifstream:

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:

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

Close the ifstream.

Note that array indexes should start with 0, not 1.

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):

#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

You opened filein and you're reading from cin.

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?

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:

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.

Thanks a lot. That totally solves all my problems.

I really appreciate it. You guys are so helpful!

Aly

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.

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.

You can read in a character at a time using the get command. Check to see whether you just read in an equals sign. If not, read in another character. Once you have read in the equals sign, go on to read the numbers, as before, with a for-loop:

http://www.cplusplus.com/reference/iostream/istream/get/

I actually just read each character in a char variable, and it worked. But I was certainly inspired to do so using your advice.

Thanks a lot, guys, for your great help. I think, now, all problems I have in that concern are solved!

Aly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.