Basic and Advance C Question:
Download Job Interview Questions and Answers PDF
How can I open files mentioned on the command line, and parse option flags?
Answers:
Answer #1Here is a skeleton which implements a traditional Unix-style argv parse, handling option flags beginning with -, and optional filenames. (The two flags accepted by this example are -a and -b; -b takes an argument.)
#include <stdio.h>
#include <string.h>
#include <errno.h>
main(int argc, char *argv[])
{
int argi;
int aflag = 0;
char *bval = NULL;
for(argi = 1; argi < argc && argv[argi][0] == '-'; argi++) {
char *p;
for(p = &argv[argi][1]; *p != '
#include <stdio.h>
#include <string.h>
#include <errno.h>
main(int argc, char *argv[])
{
int argi;
int aflag = 0;
char *bval = NULL;
for(argi = 1; argi < argc && argv[argi][0] == '-'; argi++) {
char *p;
for(p = &argv[argi][1]; *p != '