鱼C论坛

 找回密码
 立即注册
查看: 4170|回复: 11

[已解决]关于fopen_( )的小问题

[复制链接]
发表于 2017-9-24 13:20:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)
求指点!!!
最佳答案
2017-9-24 15:15:28
问题太多,不过代码给你弄好了
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define LEN 40
  5. char *stract(char *ch, char *gh);

  6. int main(int argc, char *argv[])
  7. {
  8.         FILE *in = NULL;//声明两个指向FILE的指针
  9.         FILE *out = NULL;
  10.         int ch;
  11.         char name[LEN];//储存输出文件名
  12.         int count = 0;

  13.         //检查命令行参数
  14.         if (argc < 2)
  15.         {
  16.                 fprintf(stderr, "Usage: %s filename\n", argv[0]);
  17.                 exit(EXIT_FAILURE);
  18.         }
  19.         //设置输入
  20.         if ((fopen_s(&in, argv[1], "r")) != 0)
  21.         {
  22.                 fprintf(stderr, "I count't open the file "%s"\n", argv[1]);
  23.                 exit(EXIT_FAILURE);
  24.         }
  25.         //设置输出
  26.         strncpy_s(name, LEN,argv[1], LEN-5);
  27.         stract(name, ".red");
  28.         if ((fopen_s(&out, name, "w")) != 0)
  29.         {
  30.                 fprintf(stderr, "Can't create output file.\n");
  31.                 exit(EXIT_FAILURE);
  32.         }
  33.         //拷贝数据
  34.         while ((ch = getc(in)) != EOF)
  35.                 if (count++ % 3 == 0)
  36.                         putc(ch, out);
  37.         //收尾工作
  38.         if (fclose(in) != 0 || fclose(out) != 0)
  39.                 fprintf(stderr, "Error in closeing files\n");

  40.         return 0;
  41. }

  42. char *stract(char* ch, char* gh)
  43. {
  44.         int i, j;
  45.         for (i = 0; ch[i] != '\0'; i++)
  46.                 ;
  47.         for (j = 0; gh[j] != '\0'; j++)
  48.         {
  49.                 ch[i]=gh[j];
  50.                 i++;
  51.         }

  52.         ch[i] = '\0';

  53.         return ch;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-24 15:15:28 | 显示全部楼层    本楼为最佳答案   
问题太多,不过代码给你弄好了
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define LEN 40
  5. char *stract(char *ch, char *gh);

  6. int main(int argc, char *argv[])
  7. {
  8.         FILE *in = NULL;//声明两个指向FILE的指针
  9.         FILE *out = NULL;
  10.         int ch;
  11.         char name[LEN];//储存输出文件名
  12.         int count = 0;

  13.         //检查命令行参数
  14.         if (argc < 2)
  15.         {
  16.                 fprintf(stderr, "Usage: %s filename\n", argv[0]);
  17.                 exit(EXIT_FAILURE);
  18.         }
  19.         //设置输入
  20.         if ((fopen_s(&in, argv[1], "r")) != 0)
  21.         {
  22.                 fprintf(stderr, "I count't open the file "%s"\n", argv[1]);
  23.                 exit(EXIT_FAILURE);
  24.         }
  25.         //设置输出
  26.         strncpy_s(name, LEN,argv[1], LEN-5);
  27.         stract(name, ".red");
  28.         if ((fopen_s(&out, name, "w")) != 0)
  29.         {
  30.                 fprintf(stderr, "Can't create output file.\n");
  31.                 exit(EXIT_FAILURE);
  32.         }
  33.         //拷贝数据
  34.         while ((ch = getc(in)) != EOF)
  35.                 if (count++ % 3 == 0)
  36.                         putc(ch, out);
  37.         //收尾工作
  38.         if (fclose(in) != 0 || fclose(out) != 0)
  39.                 fprintf(stderr, "Error in closeing files\n");

  40.         return 0;
  41. }

  42. char *stract(char* ch, char* gh)
  43. {
  44.         int i, j;
  45.         for (i = 0; ch[i] != '\0'; i++)
  46.                 ;
  47.         for (j = 0; gh[j] != '\0'; j++)
  48.         {
  49.                 ch[i]=gh[j];
  50.                 i++;
  51.         }

  52.         ch[i] = '\0';

  53.         return ch;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-24 19:42:16 | 显示全部楼层
ba21 发表于 2017-9-24 15:15
问题太多,不过代码给你弄好了

非常感谢您的回复,能告诉我之前fopen_s()错在哪吗?总是打不开文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-24 19:58:24 | 显示全部楼层
圣人惠 发表于 2017-9-24 19:42
非常感谢您的回复,能告诉我之前fopen_s()错在哪吗?总是打不开文件

自已仔细对比下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-24 20:59:26 | 显示全部楼层
ba21 发表于 2017-9-24 19:58
自已仔细对比下

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

int main(void)
{
        int ch;
        FILE*fp = NULL;
        if (fopen_s(&fp, "beread.txt", "r") == 0)
                puts("the file is opened");
        puts("Done");
        return 0;
}
抱歉,打扰了!
我测试了一下,依旧无法打开文件,真的很困惑
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-24 21:01:34 | 显示全部楼层

你用什么编译器???????????
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-24 21:03:39 | 显示全部楼层
ba21 发表于 2017-9-24 21:01
你用什么编译器???????????

VS2013
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-24 21:12:34 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-24 21:17:23 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-24 21:23:52 | 显示全部楼层

恩,对所以if执行打印“文件已被打开”的语句
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-24 21:25:25 | 显示全部楼层
本帖最后由 ba21 于 2017-9-24 21:28 编辑
圣人惠 发表于 2017-9-24 21:23
恩,对所以if执行打印“文件已被打开”的语句


((fopen_s(&out, name, "w")) != 0)

按这个来改。括号不要少
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-24 21:40:09 | 显示全部楼层
OK了,还是细节不注意,真的谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 22:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表