Tuesday 15 January 2013


OOP 344 Week 2

Environment Variable Content Search Program

 V2



#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char* argv[], char* env[]) {
int i;
int pos;
int check=0;
char content[50];

if (argc != 2) {
cout << "Only input one parameter after program name" << endl;
} else {
for (i=0; env[i]!=0 && check==0; i++) {
pos = strcspn(env[i], "=");
if (strncmp(argv[1], env[i], pos) == 0) {
check = 1;
strcpy(content, env[i]+pos+1);
}
}
if (check==1)
cout << argv[1] << " = " << content << endl;
else
cout << argv[1] << " not found" << endl;
        }
    return 0;
}


Better now.

No comments:

Post a Comment