arrays - C - Relative path with fopen() -
here's question: have relative paths saved in matrix of strings. depending on user choiche, have open file. problem that, when use fopen function, file pointer doesn't point anything. here's sample of code:
#include <stdio.h> #include <stdlib.h> #define max_path 100 ///global matrix of strings, containing 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 , initializing 3 file pointers null file *filepntr1 = null; file *filepntr2 = null; file *filepntr3 = null; ///opening 3 files correct arrays filepntr1 = fopen(paths[1], "w"); filepntr2 = fopen(paths[2], "w"); filepntr3 = fopen(paths[3], "w"); ///typing on files opened, check if files opened fprintf(filepntr1, "hello"); fprintf(filepntr2, "hello"); fprintf(filepntr3, "hello"); ///closing files fclose(filepntr1); fclose(filepntr2); fclose(filepntr3); return 0; }
obviously, 3 files remain blank.
what doing wrong?
main issue incorrectly create , populate array of paths, try approach example:
const char* paths[3] = {"c:\\users\\thispc\\desktop\\file1.txt", "c:\\users\\thispc\\desktop\\file2.txt", "c:\\users\\thispc\\desktop\\file3.txt"};
second issue array index starts 0:
filepntr1 = fopen(paths[0], "w"); filepntr2 = fopen(paths[1], "w"); filepntr3 = fopen(paths[2], "w");
Comments
Post a Comment