Reading Line by Line from Text File into Char Array

Hey everyone,

I have a question about file streaming. I have a couple of text files that I'm trying to read from in order to put their contents into a class I called TextFile.
All that TextFile has is the following:

1
2
3
4
5
6
7
class TextFile
{
  public:
     TextFile(char *name);
     ~TextFile();
     char content[500];
}


So from the text files I want to find a way to read into the content array of a TextFile object...of course in the most efficient manner. By that I mean this...I've read a couple of things online, but all of them seemed to have read from the text into a char array in a character by character fashion. I'm wondering if there's a way to read from a text file into a char array in a LINE BY LINE fashion.

My current implementation of file streaming is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string line;

	std::ifstream backstory ("backstory.txt");
	if (backstory.is_open())
	{
		while (backstory.good())
		{
			getline(backstory,line);
			std::cout << line << std::endl;
		}
		backstory.close();
	}
	else
	{
		std::cout << "Unable to open file" << std::endl << std::endl;
	}


This outputs the contents of the text file in a line by line fashion into the standard output. I like the way it works a lot. Now I just want to redirect its destination to an array.

Thanks in advance for any help! :)
Have you thought about an array of strings?

1
2
3
4
5
6
7
string line[256];

for(int i = 0; backstory.good(); i++)
{
    getline(backstory, line[i]);
    cout<<line <<endl;    
}


I don't know if this is what you want but theres my two pence :)

I hope this helps.
Actually, I haven't though about using an array of strings. Thanks for bringing that up. Your proposition isn't exactly what I'm looking for as it still prints out to the stdout (cout) in C++. I want whatever's read to be fed into a char array. But your response led me to the answer I was looking for, so pat yourself on the back :D.

Lo and behold, this is what I was looking for:

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
#include <iostream>
#include <fstream>
#include "TextFile.h"

TextFile *OpenFile (const char *name)
{
	std::string line;
	TextFile *file = new TextFile();

	std::ifstream text (name);
	if (text.is_open())
	{
		while (text.good())
		{
			getline(text,line);
			file->content.append(line);
			file->content.push_back('\n');
		}
		text.close();
	}
	else
	{
		std::cout << "Unable to open file" << std::endl << std::endl;
	}
	return file;
}


One thing to note before I quickly say what the above function does is that I replaced the char content[500] in the TextFile class above (in my first post) with std::string content. This lead to things being much easier!

Anway, the OpenFile function takes an outside text file, parses it line by line, and puts it into the content variable of a TextFile object (but you can reformat the code to fit any object you're dealing with)...So in short, this function transforms an external text file into a C++ object with its contents stored within it <3. Hooray!

Thanks for the epiphany lk2019 :D!

(SIDE NOTE: For all you file streamers out there using C++, std::string is the way to go for anything that is PURE text. Forget about using char[] or char * --- std::string was built for that stuff :] )
Last edited on
One more thing for you to fix: this: while (text.good()) is wrong. It iterates one extra time after the end of file is reached, and appends one additional empty line to your string.

Typical (and correct) C++ line-by-line loop looks like this: while (getline(text, line)).

And while at it, consider using operators rather than member functions for strings, they are easier to compose into expressions:

1
2
3
4
while (getline(text,line) )
{
    file->content += line + '\n';
}
Last edited on
Topic archived. No new replies allowed.