用c语言编写个程序,条件:执行程序时,可以输入一个文件名,建立的这个文件保存路径用程序确定。

2025-04-13 08:37:44
推荐回答(1个)
回答1:

#include 

void main()
{
    char file[256], str[4096], path[256], ch;
    FILE *fp;
    printf("please input filename:");
    scanf("%s", file);
while(ch = getchar() != '\n' && ch != EOF);

    sprintf(path, "D:/%s.txt", file);
    printf("please input string:");
    gets(str);
    fp = fopen(path, "w");
    if(fp==NULL)
    {
        printf("Can't open the file <%s>\n", path);
        return ;
    }
    fputs(str, fp);
    fclose(fp);
}