Problem with file loader

Pages: 12
I can fix it!

What you need is to put a * in just behind the variable of type char *. It may seem counter-intuitive, however that's... what you need to do.

To understand why:
http://cplusplus.com/doc/tutorial/pointers/

-Albatross
Last edited on
Now it gets through the first few parts of my loop, and then crashes, with Windows 7 saying "CPhys has stopped working."

This is because of a loop somewhere in the loader header file initiating an infinite loop in reading a file, and it's in the last 3 loops, but I don't know whar D:

I'll spend some time looking.
Aight, I've isolated it to somewhere inside of;

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
bool load_values()
{
    int xres[4]; //X resolution temporary storage
    int yres[4]; //Y resolution temporary storage
    int fin[5]; //Finish counter temporary storage
    char 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;
        ++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;

    //Reset counter
    a = 0;
    //Set what character we're looking for
    fseek ( file , 8 , SEEK_SET );

    while ( a <= 4 )
    {
        //Get finish counter
        fin[a] = fgetc(file);
        //Convert ASCII to a usable array
        fin[a] -= 48;
        ++a;
    }

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


Or it's implementations in main.cpp
You could have used atoi(), first of all.

I can't compile these due to the fact that I'm on a different OS and don't have SDL installed, but why not use breakpoints at each loop to determine which is going infinite (none of those look like they would, by the way).

Why not use fscanf()?
http://cplusplus.com/reference/clibrary/cstdio/fscanf/

And why not use for loops?
for(initialize;condition;increment)

-Albatross
Last edited on
Whenever I use a for loop inside of a for loop, it facks up for me :O

And I find this way much faster and easier.

And I tried fscanf, but it didn't work (ofc, I later found it was due to other reasons) and I started using fgetc, and I never went back.

And what does atoi() do? :O
http://cplusplus.com/reference/clibrary/cstdlib/atoi/

As for not using fscanf... you do realize, of course, that this way you will also be fetching white spaces.

And as for using a nested for loop, try using a different variable as an increment (say j)?

-Albatross
Last edited on
No, I use 2 different variables, or 3 or 4, or however many I need for all of the nests.

But I mean, even if I do have A and B and C and D all individually assigned, it doesn't work for some reason D:

Or at least in my current compiler it doesnt'. In Dev-C++ it did :O
Visual Studio Express 2010 is to Dev-C++ as a mallard duck is to an aachenosaurus.

-Albatross

I know, now I use Code::Blocks, and I don't want to use up bandwidth on something as large as VSE (I only get 16 gigs of bandwidth a month, and they cut off my service at 12 gigs, and I live with 3 other people, 1 other person who uses the internet as much as I do (lots))

So yah.

What's wrong with mah program? D:
I'm not fully sure, to be honest. On the surface, it looks fine, however I can't compile it as I don't have SDL and really don't wish to download and install it (though maybe I should, even if I use SFML). If I find anything, I'll tell you, but aside from that I don't see anything inherently obvious.

-Albatross
Topic archived. No new replies allowed.
Pages: 12