<cstdio> fscanf problem

I have some problems with my code, i'm not sure if it's correcty written this is why i am here.
For example, i should get this:

cutie.in :
"
6 2
....XX
X.....
......
..B...
.XXX..
......
S
D
"

cutie.out:
"
....XX
X.....
......
......
.XXX..
B.....
"


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
#include <fstream>
#include <cstdio>
using namespace std;

FILE * is = fopen("cutie2.in", "r");
FILE * os = fopen("cutie.out", "w");

char a[1001][1001];
int k2, n;
char x;
int bi, bj;
short Direction;


void Read();
void Solve();
void WriteD1();
void WriteD2();
void WriteD3();
void WriteD4();

int main()
{
    Read();
    Solve();
    if (Direction == 1)
        WriteD1();
    if (Direction == 2)
        WriteD2();
    if (Direction == 3)
        WriteD3();
    if (Direction == 4)
        WriteD4();
    fclose(is);
    fclose(os);
    return 0;
}


void Solve()
{
    Direction = 3;
    for (int k = 0; k < k2; k++)
    {
        fscanf(is, "%c", &x);
        int i, j;
        if (x == 'S') Direction++;
        if (x == 'D') Direction--;
        if (Direction == 0) Direction = 4;
        if (Direction == 5) Direction = 1;
        i = bi;
        j = bj;
        if (Direction == 1)
        {
            i--;
            while (a[i][j] ==  '.')
                i--;
            i++;
            a[bi][bj] = '.';
            bi = i;
            bj = j;
            continue;
        }
        if (Direction == 2)
        {
            j++;
            while (a[i][j] ==  '.')
                j++;
            j--;
            a[bi][bj] = '.';
            bi = i;
            bj = j;
            continue;
        }
        if (Direction == 3)
        {
            i++;
            while (a[i][j] ==  '.')
                i++;
            i--;
            a[bi][bj] = '.';
            bi = i;
            bj = j;
            continue;
        }
        if (Direction == 4)
        {
            j--;
            while (a[i][j] ==  '.')
                j--;
            j++;
            a[bi][bj] = '.';
            bi = i;
            bj = j;
            continue;
        }
    }
    a[bi][bj] = 'B';
};

void Read()
{
    fscanf(is, "%d", &n);
    fscanf(is, "%d", &k2);
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
        {
            fscanf(is, "%c", &x);
            a[i][j] = x;
            if (a[i][j] == 'B') bi = i, bj = j;
        }
};


void WriteD1()
{
    for (int i = n; i > 0; --i)
    {
        for (int j = n; j > 0; --j)
        {
            fprintf(os, "%c", a[i][j]);
        }
        fprintf(os, "\n");
    }
};

void WriteD2()
{
    for (int i = 1; i <= n; ++i)
    {
        for (int j = n; j > 0; --j)
        {
            fprintf(os, "%c", a[j][i]);
        }
        fprintf(os, "\n");
    }
};

void WriteD3()
{
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            fprintf(os, "%c", a[i][j]);
        }
        fprintf(os, "\n");
    }
};

void WriteD4()
{
    for (int i = n; i > 0; --i)
    {
        for (int j = 1; j <= n; ++j)
        {
            fprintf(os, "%c", a[j][i]);
        }
        fprintf(os, "\n");
    }
};
closed account (o3hC5Di1)
Hi there,

As this is a C++ forum, and you're including <fstream> anyway, I would suggest using the c++ way of handling file I/O: http://www.cplusplus.com/doc/tutorial/files/

As for your problem, I'm not sure I understand your question. Could you please be a little more specific about what is not working correctly?

All the best,
NwN
Last edited on
Hey, thanks for the answer

I prefer fstream instead of cstdio, but this time, it seems i am forced to use cstdio.

I did a side by side comparison on my <fstream> code and the one with <cstdio> (<fstream> code works perfect, but doesn't fit int the time limit of 0.5 seconds, restrictions : n <= 1000, k2 <= 500000), the writing(fprintf) works just fine, but the reading(void Read(), most likely in the second for, at the char variables AND in void Solve(), at the char reading) has some problems, i don't know whats wrong, i should get this
"
....XX
X.....
......
......
.XXX..
B.....
"
but i get this thing:
"
....X
X
X...
..
...
...
..
B...
.
XXX..
"
which is totally wierd.

For example, even for the writing i found improvements in time. With fstream i got about 0.709s and with cstdio 0.574.
HELP PLS
On line 108, use: fscanf(is, " %c", &x);. Note the space before the %c which indicates the call should ignore whitespace.
omg dude, you're a sick genius MAN!! YOU DID IT MAN YOU DID ITTTT!!!
FINALLY SOMEONE FOUND THE MISTAKE!!!
Topic archived. No new replies allowed.