现在,我所拥有的是:

#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/

10-11 22:26
查看更多