|
上篇寫到程序清單11.27的相關(guān)VS2010的相關(guān)操作,13-2的程序相對要復(fù)雜一點,故此說明。程序如下
// reducto.c -- reduces your files bytwo-thirds!
#include
#include
#include
#define LEN 40
int main(int argc, char *argv[])
{
FILE *in, *out;
int ch;
char name[LEN];
int count = 0;
//check the command arguments
if(argc < 2)
{
fprintf(stderr,"Usage: %s filename\n",argv[0]);
getchar();
exit(1);
}
// finish the input action
if((in = fopen( argv[1], "r")) == NULL)
{
fprintf(stderr,"I couldn't open the file\"%s\"\n",argv[1]);
getchar();
exit(2);
}
// finish the output action
strcpy(name,argv[1]);//把文件名復(fù)制到數(shù)組中
strcat(name,".txt");//在文件名后添加.txt
if((out = fopen(name,"w"))== NULL)//檢查文件是否為空
{//打開文件以供寫入
fprintf(stderr,"Can't create output file.\n");
getchar();
exit(3);
}
// copy data
while((ch = getc(in))!= EOF)
if(count++ % 3 == 0)
putc(ch,out);
// clear up
if(fclose(in)!= 0 ||fclose(out)!= 0)
fprintf(stderr,"Error in cliosing file.\n",argv[1]);
return 0;
}
與上篇不同的是在 command lines 輸入a ,還要設(shè)置路徑(workingdirectory)為桌面,以便大家方便地觀看結(jié)果,并原書中的“.red”替換成“.txt”,增加可讀性。也在exit();前添加上getchar();以便更加直觀地觀看error.
?還需在桌面建立一個a.txt,在文本中寫入 So even Eddy cameoven ready. 在通過屬性更改去掉.txt.
編譯完成,執(zhí)行。桌面上就會出現(xiàn)a.txt的新文本文件,打開文件中,就會出現(xiàn) Sendmoney? 。如果忘記新建桌面設(shè)置這樣的a文件,那木
?就會彈出對話框, I couldn't open the file"a"。
|
|