//copy.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char * argv[]){
FILE *in, *out;
int c;
if(argc!=3){
printf("U forgot to enter a filename!");
exit(0);
}
if((in=fopen(argv[1],"r"))==NULL){
printf("cannot open infile\n");
exit(0);
}
if((out=fopen(argv[2],"w"))==NULL){
printf("cannot open outfile\n");
exit(0);
}
while( ( c = fgetc(in) ) != EOF )
{
fputc(c, out);
}
fclose(in);
fclose(out);
}
-------------------------------------------------------------------------------------------------------------------------
cc copy.c -o copy
copy fileA fileB
and you get a new fileB same as fileA.
PS: In linux, if you copy a executable file using this program,to run the new file u need to give the new file execute permission(chmod)......
Monday, 21 January 2013
CMD line calculation PGM
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int valid(char s[]){
int valid = 0;
int cnt = 0;
int i;
for(i = 0;i < strlen(s);i++){
if(s[i] >= '0' && s[i] <= '9'){
valid = 1;
}
else if(s[i] == '.')
cnt++;
else{
valid = 0;
break;
}
}
if(cnt > 1)
valid = 0;
if(valid == 0){
printf("Plz enter valid numbers!\n");
exit(1);
}
return valid;
}
int main(int argc,char * argv[]){
double num1;
double num2;
if(argc!=3){
printf("Plz enter two numbers!!\n");
exit(0);
}
if((valid(argv[1])) && (valid(argv[2]))){
sscanf(argv[1],"%lf",&num1);
sscanf(argv[2],"%lf",&num2);
}
printf("answer: %lf \n",num1+num2);
/* num1-num2
num1*num2
To get num1/num2 need to validate that the num2 cant not be zero.
*/
return 2;
}
#include <string.h>
#include <stdlib.h>
int valid(char s[]){
int valid = 0;
int cnt = 0;
int i;
for(i = 0;i < strlen(s);i++){
if(s[i] >= '0' && s[i] <= '9'){
valid = 1;
}
else if(s[i] == '.')
cnt++;
else{
valid = 0;
break;
}
}
if(cnt > 1)
valid = 0;
if(valid == 0){
printf("Plz enter valid numbers!\n");
exit(1);
}
return valid;
}
int main(int argc,char * argv[]){
double num1;
double num2;
if(argc!=3){
printf("Plz enter two numbers!!\n");
exit(0);
}
if((valid(argv[1])) && (valid(argv[2]))){
sscanf(argv[1],"%lf",&num1);
sscanf(argv[2],"%lf",&num2);
}
printf("answer: %lf \n",num1+num2);
/* num1-num2
num1*num2
To get num1/num2 need to validate that the num2 cant not be zero.
*/
return 2;
}
Monday, 14 January 2013
Subscribe to:
Posts (Atom)