line editor in c

unable to get output through this code

Page 1 of 1

1 Replies - 3564 Views - Last Post: 04 November 2009 - 11:16 AM Rate Topic: -----

#1 pcnitjsr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-November 09

line editor in c

Post icon  Posted 04 November 2009 - 10:50 AM

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main(int argc,char **argv)
{
clrscr();

if(strcmp("-c",argv[2])==0)			   //create file
{
FILE *fp=fopen(argv[1],"a");
if(fp!=NULL)
printf("\nFile Created Successfully");
else
printf("ERROR");
}

if(strcmp("-del",argv[2])==0)		  //delete file
{
int r = remove(argv[1]);
if(r==0)
printf("\nFile removed successfully");
else
printf("\nERROR in remove");
}

if(strcmp("-d",argv[2])==0)   					   //delete a line
{
int no=-atoi(argv[3]);char line[80];
int i=1;
FILE *fp=fopen(argv[1],"r");
FILE *ft=fopen("temp.txt","w");
for(;i<no;i++)
{
fgets(line,80,fp);
fputs(line,ft);
}
fgets(line,80,fp);
while(!feof(fp))
{
fgets(line,80,fp);
fputs(line,ft);
}
remove(argv[1]);
rename("temp.txt",argv[1]);
}

if(strcmp("-p",argv[2])==0)		  // print all
 {


	if(strcmp("-all",argv[3])==0)
	  {
	   FILE *fp=fopen(argv[1],"r");int ch=0;
	   while(ch!=EOF)
		{
		 ch = getc(fp);
		 putchar(ch);

	  }
	   }
	else						// print n specfic line
	{
	 FILE *fp=fopen(argv[1],"r");
	 int i=0;
	 int n= -atoi(argv[3]);
	// printf("%d",n);
	 for(int count=0;count<n-1;)
	 {
	 i=getc(fp);
	 if(i=='\n')
	 count++;
	 }
	 i=getc(fp);
	 while(i!='\n')
	 {
putchar(i);
i=getc(fp);
	   }
	   }
}

if(strcmp("-u",argv[2])==0)	   //update a file pointer
{
FILE *fp,*ft;
int ch;char line[80];

int a= -atoi(argv[3]);
int b= -atoi(argv[4]);
int c= -atoi(argv[5]);

fp = fopen("temp.txt","w");
ft = fopen(argv[1],"r");

int count = 1;

while(count<a)
	{
	 fgets(line,80,ft);
	 fputs(line,fp);
	 count++;
	 }
	 count =1;

	 while(count<b)
	 {
	 ch= getc(ft);
	 putc(ch,fp);
	 count++;
	 }
	 int l=strlen(argv[6]);
	 fprintf(fp,"%s",argv[6]);
	 int k=c-l;
	 while(k>0)
	 {
	 putc(' ',fp);
	 k--;
	 }
	 fseek(ft,c-b,SEEK_CUR);
	 char ch1=getc(ft);
	 while(ch1!=EOF)
	 {
	 ch1=getc(ft);
	 putc(ch1,fp);
	 }
	 remove(argv[1]);

	   rename("temp.txt",argv[1]);
fclose(fp);
	 }

if(strcmp("-i",argv[2])==0)	   //insert in file
{
FILE *fp,*ft;
int ch;char line[80];

int a= -atoi(argv[3]);
int b= -atoi(argv[4]);

fp = fopen("temp.txt","w");
ft = fopen(argv[1],"r");

int count = 1;

while(count<a)
	{
	 fgets(line,80,ft);
	 fputs(line,fp);
	 count++;
	 }
	 count =1;

	 while(count<b)
	 {
	 ch= getc(ft);
	 putc(ch,fp);
	 count++;
	 }

	 fprintf(fp,"%s",argv[5]);

	 while(!feof(ft))
	 {
	 fgets(line,80,ft);
	 fputs(line,fp);
	 }

	   remove(argv[1]);

	   rename("temp.txt",argv[1]);
fclose(fp);
}

if(strcmp("-a",argv[2])==0)
{
if(strcmp("-e",argv[3])==0)		//append at end
{
FILE *fp=fopen(argv[1],"a");
printf("%s",argv[4]);
fprintf(fp,"%s",argv[4]);
fclose(fp);
}
if(strcmp("-b",argv[3])==0)		// append at beginning
{
FILE *fp,*ft;int ch=0;
fp=fopen(argv[1],"r");
ft=fopen("temp.txt","w");
fprintf(ft,"%s",argv[4]);
while(ch!=EOF)
{
ch=getc(fp);
putc(ch,ft);
}
remove(argv[1]);
rename("temp.txt",argv[1]);
}

}
getch();
}



Is This A Good Question/Topic? 0
  • +

Replies To: line editor in c

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,657
  • Joined: 23-August 08

Re: line editor in c

Posted 04 November 2009 - 11:16 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1