Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc , char *argv[] )
{
FILE *fPtr;
char s;
if ( argc == 1 ){
printf("Usage: [Option] <Filename.txt>\n"
"-r Read the content of a textfile\n"
"-w Write to a Testfile\n");
}
else {
if ( strcmp( argv[1], "-r" ) == 0 ){
if ( ( fPtr = fopen(argv[2],"r")) != NULL ){
s = getc( fPtr );
fputc(s, stdout);
while( !feof( fPtr ) ){
s = getc( fPtr );
fputc(s, stdout);
}
}
else
printf("****ERROR*****\nCould not open %s\n", argv[2] );
}
else if ( strcmp(argv[1],"-w" ) == 0 ){
if ( ( fPtr = fopen(argv[2],"w")) != NULL ){
while ( !feof( stdin ) ){
s = fgetc(stdin);
fputc(s,fPtr);
}
}
else
printf("*****ERRORR*****\nCould not open %s\n", argv[2] );
}
else
printf("\nWrong option\n");
fclose(fPtr);
}
return 0;
}
First of all sorry if my code is a bit messy .Well at the movement i am not my self had alil to much gin .Okay so to the point ..I just did this programm in not my sence i cant find the problem in the code like when i use the option -w it should write to a file but it does not .Some times it does ?? and when i use the -r option to read it does print the content of the file but also prints Wrong option Why ???
Thanks alot guys
DaIn