Need a guru to take a gander

I'm thinking I'm on the right track but the code isn't displaying anything. I'm trying to create a program to reads lines of text and appends them to char buffer[1000]. I get no errors but nothing is showing up can one of you gurus take a peak at my code and tell me what I'm doing wrong? Much obliged, Sean
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	const int BUFFER_CAPACITY = 1000;
	const int LINES_CAPACITY = 100;
	char buffer[BUFFER_CAPACITY];
	char* lines[LINES_CAPACITY];
	int buffer_size = 0;
	int lines_size = 0;
	ifstream fin;
	fin.open("data.txt");
	bool more = true;
	bool newline = true;
	while (fin >> more)
	{			
		char c = cin.get();		
		if (cin.fail())
		{
			more = false;
		}
		else if (buffer_size >= BUFFER_CAPACITY)
		{
			more = false;
		}
		if (newline)
		{
			if (lines_size < LINES_CAPACITY)
			{
				lines[lines_size] = buffer + buffer_size;
				lines_size++;
				newline = false;
			}
			else { more = false; }
		}
		if (more)
		{
			if (c == '\n')
			{
				buffer[buffer_size] = '\0';
				buffer_size++;
				newline = true;
			}
			else
			{
				buffer[buffer_size] = c;
				buffer_size++;
			}
		}
	}
	buffer[BUFFER_CAPACITY - 1] = '\0';	
	for (int i = lines_size - 1; i >= 0; i--)
	{
		cout << lines[i] << endl;
	}
	fin.close();
	system("pause");
	return 0;
}
Line 19 is attempting to read a boolean from a file. This is likely failing and putting the stream in a bad state, which causes the entire while loop to be skipped:

 
while (fin >> more) // <- you are extracting to 'more' 


You probably don't want to do that. But instead just want to check to see if the stream is in a good state:

 
while(fin)




Also... I don't know if you are doing this to just experiment with pointers or what... but this is significantly easier if you just use strings and getline() instead of using char arrays and getting 1 character at a time.
Disch,
I'm trying to deal with pointers and I might be approaching the population of the loop incorrectly...
figured it out... Thank Disch

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
65
#include <iostream>
#include <fstream>


using namespace std;

int main()
{
	const int BUFFER_CAPACITY = 1000;
	const int LINES_CAPACITY = 100;
	char buffer[BUFFER_CAPACITY];
	char* lines[LINES_CAPACITY];
	int buffer_size = 0;
	int lines_size = 0;
	char str[1000];
	cout << "Enter the name of file:";
	cin.get(str, 1000);
	ifstream is(str);

	bool more = true;
	bool newline = true;
	while (more)
	{			
		char c = is.get();		
		if (cin.fail())
		{
			more = false;
		}
		else if (buffer_size >= BUFFER_CAPACITY)
		{
			more = false;
		}
		if (newline)
		{
			if (lines_size < LINES_CAPACITY)
			{
				lines[lines_size] = buffer + buffer_size;
				lines_size++;
				newline = false;
			}
			else { more = false; }
		}
		if (more)
		{
			if (c == '\n')
			{
				buffer[buffer_size] = '\0';
				buffer_size++;
				newline = true;
			}
			else
			{
				buffer[buffer_size] = c;
				buffer_size++;
			}
		}
	}
	buffer[BUFFER_CAPACITY - 1] = '\0';	
	for (int i = lines_size - 1; i >= 0; i--)
	{
		cout << lines[i] << endl;
	}
	system("pause");
	return 0;
} 
Topic archived. No new replies allowed.