c - 如何将 char 数组传递给 fopen_s

标签 c fopen

#include <stdio.h>

int main(void)

{
    int i;
    char name[20];
    FILE * src;
    FILE * des;
    fgets(name,20,stdin); 
    fopen_s(&src,"배경사진.jpg", "rb");
    fopen_s(&des,name,"wb");
    char buf[20];
    int readcnt;

    if (src == NULL || des == NULL) {
        puts("파일 오픈 실패!");
        return -1;
    }
    while (1)
    {
        readcnt = fread((void*)buf, 1, sizeof(buf), src);

        if (readcnt < sizeof(buf))
        {
            if (feof(src) != 0)
            {
                fwrite((void*)buf, 1, readcnt, des);


                puts("파일복사 완료");
            }
            else
                puts("파일복사 실패");

            break;
        }

我想在 cmd 中获取(要复制的新文件的名称)。 一般使用fopen_s(&des,"~.jpg", "wb");。 但我使用了 char 数组而不是 "~.jpg"

我该怎么做才能让程序用户可以输入..

最佳答案

如果我理解你的问题。

fgets(name,20,stdin); 

fopen_s(&des,name,"wb");

您的 fopen_s 将失败,因为 fgets 将换行符添加到 name

因此您需要从 name 中删除换行字符。

例子:

size_t len = strlen(name);
if (len > 0 && name[len - 1] == '\n')
    name[len - 1] = '\0';

fopen_s(&des,name,"wb");

关于c - 如何将 char 数组传递给 fopen_s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53712768/

相关文章:

c - C 中的隐式转换

c - 如何默认结束 switch case 中函数的执行? (或任何替代解决方案)

使用信号处理程序的正确方法

32 位处理器真的可以寻址 2^32 个内存位置吗?

c - fopen 不兼容类型 char (*)[9] 而不是 C 中的 char(*)

c - 文件名包含空格无法用 fopen 打开

ios - stdargs [va_start()、va_arg() 等] 在 arm64 上损坏了吗?

php - 在PHP中上传文件时如何获得完整的文件路径?

C-opendir + fopen

c - 从任何数据函数读取打印行