如何在C中设置驱动器

时间:2018-10-28 10:10:59

标签: c windows cross-platform

我想知道是否可以在C中切换驱动程序。

例如:

/*Example*/
#include <stdio.h>;

int main(){
    SwitchDrive("C:\");
    FILE *c=fopen("example.txt","w");
    fprintf(c,"Example");
    fclose(c);
    SwitchDrive("D:\");
    FILE *d=fopen("d_drive.txt","w");
    fprintf(d,"Example");
    fclose(d);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

您需要使用SetCurrentDirectory()

答案 1 :(得分:0)

为了获得最佳的跨平台兼容性,您需要使用chdir

此功能在C标准Posix库中,该库已得到各种OS的广泛支持。

不过,您仍然需要进行一些#ifdef检查才能解决Microsoft因弃用chdir而产生的特定问题。他们的entry for chdir提及

  

不推荐使用此POSIX函数。请改用符合ISO C ++的_chdir
  重要
  在Windows运行时中执行的应用程序中不能使用此API。 [...]

,并重定向您使用_chdir