Problems reading from a file with a loop

Hi, I want to do a function that reads info from a txt file writed in one column that haves a termination string that is "/". I want this "/" to be the end of the loop but I'm only creating an infinite loop or something, and I can't understand why... Please, can you help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  void loadTXT(tList &myList){
	ifstream file;
	int i = 0;
	file.open("purchases.txt");
	getline(file, myList.products[i].name);
	while(myList.products[i].name != "/"){
		file >> myList.products[i].price;
		getline(file, myList.products[i].establishment);
		i++;
		getline(file, myList.products[i].name);
	}
	file.close();
}
Please show a small sample of your input file.

You should be checking to insure your file actually opened, something like this:
1
2
3
4
5
6
   ifstream file("purchases.txt")
   if(!file) // Always check that the file opens properly!
   {
        throw("Failed to open purchases.txt");
   }
   ...



Of course, my input file is like this:

Leche
3.66
DIA_Corredera
Cacahuete
1.31
DIA_Corredera
Spaguetti_1kg
0.77
DIA_Corredera
Aceituna_Jolca
0.60
DIA_Corredera
/
Last edited on
Are you sure that you're actually reading the file contents correctly? Have you tried printing the variable contents after you've read them?

I would guess that there is a problem reading your file that is being caused by mixing the extraction operaotr>> and getline().


The thing is I'm unable to do it, cause windows gives an error when I execute the .exe file.
Nothing apears in the console but the console cursor and the error message from windows.

I tried to change it by not using getline() putting a "_" instead of white space in the txt file and I obtain the same result.
Are you testing to see that the file opened correctly?

Did you run the program with you debugger? The debugger will be able to tell you exactly where it detects the problem and you can view the variables at the time of the crash.

Last edited on
I tested it by using an if else statement and the function .is_open(). The result seems to be that it's open.

I have never used the debugger before, and I don't have a clue of how to use it. I think that it's out of my range right now.
Last edited on
I think that it's out of my range right now.

You really really need to learn how to use your debugger. Find an read some documentation for the IDE/compiler you're using.

Without a complete program that illustrates your problem there is no way I can tell where your program is failing.

I will post the complete program that I have, if that can help:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream> // para operaciones de entrada/salida (cin/cout).
#include<fstream> // (ifstream/ofstream).

using namespace std;

const int MAXprod = 200;		// maximo de productos que contendrá el array.

struct tProduct{
	string name;
	double price;
	string establishment;
};

struct tList{
	tProduct products[MAXprod];
	int counter;
};

void loadTXT(tList &);
	// Función principal:
int main()
{
	tList list;
	loadTXT(list);
	cout << list.products[0].name;
	return 0;
}

	// Función que carga la información contenida en un txt para su manejo por el programa:

void loadTXT(tList &myList){
	ifstream file;
	int i = 0;
	file.open("purchases.txt");
	if (!file.is_open()){
		cout << "File couldn't be open" << endl;
	}
	else{
		cout << "File open" << endl;
		file >> myList.products[i].name;
		while(myList.products[i].name != "/"){
			file >> myList.products[i].price;
			file >> myList.products[i].establishment;
			i++;
			file >> myList.products[i].name;
		}
		file.close();
	}
	
}
Last edited on
If your file fails to open you need you need to stop the program, not just print an error message.

If your strings have spaces embedded within them you can't use the extraction operator>> since it stops processing strings when it encounters whitespace characters.

How can I stop it?

I know, but I changed the txt file so now it not contains white spaces, instead it have "_" between words.
I did that because you suggested me that the problem could be mixing >> and getline.
Last edited on
Topic archived. No new replies allowed.