fstream.peek() moving file pointer?

Hi there. I am trying to write a program that reads a series of commands from a text file and draws text art into an output file. I want to make bolded lines (a series of '#' chars) take precedence over non bolded lines (series of '*' chars). To do this, I am trying to use fstream.peek() to check if the char at the current position is a '#', however, when I use this method my output is somehow interfered with and the characters get printed wildly far off from where they are meant to be. What do?

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <fstream>
#include <iostream>

using namespace std;

void drawUp(fstream &file, bool &down, bool &b, int &numLength)
{
    char c = ' ';

    //loop to draw line/move pointer
    for(int i = 1; i <= numLength; i++)
    {
        //move pointer up one line
        file.seekp(-52, ios::cur);
        //if pen is down, draw
        if(down)
        {
            //if bold, use #, otherwise use *
            if(b)
            {
                file << "#";
                //move pointer back to offset drawing movement
                file.seekp(-1, ios::cur);
            }
            else
            {
                //if a bold line is in place, do not overwrite
                c = file.peek();
                if(c != '#')
                {
                    file << "*";
                    //move pointer back to offset drawing movement
                    file.seekp(-1, ios::cur);
                }
            }
        }
    }
}

void drawDown(fstream &file, bool &down, bool &b, int &numLength)
{
    char c;

    //loop to draw line/move pointer
    for(int i = 1; i <= numLength; i++)
    {
        //move pointer down one line
        file.seekp(52, ios::cur);
        //if pen is down, draw
        if(down)
        {
            //if bold, use #, otherwise use *
            if(b)
            {
                file << "#";
                //move pointer back to offset drawing movement
                file.seekp(-1, ios::cur);
            }
            else
            {
                //if a bold line is in place, do not overwrite
                c = file.peek();
                if(c != '#')
                {
                    file << "*";
                    //move pointer back to offset drawing movement
                    file.seekp(-1, ios::cur);
                }
            }
        }
    }
}

void drawLeft(fstream &file, bool &down, bool &b, int &numLength)
{
    char c = ' ';

    //move pointer to the end of the line in order to draw to the right
    file.seekp((0 - numLength), ios::cur);
    //loop to draw line
    for(int i = 1; i <= numLength; i++)
    {
        //if pen is down, draw line
        if(down)
        {
            //if bold, use #, otherwise use *
            if(b)
                file << "#";
            else
            {
                //if a bold line is in place, do not overwrite
                c = file.peek();
                if(c != '#')
                    file << "*";
            }
        }
    }
    //move pointer back to the end of the line after drawing
    file.seekp((0 - numLength), ios::cur);
}

void drawRight(fstream &file, bool &down, bool &b, int &numLength)
{
    char c = ' ';

    //loop to draw line
    for(int i = 1; i <= numLength; i++)
    {
        //if pen is down, draw line
        if(down)
        {
            //if bold, use #, otherwise use *
            if(b)
                file << "#";
            else
            {
                //if a bold line is in place, do not overwrite
                c = file.peek();
                if(c != '#')
                    file << "*";
            }
        }
        //if pen is up, move without drawing
        else
            file.seekp(1, ios::cur);    
    }
}

void printFile(fstream &file)
{
    //open a file stream to read characters
    ifstream print("paint.txt");

    //loop through the file char by char
    while(print.good())
        //print each character
        cout << (char)print.get();
    //print newlines to add buffer between prints
    cout << endl << endl;
    //close the file
    print.close();
}

int main()
{
    //open file stream for commands
    ifstream commands("commands.txt");
    //create a new file for output called paint.txt
    ofstream create("paint.txt");
    create.close();
    //convert paint to fstream to access i/o functions.
    fstream paint("paint.txt");

    char c, direction, ch  = ' ';
    bool penDown, bold     = false;
    int length             = 0;

    //loop to set up 50x50 canvas to draw on
    for(int i = 1; i <= 50; i++)
    {
        for(int j = 1; j <= 50; j++)
            paint.put(' ');
        paint.put('\n');
    }
    //reset pointer to start of file
    paint.seekp(0, ios::beg);

    //loop through commands char by char
    while(commands.get(c))
    {
        //if command is 1, raise the pen
        if(c == '1')
            penDown = false;
        //if command is 2, set pen down
        else if(c == '2')
            penDown = true;
        //if command is 3, prepare to draw
        else if(c == '3')
        {
            //find the direction by skipping possible spaces and commas
            do commands.get(ch);
            while((ch == ' ')||(ch == ','));
            direction = ch;
            //find the length by skipping possible spaces and commas
            do commands.get(ch);
            while((ch == ' ')||(ch == ','));
            //reset character position to use >> for length
            commands.unget();
            commands >> length;

            //if direction is up, call drawUp
            if((direction == 'N')||(direction == 'n'))
                drawUp(paint, penDown, bold, length);
            //if direction is down, call drawDown
            else if((direction == 'S')||(direction == 's'))
                drawDown(paint, penDown, bold, length);
            //if direction is right, call drawRight
            else if((direction == 'E')||(direction == 'e'))
                drawRight(paint, penDown, bold, length);
            //otherwise call the only remaining direction, drawLeft
            else
                drawLeft(paint, penDown, bold, length);
        }
        //if command is 4, print output to the console
        else if(c == '4')
            printFile(paint);
        //if command is B, set bold to on
        else if(c == 'B')
            bold = true;
        //if command is b, set bold to off
        else if(c == 'b')
            bold = false;
    }
    //print final canvas
    printFile(paint);

    //close files.
    commands.close();
    paint.close();

    return 0;
}

Commands:
1 - pen up
2 - pen down
3 - draw/move
4 - print to console
B - bold on
b - bold off

Sample input file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
B
3, E, 10
b
3, W, 5
3, S, 20
B
3, N, 10
b
3, S, 3
1
3, E, 10
2
3, S, 10
B
3, W, 10



Note that peek() is used in 4 different functions (drawUp, drawDown, drawLeft, and drawRight). If I comment out the precedence checks (peek() and if(c != '#')) the code works exactly as intended (minus the precedence thing).

What am I doing wrong? How can I fix this?
Last edited on
Generally you want to seek each time you switching from reading to writing. Otherwise you will have problems with buffer synchronisation.
Topic archived. No new replies allowed.