Problem with file loader

Pages: 12
Hello everybody.

For the past week or so, I've been working on a custom-algorithm generated line drawer. For the past 24 hours I've been staring at the code for the file loader and wondering why it won't work.

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
static int sx,sy; //X & Y resolution
static int finish; //Finish counter
static int color_alg_r, //Red algorithm
           color_alg_g, //Green algorithm
           color_alg_b; //Red algorithm

bool load_values()
{
    int xres[4]; //X resolution temporary storage
    int yres[4]; //Y resolution temporary storage
    int fin[10]; //Finish counter temporary storage
    int car[30],cag[30],cab[30]; //Color algorithms temporary storage

    //Initialize counter
    int a = 0;

    //Open algorithm
    FILE *file = fopen( "used_alg.alg" , "r+" );

    while ( a <= 3 )
    {
        //Get the X resolution
        xres[a] = fgetc(file);
        //Convert from ASCII to usable array
        xres[a] -= 48;
        ++a;
    }

    //Convert from array to usable integer
    sx = ((xres[0]*1000)+(xres[1]*100)+(xres[2]*10)+xres[3]);
    //Output just to make sure
    cout << sx << endl;

    //Initialize counter
    int b = 0;
    //Set what character we're looking for
    fseek ( file , 5 , SEEK_SET );

    while ( b <= 3 )
    {
        //Get Y resolution
        yres[b] = fgetc(file);
        //Convert ASCII to a usable array
        yres[b] -= 48;
        cout << yres[b] << endl; //Testing purposes
        ++b;
    }

    //Convert from array to usable integer
    sy = ((yres[0]*1000)+(yres[1]*100)+(yres[2]*10)+yres[3]);
    //Output just to make sure
    cout << sy << endl;

    //Close the file
    fclose( file );
}


It's reading 06000500

But it's outputting this;

"600
-38
0
5
5
-37945"

What did I do wrong? :O
Rethink your converions...
Even when I'm not converting it though, it comes out with random irrelevant numbers, and when I change the values in the text file, it comes out with the SAME exact thing! D:
This line is wrong:
fseek ( file , 5 , SEEK_SET ); // 5 ????
How is that wrong?

The first loop reads the first 4 characters.

Therefore, I want to set it to the fifth character.

That was actually to just test something, and I decided to leave it in.
Here's what you do.
Change it to 4 and see what happens.
Wtf, same exact output.

-38
0
5
5
-37945
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
#include <iostream>

#include <cstdlib>

using namespace std;

static int sx,sy; //X & Y resolution
static int finish; //Finish counter
static int color_alg_r, //Red algorithm
           color_alg_g, //Green algorithm
           color_alg_b; //Red algorithm

bool load_values()
{
    int xres[4]; //X resolution temporary storage
    int yres[4]; //Y resolution temporary storage
    int fin[10]; //Finish counter temporary storage
    int car[30],cag[30],cab[30]; //Color algorithms temporary storage

    //Initialize counter
    int a = 0;

    //Open algorithm
    FILE *file = fopen( "used_alg.alg" , "r+" );

    while ( a <= 3 )
    {
        //Get the X resolution
        xres[a] = fgetc(file);
        //Convert from ASCII to usable array
        xres[a] -= 48;
        ++a;
    }

    //Convert from array to usable integer
    sx = ((xres[0]*1000)+(xres[1]*100)+(xres[2]*10)+xres[3]);
    //Output just to make sure
    cout << sx << endl;

    //Initialize counter
    int b = 0;
    //Set what character we're looking for
    fseek ( file , 4 , SEEK_SET );

    while ( b <= 3 )
    {
        //Get Y resolution
        yres[b] = fgetc(file);
        //Convert ASCII to a usable array
        yres[b] -= 48;
        cout << yres[b] << endl; //Testing purposes
        ++b;
    }

    //Convert from array to usable integer
    sy = ((yres[0]*1000)+(yres[1]*100)+(yres[2]*10)+yres[3]);
    //Output just to make sure
    cout << sy << endl;

    //Close the file
    fclose( file );

    return true;//added by GuestG to stop compiler complaining about return value
}

int main()
{
    load_values();

    return 0;
}


My output on MSVC and GCC compilers, with a text file with the exact data you gave us
600
0
5
0
0
500


Is your data file correct?? You gave us this data all on one line 06000500
Last edited on
06000500

As far as I see.

Could it be because I'm using Code::Blocks or could because I'm using SDL in the project?
I think if your code is the same as mine above, then the file isn't quite what you think it is - maybe they are some other characters in there.

EDIT
Come to think of it - if you subtract 48 from some_value and get -38, as you did in your last test when you changed from 5 to 4 - then the original value was 10
which is the code for Line Feed
So the datafile format is not quite what we think.
Last edited on
http://www.mediafire.com/?zyuj3zdyn5y

I have no idea anymore.

Perhaps you can see what's going on?

PS: I tried it again with another file called test.txt with only "06000500" in it.

The output?

-54439
-49
-49
-49
-49
-54439

Hence, fgetc returned -1 each time. >_<
Don't worry - this is easy enough.
(And it is as I thought - the numbers are on two different lines).

Can you use C++ or do you want to do it in C
C++ all the way, bro.
Bump.
It's very late here- going to bed soon.
I take it that you only want to load the first 3 lines (the 600, 550 and 0).
What about the last 3 lines
y/2+20
(x+y)/5
x/2+10

what's going to happen to them??
The third line is going to be assigned to the finish counter temporary variable, while the last three lines are going to be respectively assigned to the R, G, and B temporary color algorithm storages.
[code=OctavariumXI ]while the last three lines are going to be respectively assigned to the R, G, and B temporary color algorithm storages.[/code]

You can load those last three lines as strings - but how were you expecting to actually use them
as algorithms?? Have you got some type of expression evaluator
I can make one :P
-facepalm-

I just found the problem.

This WHOLE time I thought it was reading from the /bin/ version of the file.

Turns out, it was reading from the /CPhys/ version of the file!

I've got it all working now, if I have any more problems, I'll just post :P
http://www.mediafire.com/?d4ommwwqny0

I've got a final little bug until it's completely done;

It can't convert *char to int, and I need halp.

Can you fix it for me, gulkan? :o
Pages: 12