C - fopen()的相对路径

ale*_*asa 1 c arrays io fopen file

这是我的问题:我在字符串矩阵中保存了一些相对路径.根据用户选择,我必须打开某个文件.问题是,当我使用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;
}
Run Code Online (Sandbox Code Playgroud)

显然,这三个文件仍然是空白的.

我究竟做错了什么?

ISa*_*ych 5

您错误地创建和填充路径数组的主要问题,例如尝试此方法:

const char* paths[3] = {"c:\\Users\\ThisPc\\Desktop\\file1.txt",
                        "c:\\Users\\ThisPc\\Desktop\\file2.txt",
                        "c:\\Users\\ThisPc\\Desktop\\file3.txt"};
Run Code Online (Sandbox Code Playgroud)

数组索引从0开始的第二个问题:

filepntr1 = fopen(paths[0], "w");
filepntr2 = fopen(paths[1], "w");
filepntr3 = fopen(paths[2], "w");
Run Code Online (Sandbox Code Playgroud)