Some Questions on ifstream Function

My first question is should I put name or name2 in the while loop (). How do I clear drawer like it asks? When I try to print the first character and then print the rest of the line did I correctly do so by printing firstLetter and name2??

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
void loadFile(Graphics& drawer)
{

	/*Opens a file
		*     Start with a blank canvas(drawer)
		*     Start reading from file.For each line....
		*        Read the 1st character to determine shape
		*        Read the shape : L reads a line, C reads a circle, T read a triangle
		*            R reads a rectangle.
		*            For any other character, clears drawer and prints
		*            "Error in input file: " << [character already read]
		* << [all chars remaining on the line] << endl;
	*Draw shape on canvas
		*     Close file
		*     Print "[Loaded filename]"
    TODO: implement */
	ifstream name;
	string name2 = openFile(name);
	char firstLetter;
	while (name >> firstLetter) {
		if (firstLetter == 'L') {
			Line l;
			name >> l;
			l.draw(drawer);
		}

		else if (firstLetter == 'C') {
			Circle c;
			name >> c;
			c.draw(drawer);
		}

		else if (firstLetter == 'T') {
			Triangle t;
			name >> t;
			t.draw(drawer);
		}

		else if (firstLetter == 'R') {
			Rectangle r;
			name >> r;
			r.draw(drawer);
		}

		else {

			cout << "Error in input file : " << firstLetter
				 << name2 << endl;
		}


	}


	name.close();
	cout << "[Loaded " << filename << "]";
}


Last edited on
My first question is should I put name or name2 in the while loop (). How do I clear drawer like it asks? When I try to print the first character and then print the rest of the line did I correctly do so by printing firstLetter and name2??

Can you make the problem more clear?
My first question is should I put name or name2 in the while loop ().

There are a couple of errors in the way you try to open the file.
17
18
    ifstream name;
    string name2 = openFile(name);

First I'd suggest a more meaningful variable name for the stream than just 'name'.
I tend to use something like fin and fout for input and output streams respectively, they are brief, and the similarity to cin and cout make their meaning fairly clear.
Then your code might look like this:
17
18
    string filename = "data.txt";
    ifstream fin(filename);    // define and open the file 


I don't know about your Graphics usage, it presumably belongs to some library which you are using.

A simplified version of your code, with no graphics, might look like this:
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
void loadFile(Graphics& drawer)
{
    string filename = "data.txt";
    
    ifstream fin(filename);
    if (!fin)
    {
        cout << "File not open: " << filename << '\n';
        return;
    }
    
    char firstLetter;
    string body;
    
    while (fin >> firstLetter) 
    {
        if (firstLetter == 'L') 
        {
            getline(fin, body);
            cout << "Line: " << body << '\n'; 
        }
        else if (firstLetter == 'C') 
        {
            getline(fin, body);
            cout << "Circle: " << body << '\n'; 
        }
        else if (firstLetter == 'T') 
        {
            getline(fin, body);
            cout << "Triangle: " << body << '\n'; 
        }
        else if (firstLetter == 'R') 
        {
            getline(fin, body);
            cout << "Rectangle: " << body << '\n'; 
        }
        else 
        {
            getline(fin, body);         
            cout << "Error in input file : " << firstLetter
                 <<  body << '\n'; 
        }
    }

    cout << "[Loaded " << filename << "]";
}

Input file:
L this is a line
Once upon a time
C a circle
It was a dark and stormy night
R a rectangle
T a triangle
S something else

Output:
Line:  this is a line
Error in input file : Once upon a time
Circle:  a circle
Error in input file : It was a dark and stormy night
Rectangle:  a rectangle
Triangle:  a triangle
Error in input file : S something else
[Loaded data.txt]

Topic archived. No new replies allowed.