Saving strings from a text file to a array

Hello. I need help associating the strings of a text file to an array.
So I have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <fstream>

int main()
{
	using namespace std;

	ifstream file("projeto1.txt");
	if (file.is_open())
	{
		string myArray[5];

		for (int i = 0; i < 5; ++i)
		{
			file >> myArray[i];
			cout << myArray[i] << endl;
		}
	}

}


Now there are two things that I want to know.
First the text file will vary and by so the position of a certain string will also vary so how can I save a specific part of the text file?
Second in my for cicle how should I go about putting the number of word into it (i < number_of_words ) should I just put a huge number? but that would be bad for the machine i guess...

Thanks in advance for your help.
First the text file will vary and by so the position of a certain string will also vary so how can I save a specific part of the text file?

Without seeing a small sample of your text file and you being more specific as to what you're trying to accomplish it would only be guess work to answer this question.

Second in my for cicle how should I go about putting the number of word into it (i < number_of_words ) should I just put a huge number?

Well instead of using an array I suggest you use a vector and instead of a for loop just insert all the strings into the vector.
My text file looks like this:

Title: Projeto
Company: UMA
Teacher: Joaquim
Duration: 5 months
Keywords: #best_project_ever
Expenses:
Equipment:$300
Food: $50
Consumable expenses: $100
Consulting expenses: $40
Travel: $150
Extras: $500

and now I want to get all those expenses values (300,50,100,.....) and do the sum . I would also like to get the title and the company name, the thing is the company name will vary hence my first question..
Is there only one record in the file? Or can there be multiple records?

I recommend you use a class or structure to hold the information for each record from the file.

If you read an entire line into a string you should be able to parse the lines with a stringstream to remove the relevant data.

There can be multiple records, ok I will try what you sugested.
Topic archived. No new replies allowed.