读书人

程序修改解决方法

发布时间: 2012-02-06 15:52:44 作者: rapoo

程序修改
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int encode_character(int ch,int val);
int decode_character(int ch,int val);
int main(int argc,char *argv[])
{
FILE *fh;
int rv=1;
int ch=0;
unsigned int ctr=0;
int val=5;
char buffer[257];
if(argc!=3)
{
printf("\nError:Wrong number of parameters...");
printf("\n\nUsage:\n %s filename action",argv[0]);
printf("\n\n Where:");
printf("\n filename-name of file to code or decode");
printf("\n action=D for decode or C for encode\n\n");
rv=-1;
}
else
if((argv[2][0]=='D') || (argv[2][0]=='d'))
{
fh=fopen(argv[1],"r");
if(fh<=0)
{
printf("\n\nError opening file...");
rv=-2;
}
else
{
ch=getc(fh);
while(!feof(fh))
{
ch=decode_character(ch,val);
putchar(ch);
ch=getc(fh);
}
fclose(fh);
printf("\n\nFile decoded to screen.\n");
}
}
else
{
fh=fopen(argv[1],"w");
if(fh<=0)
{
printf("\n\nError creating file...");
rv=-3;
}
else
{
printf("\n\nEnter text to be coded.");
printf("Enter a blank line to end.\n\n");
while(getc(buffer)!=NULL)
{
if(buffer[0]==0)
break;
for(ctr=0;ctr<strlen(buffer);ctr++)
{
ch=encode_character(buffer[ctr],val);
ch=fputc(ch,fh);
}
}
printf("\n\nFile encoded to file.\n");
fclose(fh);
}
}
return rv;
}
int encode_character(int ch,int val)
{
ch=ch+val;
return ch;
}
int decode_character(int ch,int val)
{
ch=ch-val;
return ch;
}
请问以上程序错在那里,该如何修改,在VC下编译不能通过

[解决办法]
这样?
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int encode_character(int ch,int val);
int decode_character(int ch,int val);
int main(int argc,char *argv[])
{
FILE *fh;
int rv=1;
int ch=0;
unsigned int ctr=0;
int val=5;
char buffer[257];
if(argc!=3)
{
printf("\nError:Wrong number of parameters...");
printf("\n\nUsage:\n %s filename action",argv[0]);
printf("\n\n Where:");
printf("\n filename-name of file to code or decode");
printf("\n action=D for decode or C for encode\n\n");
rv=-1;
}
else
if((argv[2][0]=='D') || (argv[2][0]=='d'))
{
fh=fopen(argv[1],"r");
if(fh <=0)
{
printf("\n\nError opening file...");
rv=-2;
}
else
{
ch=getc(fh);
while(!feof(fh))
{
ch=decode_character(ch,val);
putchar(ch);
ch=getc(fh);
}
fclose(fh);
printf("\n\nFile decoded to screen.\n");
}
}
else
{
fh=fopen(argv[1],"w");
if(fh <=0)
{
printf("\n\nError creating file...");
rv=-3;
}
else
{
printf("\n\nEnter text to be coded.");
printf("Enter a blank line to end.\n\n");


while(getc((FILE*)(buffer))!=NULL)
{
if(buffer[0]==0)
break;
for(ctr=0;ctr <strlen(buffer);ctr++)
{
ch=encode_character(buffer[ctr],val);
ch=fputc(ch,fh);
}
}
printf("\n\nFile encoded to file.\n");
fclose(fh);
}
}
return rv;
}
int encode_character(int ch,int val)
{
ch=ch+val;
return ch;
}
int decode_character(int ch,int val)
{
ch=ch-val;
return ch;
}





[解决办法]

C/C++ code
#include   "stdio.h" #include   "stdlib.h" #include   "string.h" int   encode_character(int   ch,int   val); int   decode_character(int   ch,int   val); int   main(int   argc,char   *argv[]) { FILE   *fh; int   rv=1; int   ch=0; unsigned   int   ctr=0; int   val=5; char   buffer[257]; if(argc!=3) { printf("\nError:Wrong   number   of   parameters..."); printf("\n\nUsage:\n   %s   filename   action",argv[0]); printf("\n\n   Where:"); printf("\n           filename-name   of   file   to   code   or   decode"); printf("\n           action=D   for   decode   or   C   for   encode\n\n"); rv=-1; } else if((argv[2][0]=='D')   ¦ ¦   (argv[2][0]=='d')) { fh=fopen(argv[1],"r"); if(fh <=0) { printf("\n\nError   opening   file..."); rv=-2; } else { ch=getc(fh); while(!feof(fh)) { ch=decode_character(ch,val); putchar(ch); ch=getc(fh); } fclose(fh); printf("\n\nFile   decoded   to   screen.\n"); } } else { fh=fopen(argv[1],"w"); if(fh <=0) { printf("\n\nError   creating   file..."); rv=-3; } else { printf("\n\nEnter   text   to   be   coded."); printf("Enter   a   blank   line   to   end.\n\n"); while(getc(buffer)!=NULL) //getc的原型int getc(FILE *stream); { if(buffer[0]==0) break; for(ctr=0;ctr <strlen(buffer);ctr++) { ch=encode_character(buffer[ctr],val); ch=fputc(ch,fh); } } printf("\n\nFile   encoded   to   file.\n"); fclose(fh); } } return   rv; } int   encode_character(int   ch,int   val) { ch=ch+val; return   ch; } int   decode_character(int   ch,int   val) { ch=ch-val; return   ch; } 

读书人网 >C语言

热点推荐