|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//13.3 - 把原来的文件压缩成1/3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
char *stract(char *ch,char *gh);
int main(int argc, char *argv[])
{
FILE *in=NULL ;//声明两个指向FILE的指针
FILE* out = NULL;
int ch;
char name[LEN];//储存输出文件名
int count = 0;
//检查命令行参数
if (argc < 2)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
//设置输入
if ((fopen_s(&in,argv[1], "r")) == NULL)
{
fprintf(stderr, "I count't open the file \"%s\"\n", argv[1]);
exit(EXIT_FAILURE);
}
//设置输出
strncpy_s(name, argv[1], LEN - 5,LEN);
stract(name, ".red");
if ((fopen_s(&out,name, "w")) == NULL)
{
fprintf(stderr, "Can't create output file.\n");
exit(EXIT_FAILURE);
}
//拷贝数据
while ((ch = getc(in)) != EOF)
if (count++ % 3 == 0)
putc(ch, out);
//收尾工作
if (fclose(in) != 0 || fclose(out) != 0)
fprintf(stderr, "Error in closeing files\n");
return 0;
}
char *stract(char* ch, char* gh)
{
int i, j;
for (i = 0; ch[i] != '/0'; i++)
;
for (j = 0; gh[j] != '/0'; j++)
{
ch[i] = gh[j];
i++;
}
return ch;
}
弹出窗口中断程序:
Debug Assertion Feiled!
Program:C:\Users\XXX\13.3,exe
File:f:\dd:vctools\crt\crtw32\stdio\fopen.c
Lile:159
Expression:(pfile !=NULL)
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
求指点!!!
问题太多,不过代码给你弄好了
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LEN 40
- char *stract(char *ch, char *gh);
- int main(int argc, char *argv[])
- {
- FILE *in = NULL;//声明两个指向FILE的指针
- FILE *out = NULL;
- int ch;
- char name[LEN];//储存输出文件名
- int count = 0;
- //检查命令行参数
- if (argc < 2)
- {
- fprintf(stderr, "Usage: %s filename\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- //设置输入
- if ((fopen_s(&in, argv[1], "r")) != 0)
- {
- fprintf(stderr, "I count't open the file "%s"\n", argv[1]);
- exit(EXIT_FAILURE);
- }
- //设置输出
- strncpy_s(name, LEN,argv[1], LEN-5);
- stract(name, ".red");
- if ((fopen_s(&out, name, "w")) != 0)
- {
- fprintf(stderr, "Can't create output file.\n");
- exit(EXIT_FAILURE);
- }
- //拷贝数据
- while ((ch = getc(in)) != EOF)
- if (count++ % 3 == 0)
- putc(ch, out);
- //收尾工作
- if (fclose(in) != 0 || fclose(out) != 0)
- fprintf(stderr, "Error in closeing files\n");
- return 0;
- }
- char *stract(char* ch, char* gh)
- {
- int i, j;
- for (i = 0; ch[i] != '\0'; i++)
- ;
- for (j = 0; gh[j] != '\0'; j++)
- {
- ch[i]=gh[j];
- i++;
- }
- ch[i] = '\0';
- return ch;
- }
复制代码
|
|