cmd line parameters not working?

Hello i have made a level editor in c and
want to add command line parameters so you can
type:
editor mapname
and it will load that map

i have attempted to pass argv[1] to my load function
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
void load(char *x)//Finaly working (to my knowlage)
{//start of load function
 auto unsigned short i;
 auto unsigned short j;
 auto short k = 0;
 static signed int ch;
 Clear_Screen();
 printf("loading...\n");
 FILE *fp;
 fp=fopen(x, "r");//opens file
 if(fp == NULL){//start of if(fp == NULL)
      perror("\nFailed to Loading Map :(\nError \a");getchar();
 }//end of if(fp == NULL)
      for (i = 0;i < MAXX;i++){//start of for (i =0;i < MAXX;i++)
          for(j = 0;j < MAXY;j++){//start of for(j = 0;j < MAXY;j++)
              fscanf(fp, "%d", &ch);
              Set_Map(i, j, ch);//Testing...
              printf("%c", ch);
              }//end of for (i =0;i < MAXX;i++)
              }//end of for(j = 0;j < MAXY;j++)
              goto exit;
exit:
     fclose(fp);
     printf("Finished");
     getchar();
     Set_walls();
     return;
}//end of load function

but this has not worked(compiled fine just did not load the map)
also heres my int main():
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
int main(int argc, char *argv[])
{
    if (First_main_func_call == 1){
        Set_k_default();
        }
    First_main_func_call = 0;
    register short keystroke;
    Set_ground();
    Set_walls();
    while(1)
    {
    if (y <= 0)
    {y++;}
    if (y >= 59)
    {y--;}
    if (x <= 0)
    {x++;}
    if (x >= 24)
    {x--;}
    Set_Map(x, y, val);
    Clear_Screen();
    printf("TT Level editor 0.77 ALPHA(Linux port) \nEnter o key to save(l to load) Ctrl Z to exit\n");
    printMap();
    printf("xcord: %d\nycord: %d\nTile ID: %c", x, y, Set_Map(x, y, val));
    printf("\nPress ESC for Menu\n");
    keystroke = getchar();//getch wont work on linux
    if( keystroke == 0 || keystroke == 224)
             keystroke = getchar() + 255;//getch wont work on linux
    if (keystroke == w)
    {
     x--;
    }
    else if (keystroke == s)
    {
     x++;
    }
    else if (keystroke == a)
    {
     y--;
    }
    else if (keystroke == d)
    {
     y++;
    }
    if (keystroke == key_1)
    {val = k1;}
    if (keystroke == key_2)
    {val = k2;}
    if (keystroke == key_3)
    {val = k3;}
    if (keystroke == key_4)
    {val = k4;}
    if (keystroke == key_5)
    {val = k5;}
    if (keystroke == key_6)
    {val = k6;}
    if (keystroke == key_7)
    {val = k7;}
    if (keystroke == key_8)
    {val = k8;}
    if (keystroke == key_9)
    {val = k9;}
    if (keystroke == key_0)
    {val = k0;}
    if (keystroke == SPACE){val = 32;}
    if (keystroke == ESC)//esc key
    {menu();}
    if (keystroke == o)
    {save();}
    if (keystroke == l)
    {load(argv[1]);}//calling load function
    }
  return 0;
}

and headers if you want to know where the functions like SetMap() came from:
#include <stdio.h>
#include <stdlib.h>
#include "Text Engine.h"
#include "Functions.h"
#include "macros.h"

if anyone knows why this isnt working please reply!!
also i am running linux(xubuntu) with gcc as my compiler
thanks in advance!
Last edited on
if (keystroke == l)
This compares keystroke to a variable called l. If you mean to look for the letter l then it should be if (keystroke == 'l')

The same applies at line 68 where you compare to o.
A couple of other comments on your code:
load line 21: avoid the use of goto.

main line 3: It appears that you're calling main recursively. This is explicitly prohibited by the standard.

main line 7: Using the register keyword is unnecessary with today's optimizing compilers. If you're concerned about efficiency, you should use a switch statement for lines 27-70.
in reply to dhayden
i made macro in another header file called l
in reply to AbstractionAnon
thanks for the advice on the register keyword
but i have never understood why pepole dont use goto
i agree you should not use it often but for example
if you need to quickly escape from 4 layers of while and for loops
i think goto is a good idea
and also in simple functions i dont see why you should not use it
as long as it stays simple
why pepole dont use goto
Read Dijkstra's paper "Goto considered harmful". It is old, but still reasonable. For modern compilers main drawback is that goto can seriously hamper optimizations. Goto can be useful and descriptive as qoick jump in case of error (it should almost never be a part of normal execution): goto error; where error label is placed after last proper return.

if you need to quickly escape from 4 layers of while and for loops
Having 4 nested loops is already bad idea ( O(N4) complexity? ). Usually this is solved by refactoring your program and in most cases these loops are moved into separate function which you just return from.
> why pepole dont use goto

>> Read Dijkstra's paper "Goto considered harmful". It is old, but still reasonable.
>> For modern compilers main drawback is that goto can seriously hamper optimizations.

http://www.cplusplus.com/forum/general/93381/#msg502210
I made macro in another header file called l

Something contained in a separate file should have a more descriptive name. Also, macros are generally written in all capitals as a hint to the reader that they are macros.

I wish they would add a feature to the language that would let you put one or more optional keywords after the break command. The keywords would indicate what you want to break out of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (...) {
    while (...) {
        switch(x) {
        case 1:
               break;   // breaks out of switch
        case 2:
               break switch while;  // breaks out of switch and while loop
        case 3:
               break for;            // syntax error. First breakable statement isn't a for loop
        case 4:
               break; switch while for // breaks out of all three
        }
    }
}

Topic archived. No new replies allowed.