Accepting more than one argument

I was really hoping someone could help me out here, as I am totally stuck. How do I accept an -f flag for file versus defining test.txt, with what I have already written here?

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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main (int argc, char **argv)
{
		FILE *fp;
char string[1000];

  char *wvalue = NULL;
  int index;
  int w;

  opterr = 0;

  while ((w = getopt(argc, argv, "drw:")) != -1)
    switch (w)
      {
      case 'w':
		
	wvalue = optarg;
	fp = fopen("test.txt", "w");
	if (fp == NULL) {
	printf("Couldn't open test.txt for writing.\n");
      			}
		
			fprintf(fp,wvalue, string);

		fclose(fp);

		return  0;
	case 'r':
	fp =fopen("test.txt","r");
    		if (!fp)
        		return 1;

    		while (fgets(string,1000, fp)!=NULL)
        		printf("%s",string);

		fclose(fp);
    		return 0;


	case 'd':
	if (remove("test.txt") == -1)
	perror("Error deleting file");
	return 0;

}
return 0;
}
Last edited on
closed account (E0p9LyTq)
How to parse command line parameters
http://www.cplusplus.com/articles/DEN36Up4/
Since there are no comments and I can't figure out what all your statements do try this example.

http://code.runnable.com/UqvNM9NFP0gtAAAM/command-line-arguments-with-options-in-c%2B%2B

I'm sorry for the lack of comments. I have been busily working on this, and have slacked where it comes to comments

I can do what I am trying to do using argv[], but am trying to do the same thing using getopts, as I have with the -w option and I cannot figure out how to do it with more than one argument.

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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main (int argc, char **argv)
{
		FILE *fp;
char string[1000];

  char *wvalue = NULL;
  int index;
  int w;

  opterr = 0;

	while ((w = getopt(argc, argv, "drw:")) != -1)
    switch (w)
      {
      case 'w':
		
	wvalue = optarg;
	fp = fopen("test.txt", "w");
	if (fp == NULL) {
	printf("Couldn't open test.txt for writing.\n");
      			}
		
			fprintf(fp,wvalue, string);

		fclose(fp);

		return  0;
	case 'r':
	fp =fopen(argv[2],"r");
    		if (!fp)
        		return 1;

    		while (fgets(string,1000, fp)!=NULL)
        		printf("%s",string);

		fclose(fp);
    		return 0;


	case 'd':


	if (remove(argv[2]) == -1)
	perror("Error deleting file");
	return 0;

}
return 0;
}
Your option handling is a little off -- and I think it is because you are struggling to understand the getopt() POSIX function.

The list of options is given by the third argument: each character represents an option:

    "abc"           -a, -b, and -c.

If an option has an associated string (or any kind of value), put a colon after it:

    "a:b:c:"        -a a-value, -b b-value, and -c c-value.

It appears that all your options expect an argument filename:

    -d filename_to_delete
    -r filename_to_read
    -w filename_to_write
    -f filename_to_test

Do that by being specific:

getopt(argc,argv,"d:r:w:f:")


As far a structuring your code, it is typically a good idea to collect all your options before acting on them.

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
int main( int argc, char** argv )
{
  // Option values
  const char* d_filename = NULL;
  const char* r_filename = NULL;
  const char* w_filename = NULL;
  const char* f_filename = NULL;

  // Collect options
  int w;
  while ((w = getopt( argc, argv, "d:f:r:w:" )) != -1)
    switch (w)
    {
      case 'd': d_filename = optarg; break;
      case 'f': f_filename = optarg; break;
      case 'r': r_filename = optarg; break;
      case 'w': w_filename = optarg; break;
    }

  // Now check to see that it all makes sense. 
  // For example, if you only want ONE option to be possible at a time:
  switch ((int)!!d_filename + (int)!!f_filename + (int)!!r_filename + (int)!!w_filename)
  {
    case 0: r_filename = "test.txt"; break;  // here's a default if no options were specified
    case 1: break;  // only one option was specified
    default: fooey();  // more than one option was specified
  }

  // Now act on the data. 
  // The order and relationship here matters depending on your program's design.
  // This example assumes, again, that only ONE option is possible at a time.
  if      (d_filename) delete_file( d_filename );
  else if (f_filename) test_file( f_filename );
  etc

It's ugly, but straight-forward.

Hope this helps.
Last edited on
Have it working now. Thank you all for your help :-)

Now I better understand.
Topic archived. No new replies allowed.