passing parameters from the commandline

I have written the following code and need to modify it so that I can type in the file name instead of it being hardcoded. I also need to check to make sure that I am returning the correct parameters after each is initialized. Here's what I have:

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
#include stdlib.h
#include string.h
#include time.h
#include parameter.h



lParameter;
bParameter;

//array  for buffer
int param(char*fileInputBuffer, int*bufferPointer){

//pointer to buffer length
int bufferPointer = *buffPointer;

//opens file
file*param=fopen("parametersfile.txt","r");
char*temp;

if (NULL!= param){

bufferPointer=read_thisLine(&fileInputBuffer, bufferPointer, param);
init (lParameter);
storeParam(fileInputBuffer, ":");
temp =storeParam (Null, "\n");

bufferPointer=read_thisLine(&fileInputBuffer, bufferPointer, param);
init (bParameter);
storeParam(fileInputBuffer, ":");
temp =storeParam (Null, "\n");

*buffPointer=bufferPointer;

fclose(parametersfile);
}
else{
printf("error\n");
return FAil;
}
return Pass;
}


I'm thinking that a try catch block may work for error handling. So it would look like this but I'm not quite sure.I'm not sure where to start in regards to the commandline.

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
#include stdlib.h
#include string.h
#include time.h
#include parameter.h



lParameter;
bParameter;

//array  for buffer
int param(char*fileInputBuffer, int*bufferPointer){

//pointer to buffer length
int bufferPointer = *buffPointer;

//opens file
file*param=fopen("parametersfile.txt","r");
char*temp;

if (NULL!= param){

tyr{
bufferPointer=read_thisLine(&fileInputBuffer, bufferPointer, param);
init (lParameter);
storeParam(fileInputBuffer, ":");
temp =storeParam (Null, "\n");
}
catch{}

try{
bufferPointer=read_thisLine(&fileInputBuffer, bufferPointer, param);
init (bParameter);
storeParam(fileInputBuffer, ":");
temp =storeParam (Null, "\n");
}
catch}
*buffPointer=bufferPointer;

fclose(parametersfile);
}
else{
printf("error\n");
return FAil;
}
return Pass;
}
Do those includes even work without quotes?
OK, now then: What are you trying to do, where is your main and what are the side functions? The current code you have should not even compile because there is no main. Why is your filename even hardcoded? (And heck, that's some horrendous indenting.)

EDIT: I suppose I should kind of answer your question in the topic line.
1
2
3
4
5
int main (int argc, char** argv)
{
    // Your code here
    return 0;
}

The commandline arguments you pass into your program are stored in the array of c-style strings argv (or any name). The number of arguments is argc (or again any other name). To call your program with arguments, in your commandline you type
C:\Yourdirectoryhere\Yourprogramhere.exe arg1 arg2 arg3 andSoOn

whereupon the args will each be stored into one slot of the argv array (separated by spaces and outlined with double quotes of course). To access them simply access argv as though it were an array you had declared (be sure to check argc to make sure you know how many arguments there were).
Last edited on
The quotes were in the code but I mistyped. The filename was hardcoded just to get it to work because I did not quite know how do it otherwise. So I'm trying to rewrite this section so it's not not hardcoded.
Answer is simple.
#include <string> and then create a string for your filename. Input the string somewhere in your program. (This is why you need to use iostream instead of stdio. I have no idea how stdio responds to the string class. But just the fact that string class is so much better than char[] justifies the use of iostream and fstream over cstdio.) When you create your fstream open to the string as the filepath.
Speaking of which you are using legacy file io aren't you? That would explain why none of it makes sense to me. Try using streams instead. More flexible and modern.
See this page http://www.cplusplus.com/doc/tutorial/files/ for the use of the file stream classes. It works just as well if not better, I find. In addition you don't need to catch any exceptions, just check the stream state to make sure the file opened properly.
Now then, how about your main?
Last edited on
Topic archived. No new replies allowed.