hi all
Code:
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#define BUF 250
char read_file(char *file_name);
char write_file(char *file_name);
void help(char *string);
main(int argc,char **argv)
{
int c;
char *file_name;
if(argc<2||argc>3)
{
help(argv[0]);
}
while((c=getopt(argc,argv,"r:w:h"))!=-1)
{
switch(c)
{
case 'r':
file_name=strdup(optarg);
read_file(file_name);/* inside there is a while loop for reading the file this function only called onec"*/
break;
case 'w':
file_name=strdup(optarg);
write_file(file_name);/* inside there is a while loop for writing in the file this function only called onec*/
case 'h':
help(argv[0]);
break;
}
}
}
char read_file(char *file_name)
{
FILE *fp;
fp=fopen(file_name,"r");
if(fp==NULL)
{
fprintf(stderr,"Couldn't open File %s\n",file_name);
exit(1);
}
char buffer[BUF];
printf("\n========================Reading======================\n");
while((fgets(buffer,250,fp))!=NULL)
{
printf("%s",buffer);
}
fclose(fp);
}
char write_file(char *file_name)
{
FILE *fp;
fp=fopen(file_name,"w");
if(fp==NULL)
{
fprintf(stderr,"Couldn't open File %s\n",file_name);
exit(1);
}
char buffer[BUF];
printf("\n========================writing (:wq!) for exit ======================\n");
while((fgets(buffer,BUF,stdin))!=NULL)
{
if(strstr(buffer,":wq!"))
{
exit(0);
}
fputs(buffer,fp);
}
fclose(fp);
}
void help(char *string)
{
fprintf(stderr,"\nfor reading %s -r filename\nfor writing %s -w file_name\nfor help %s -h\n(only one operation at one)\n",string,string,string);
exit(0);
}
as you are generous so i am here only for taking advices from you
thanks in advance