C - fopen() 的相对路径
C - Relative path with fopen()
这是我的问题:我在字符串矩阵中保存了一些相对路径。根据用户的选择,我必须打开某个文件。问题是,当我使用 fopen 函数时,文件指针没有指向任何东西。这是代码示例:
#include <stdio.h>
#include <stdlib.h>
#define MAX_PATH 100
///Global matrix of strings, containing the paths used in fopen() function
char paths[MAX_PATH][3] = {{"c:\Users\ThisPc\Desktop\file1.txt"},
{"c:\Users\ThisPc\Desktop\file2.txt"},
{"c:\Users\ThisPc\Desktop\file3.txt"}};
int main(){
///Declaring and initializing the 3 file pointers to NULL
FILE *filepntr1 = NULL;
FILE *filepntr2 = NULL;
FILE *filepntr3 = NULL;
///Opening the 3 files with the correct arrays
filepntr1 = fopen(paths[1], "w");
filepntr2 = fopen(paths[2], "w");
filepntr3 = fopen(paths[3], "w");
///Typing something on the files opened, just to check if the files where really opened
fprintf(filepntr1, "hello");
fprintf(filepntr2, "hello");
fprintf(filepntr3, "hello");
///Closing the files
fclose(filepntr1);
fclose(filepntr2);
fclose(filepntr3);
return 0;
}
很明显,三个文件都是空白的。
我做错了什么?
您应该检查所有 fopen
、fprintf
、fclose
(如果失败,请考虑使用 perror
)。你可能想(有时)打电话给 fflush
.
一定要阅读我在这里提到的每个函数的文档。
顺便说一句,你可以生成一些文件路径,例如
char path[384];
int i = somenumber();
snprintf(path, sizeof(path), "/some/path/data_%d.txt", i);
FILE *f = fopen(path, "r");
if (!f) { perror(path); exit(EXIT_FAILURE); };
你想为你的路径声明一个二维数组吗?看起来您正在将整个第一行(三个字符串)传递给 fopen()
.
看这里:
您错误地创建和填充路径数组的主要问题,例如尝试这种方法:
const char* paths[3] = {"c:\Users\ThisPc\Desktop\file1.txt",
"c:\Users\ThisPc\Desktop\file2.txt",
"c:\Users\ThisPc\Desktop\file3.txt"};
数组索引从0开始的第二期:
filepntr1 = fopen(paths[0], "w");
filepntr2 = fopen(paths[1], "w");
filepntr3 = fopen(paths[2], "w");
这是我的问题:我在字符串矩阵中保存了一些相对路径。根据用户的选择,我必须打开某个文件。问题是,当我使用 fopen 函数时,文件指针没有指向任何东西。这是代码示例:
#include <stdio.h>
#include <stdlib.h>
#define MAX_PATH 100
///Global matrix of strings, containing the paths used in fopen() function
char paths[MAX_PATH][3] = {{"c:\Users\ThisPc\Desktop\file1.txt"},
{"c:\Users\ThisPc\Desktop\file2.txt"},
{"c:\Users\ThisPc\Desktop\file3.txt"}};
int main(){
///Declaring and initializing the 3 file pointers to NULL
FILE *filepntr1 = NULL;
FILE *filepntr2 = NULL;
FILE *filepntr3 = NULL;
///Opening the 3 files with the correct arrays
filepntr1 = fopen(paths[1], "w");
filepntr2 = fopen(paths[2], "w");
filepntr3 = fopen(paths[3], "w");
///Typing something on the files opened, just to check if the files where really opened
fprintf(filepntr1, "hello");
fprintf(filepntr2, "hello");
fprintf(filepntr3, "hello");
///Closing the files
fclose(filepntr1);
fclose(filepntr2);
fclose(filepntr3);
return 0;
}
很明显,三个文件都是空白的。
我做错了什么?
您应该检查所有 fopen
、fprintf
、fclose
(如果失败,请考虑使用 perror
)。你可能想(有时)打电话给 fflush
.
一定要阅读我在这里提到的每个函数的文档。
顺便说一句,你可以生成一些文件路径,例如
char path[384];
int i = somenumber();
snprintf(path, sizeof(path), "/some/path/data_%d.txt", i);
FILE *f = fopen(path, "r");
if (!f) { perror(path); exit(EXIT_FAILURE); };
你想为你的路径声明一个二维数组吗?看起来您正在将整个第一行(三个字符串)传递给 fopen()
.
看这里:
您错误地创建和填充路径数组的主要问题,例如尝试这种方法:
const char* paths[3] = {"c:\Users\ThisPc\Desktop\file1.txt",
"c:\Users\ThisPc\Desktop\file2.txt",
"c:\Users\ThisPc\Desktop\file3.txt"};
数组索引从0开始的第二期:
filepntr1 = fopen(paths[0], "w");
filepntr2 = fopen(paths[1], "w");
filepntr3 = fopen(paths[2], "w");