[Help] Reading information out of file.

Hello everyone,

I need to read some infos out of a .txt-file.

The problem is that i dont know how to do because 'getline' won't work i think.

the file looks like this:

*******************
Inventory of MyInventory
*******************
Some info
More info
-------------------

000 Toys 6.25
a short description

126/44 Cards 2.25
some text
*******************
Inventory of MyInventory2
*******************
Some info
More info
-------------------

000 Toys 6.25
a short description

126/44 Cards 2.25
some text

So what I need to know:
- the name of the Inventory (MyInventory, MyInventory2...)
- the numbers in each row (000,126/44...)
- the name what it is (Toys,Cards...)
- the price (6.25,2.25...)
- and the description(problem here is, that there are not the same amount of words for each description)

I tried it with 'getline' but this is not working because the lines are so different each time.
Also I tried to use regular expressions but this is ways over my current skill level :S

Thank you
Tobi
closed account (2UD8vCM9)
You sure you tried getline?
I think you may have tried it, but may have tried using it incorrectly.
Let's say your file is called "herp.txt" and it's in the same directory as your .exe for your program

make sure you've included <fstream> and <strings>

To read from a file do

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
	ifstream MyFile;
	MyFile.open("herp.txt");
	if (MyFile.is_open())
	{
		cout << "File successfully opened. Reading and displaying output from the file." << endl;
		while (!MyFile.eof())
		{
			string LineFromFile;
			getline(MyFile,LineFromFile);
			cout << LineFromFile << endl;
		}
		cout << "We have reached the end of the file. All of its contents have been displayed." << endl;
	} else
	{
		cout << "Error! Could not find file" << endl;
	}
	system("pause");
	return 0;
}

Last edited on
Well yeah that is not the problem I'm sorry.

I forget to mention, that I need to put
- the name of the Inventory (MyInventory, MyInventory2...)
- the numbers in each row (000,126/44...)
- the name what it is (Toys,Cards...)
- the price (6.25,2.25...)
- and the description(problem here is, that there are not the same amount of words for each description)

in a variable for a later use.

So when I read the file I want to store the name of the inventory, the numbers and all that stuff in variables.
Topic archived. No new replies allowed.