Home Forums C Programming Regarding replacing line of code?? Reply To: Regarding replacing line of code??

#3385
Priyansh Agrawal
Participant

Sorry, your code looks very strange to me. Where do you want to place the output-file ? You opened an input-file for read-only. There you can’t make any changes.

Perhaps this should help you:


#include

int main(int argc,char **argv)
{
const int max=1024;
int r=0,i;
FILE *in,*out;
char buff[max+3];

if (argc!=3)
{
r=1;
printf("usage: lower n");
}
else
{
if ((in=fopen(argv[1],"rb"))==NULL)
{
r=2;
printf("cannot open input-file!n");
}
else
{
// Output file will be overwritten when exists !!!
if ((out=fopen(argv[2],"wb"))==NULL)
{
r=3;
printf("cannot open output-file!n");
}
else
{
while (!feof(in))
{
buff[0]=0;
fgets(buff,max,in);

for (i=0;i<max && buff!=0 && buff!='r' && buff!='n' && (buff==' ' || buff=='t');i++);

if (buff=='#')
{
for (i=i+1;i<max && buff!=0 && buff!='r' && buff!='n';i++)
{
buff=tolower(buff);
}
}

fputs(buff,out);
}

fclose(out);
}

fclose(in);
}
}

return r;
}