圣人惠 发表于 2017-9-24 13:20:56

关于fopen_( )的小问题

//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;//储存输出文件名
        int count = 0;

        //检查命令行参数
        if (argc < 2)
        {
                fprintf(stderr, "Usage: %s filename\n", argv);
                exit(EXIT_FAILURE);
        }
        //设置输入
        if ((fopen_s(&in,argv, "r")) == NULL)
        {
                fprintf(stderr, "I count't open the file \"%s\"\n", argv);
                exit(EXIT_FAILURE);
        }
        //设置输出
        strncpy_s(name, argv, 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 != '/0'; i++)
                ;
        for (j = 0; gh != '/0'; j++)
        {
                ch = gh;
                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)
求指点!!!

ba21 发表于 2017-9-24 15:15:28

问题太多,不过代码给你弄好了
#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;//储存输出文件名
        int count = 0;

        //检查命令行参数
        if (argc < 2)
        {
                fprintf(stderr, "Usage: %s filename\n", argv);
                exit(EXIT_FAILURE);
        }
        //设置输入
        if ((fopen_s(&in, argv, "r")) != 0)
        {
                fprintf(stderr, "I count't open the file \"%s\"\n", argv);
                exit(EXIT_FAILURE);
        }
        //设置输出
        strncpy_s(name, LEN,argv, 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 != '\0'; i++)
                ;
        for (j = 0; gh != '\0'; j++)
        {
                ch=gh;
                i++;
        }

        ch = '\0';

        return ch;
}

圣人惠 发表于 2017-9-24 19:42:16

ba21 发表于 2017-9-24 15:15
问题太多,不过代码给你弄好了

非常感谢您的回复,能告诉我之前fopen_s()错在哪吗?总是打不开文件

ba21 发表于 2017-9-24 19:58:24

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

自已仔细对比下

圣人惠 发表于 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;
}
抱歉,打扰了!
我测试了一下,依旧无法打开文件,真的很困惑

ba21 发表于 2017-9-24 21:01:34

圣人惠 发表于 2017-9-24 20:59
#include
#include



你用什么编译器???????????

圣人惠 发表于 2017-9-24 21:03:39

ba21 发表于 2017-9-24 21:01
你用什么编译器???????????

VS2013

圣人惠 发表于 2017-9-24 21:12:34

圣人惠 发表于 2017-9-24 21:03
VS2013

ba21 发表于 2017-9-24 21:17:23

圣人惠 发表于 2017-9-24 21:12


{:10_305:}

圣人惠 发表于 2017-9-24 21:23:52

ba21 发表于 2017-9-24 21:17


恩,对所以if执行打印“文件已被打开”的语句

ba21 发表于 2017-9-24 21:25:25

本帖最后由 ba21 于 2017-9-24 21:28 编辑

圣人惠 发表于 2017-9-24 21:23
恩,对所以if执行打印“文件已被打开”的语句

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

按这个来改。括号不要少

圣人惠 发表于 2017-9-24 21:40:09

OK了,还是细节不注意,真的谢谢!
页: [1]
查看完整版本: 关于fopen_( )的小问题