默认当前路径:
- #include <stdio.h>
-
- int main()
- {
- FILE *fp = fopen("myData.txt", "w");
- fprintf(fp, "%d", 123);
- fclose(fp);
-
- return 0;
- }
显式当前路径:
- #include <stdio.h>
-
- int main()
- {
- // 在C中,'\'表示转义,故要用双斜杠
- FILE *fp = fopen(".\\myData.txt", "w");
- fprintf(fp, "%d", 123);
- fclose(fp);
-
- return 0;
- }
上级路径:
- #include <stdio.h>
-
- int main()
- {
- // 在C中,'\'表示转义,故要用双斜杠
- FILE *fp = fopen("..\\myData.txt", "w");
- fprintf(fp, "%d", 123);
- fclose(fp);
-
- return 0;
- }
上上级路径:
- #include <stdio.h>
-
- int main()
- {
- // 在C中,'\'表示转义,故要用双斜杠
- FILE *fp = fopen("..\\..\\myData.txt", "w");
- fprintf(fp, "%d", 123);
- fclose(fp);
-
- return 0;
- }
利用相对路径很有好处,程序的移植性更好. OK, 不多说.