Help With .txt Files

I have this program where I'm trying to get text from a file and use it. Which is all well and good, I've managed to get the specific text from the file that I want. My problem is that I have a really long line so I put some '\n' in it, but when I grab them from the text file and output the string it just shows the '\n' as part of the string.

I'm confused as to why this is happening instead of the program just outputting a new line, and if there's a way to fix it.

In case you are wondering I made a test program to get the same problem I had with my main program and here's the test program code

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

int main()
{
	string a;
	ifstream b("blah.txt");

	if(b.is_open())
	{
		while(b.good())
		{
			getline(b,a);
		}
	}

	cout << a;
}


and the test .txt file.

Hello World!\nAnd Hello Again!
"\n" != '\n'

Your test.txt file should look like:
Hello World!
And Hello Again!
Last edited on
Putting my txt file like you've suggested won't work for my purposes.

I need to be able to output multiple lines with just one use of a string, instead of putting the Hello World! into the string, outputting that, then putting the And Hello Again! into the string (overwriting the what was there before), and then outputting that.

The reason this won't work is because I am sending the string, which contains the text I just got from txt file, into a function and then breaking the loop.
This example is useless. Can you give us the code, where you write to the file?
Ok, here's the code for my main program.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * main.cpp
 *
 *  Created on: Jun 12, 2011
 *      Author: James
 */

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#include "room.h"

Room getRoom(int);

int main()
{
	string action;
	Room location = getRoom(1);

	location.enterRoom();
	cout << "What do you do?" << endl;
	cin >> action;

	return 0;
}

// This function finds the room that is wanted, creates
// that room, and sends it back.
Room getRoom(int x)
{
	Room a;
	string b, c;
	int pos;
	ifstream roomDesc ("Room_Descriptions.txt");

	cout << "woah!\n";

	if(roomDesc.is_open())
	{
		while(roomDesc.good())
		{
			getline(roomDesc,b);
			pos = b.find(". ");
//			cout << "pos is " << pos << endl;
			c = b.substr(0,pos);
//			cout << "c is " << c << endl;
//			cout << "c num is " << atoi(c.c_str()) << endl;
			if (atoi(c.c_str())==x)
			{
				b = b.substr(pos+2);
				break;
			}

		}
	}

	a.setDescription(b);

	return a;
}


then the code for room.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * room.h
 *
 *  Created on: Aug 16, 2011
 *      Author: james ctr grider
 */

#include <string>
using namespace std;

#ifndef ROOM_H_
#define ROOM_H_

class Room
{
	string description;		// Let's the player know what's in the room.

public:
	Room();
	void setDescription(string a);
	void enterRoom();
};

#endif /* ROOM_H_ */ 


and the code for room.cpp
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
/*
 * room.cpp
 *
 *  Created on: Aug 16, 2011
 *      Author: james ctr grider
 */

#include <iostream>
using namespace std;

#include "room.h"

Room::Room()
{
	description = "This is an empty room.\n";
}

void Room::setDescription(string a)
{
	description = a;
}

void Room::enterRoom()
{
	cout << description;
}


and the text in Room_Descriptions.txt
1
2
3
4
5
6
Room_Descriptions.txt

 Created on: Aug 16, 2011
     Author: james ctr grider

1. You awaken to find yourself in a small bedroom.\nYou are on a bed and you notice a wallet sitting on the side table next to you.\nOn the far side of the room is a door.\n


As you can see I'm making a very simple MUD and I want the line starting with 1. which will be put into my string variable b then the 1. will be taken out and b will be sent to room a as it's description. This will then be returned to location.
I just solved my own problem.

I added a third string variable, d. I then changed,
1
2
3
4
5
if (atoi(c.c_str())==x)
{
	b = b.substr(pos+2);
	break;
}

to
1
2
3
4
5
6
if (atoi(c.c_str())==x)
{
	b = b.substr(pos+2);
	d += b;
	d += "\n";
}

and
a.setDescription(b); to a.setDescription(d);.

And to finish this off deleted all the "\n" in my txt file and just put the next part on a new line.


So... Thanks Duoas, you were right the whole time! :D
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
Room getRoom(int x)
{
	Room a;
	string b, c;
	int pos;
	ifstream roomDesc ("Room_Descriptions.txt");

	cout << "woah!\n";

	if(roomDesc.is_open())
	{
		while(roomDesc.good())
		{
			getline(roomDesc,b);
                        
			string::size_type s = 0;
			while((s = b.find("\\n", s)) != string::npos)
			    b.replace(s, 2, 1, '\n');

			pos = b.find(". ");
//			cout << "pos is " << pos << endl;
			c = b.substr(0,pos);
//			cout << "c is " << c << endl;
//			cout << "c num is " << atoi(c.c_str()) << endl;
			if (atoi(c.c_str())==x)
			{
				b = b.substr(pos+2);
				break;
			}

		}
	}

	a.setDescription(b);

	return a;
}

Is it that you want?
Yeah that works too! I appreciate your help!

I'm going to use your method it keeps me from having multiple lines in my txt file with things like:
1. something
1. something else
1. more for the same room
Last edited on
Topic archived. No new replies allowed.