c - 指定文件扩展名

标签 c file-io

现在,我所拥有的是:

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main() 
{
    char fname[100];
    FILE* fp;
    memset(fname, 0, 100);

    /* ask user for the name of the file */
    printf("enter file name: ");
    gets(fname);

    fp = fopen(fname, "w");

    /* Checks if the file us unable to be opened, then it shows the 
       error message */
    if (fp == NULL)
        printf("\nError, Unable to open the file for reading\n");
    else
        printf("hello");
    getch();
}

这个功能很好,但是有没有办法可以强制它保存为 .txt 或 .data 或其他格式?现在它只是保存为您输入的名称,没有扩展名。除了要求用户输入名称和扩展名之外,我想不出其他方法来做到这一点。我的意思是,它仍然可以很好地用于阅读/写作目的,我只是认为扩展会很好。

最佳答案

扩展我的评论:

strcat(fname, ".txt");

关于c - 指定文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42405403/

相关文章:

c - fseek 和 fwrite 过去的 EOF

c - 我如何要求用户提供文件名以用作输入文件?

c - 如何通过 ssl 和套接字发送目标文件 .o

c - 从文件初始化嵌套结构的值

c++ - 在编译时读取文件(constexpr 或其他)

c - Malloc 2372(错误)无法识别我错在哪里

c++ - 为 C/C++ 设置 Windows 构建环境的最佳方法

c - 使用 UNION 和 STRUCTURE

c - OpenGL - 在窗口调整大小时保持纵横比

c++ - 如何将退格键更改为 '\b' ?