Newlines in text files I/Os

Hello. I am writing this program for a class I'm taking and I'm encountering a problem. My text file is simulating a vehicle's license, with basic information and has 13 'columns' and 3 lines. My problem is not the reading from the file (I think), but the newlines it is not ignoring.

This is my text file (it has a select amount of spaces not displayed here though, which is why I move the cursor):

DPX193 Toyota Corolla 2005 Negro Y N N Y 4 STD Juan Garcia (787) 123-4567
CDE711 Nissan Altima 2009 Blanco Y Y Y Y 2 AUT Yamilet Lebron (787) 555-5555
HCL256 Acura RSX 2007 Gris Y Y Y Y 4 AUT Pedro Perez (787) 895-9238


This is part of my code:

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
string tab, tab1, tab2, tab3, marca, modelo, anio, color, pw, pl, alarm, ac, puertas, trans, condu, tel;

//all these variables are necessary because I have treat each column as a different piece of information//

	ifstream archivo;
	archivo.open("dealer-data.txt");
	
	//proof that the file is opening correctly//
	if(!archivo)
		cout<<"Error. File could not be opened";
	
	cout<<"Write the license plate you wish to get information about: ";
	getline(cin,tab);

	//Obtain the value of each license plate//
	getline(archivo,tab1,' ');
	archivo.seekg(96,ios::beg); //moves the cursor to the next line
	getline(archivo,tab2,' ');
	archivo.seekg(89,ios::cur); //moves the cursor the the next line
	getline(archivo,tab3,' ');

        if (tab==tab1)
                cout<<"Tablilla 1";
        else if (tab==tab2)
	{
		cout<<"Tablilla 2";
	}
	else if (tab==tab3)
		cout<<"Tablilla 3";
	else
	{
		cout<<"License plate does not exist";
	}


Up to that it all works fine, but if I output the values onto the screen putting the following code:

cout<<tab1<<tab2<<tab3<<endl;

I get this:

DPX193
CDE711
HCL256


I have no newlines in my cout code, yet they are printed as newlines. And as so, if I try to input any of the last two values and compare my tab variable to them, I end up getting my else condition: "License plate does not exist".

However, if I input the first value and compare tab==tab1, I get the correct message which is "Tablilla 1".

How can I ignore newlines/what am I doing wrong?

I cannot use vectors in the program.
Last edited on
I admit I can't understand why it's displaying newlines, but may I suggest using
archivo.ignore(0x0fffffff, '\n');
or
while (archivo.get() != '\n');
instead of setting the position to a value, that way it gets to the next line regardless of the length.
Edit: though I see now you said there are a set amount of spaces, I assume you mean a set amount of characters for each column, I guess then it doesn't matter.
Last edited on
Oh, thanks, but I just discovered my problem, I have to move the cursor one more character in each new line to get it to work. So instead of

1
2
3
4
5
6
//Obtain the value of each license plate//
	getline(archivo,tab1,' ');
	archivo.seekg(96,ios::beg); //moves the cursor to the next line
	getline(archivo,tab2,' ');
	archivo.seekg(89,ios::cur); //moves the cursor the the next line
	getline(archivo,tab3,' ');


It should be

1
2
3
4
5
6
//Obtain the value of each license plate//
	getline(archivo,tab1,' ');
	archivo.seekg(97,ios::beg); //CHANGED VALUE
	getline(archivo,tab2,' ');
	archivo.seekg(90,ios::cur); //CHANGED VALUE
	getline(archivo,tab3,' ');


Thanks, however for the help!
Last edited on
Topic archived. No new replies allowed.