本文介绍了如何在C上将文件写入非特定用户的桌面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,希望它可以将文件写入桌面.我想出了如何将其写入我的数据库,但我希望它能够转到任何人的桌面(Windows).

I wrote a program and I wanted it to write a file to the Desktop. I figured out how to write it to mine, but I want it to be able to go to anybody's desktop (windows).

有没有一种方法可以使路径名更具动态性,使其可以在任何人的桌面上正常工作?

Is there a way to make the path name more dynamic so it can work on anybody's desktop?

这是一个示例:

void add(void)
{
    FILE *fp;
    fp = fopen("C:\\Users\\Jones\\Desktop\\test.txt", "w+");
    float num1;
    float num2;
    float sum;
    printf("Enter first number: ");
    num1 = getNum();
    printf("Enter second number: ");
    num2 = getNum();
    sum = num1 + num2;
    printf("%.1f + %.1f = %.1f\n", num1, num2, sum);
    fprintf(fp, "Num1: %.1f\nNum2: %.1f\nSum: %.1f ", num1, num2, sum);
    fclose(fp);
    while(getchar() != '\n')
    {
                continue;
    }

}

推荐答案

是.使用sprintf.就像fprintf一样,但用于字符串.

Yes. Use sprintf. It's like fprintf but for strings.

char fname[256];
sprintf(fname, "C:\\Users\\%s\\Desktop\\test.txt", "Jones");
fopen(fname, ...

这篇关于如何在C上将文件写入非特定用户的桌面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 11:30