1

Here's my question: I have some relative paths saved in a matrix of strings. Depending on the user choiche, I have to open a certain file. The problem is that, when I use the fopen function, the file pointer doesn't point to anything. Here's a sample of the code:

#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;
}

Obviously, the three files remain blank.

What am I doing wrong?

alemootasa
  • 83
  • 2
  • 9
  • 2
    You declared an array of 100 strings which are 3 bytes long, each. – Alexguitar Jul 22 '15 at 17:18
  • No man, check this out http://stackoverflow.com/a/1095006/5085985. Also, the compiler would have tell me that, I am initializing the matrix with longer strings than 3 characters. Thank you for responding! – alemootasa Jul 22 '15 at 17:37
  • char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1]; 100 strings, which can hold 3 bytes (including the terminating '\0') – Alexguitar Jul 22 '15 at 17:40
  • The compiler probably didn't warn you because you're making an assignment of a char ** type to a char **, which is legal. Always compile with -Wall when something goes wrong. – Alexguitar Jul 22 '15 at 17:45
  • No man, your declaration is wrong, and indices start at 0 in C – Ed S. Jul 22 '15 at 19:52
  • @Alexguitar and Ed.s Allright, the blunder is always hiding in the darkest corner of the room. You are right, sorry, my bad! – alemootasa Jul 23 '15 at 07:54
  • compiling with all warnings enabled would have told you the syntax problems in the code. When #define'ing a numeric value, always wrap the value in parens to avoid text-replacement errors – user3629249 Jul 23 '15 at 14:48

3 Answers3

5

Main issue that you incorrectly create and populate array of paths, try this approach for 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 that array index starts from 0:

filepntr1 = fopen(paths[0], "w");
filepntr2 = fopen(paths[1], "w");
filepntr3 = fopen(paths[2], "w");
ISanych
  • 21,590
  • 4
  • 32
  • 52
  • Second issue: yeah, I forgot to correct the indexes, but in the program I am actually writing the indexes are correct (they start from 0). My bad, sorry! First issue: it worked, thank you very much! – alemootasa Jul 22 '15 at 17:22
1

You should check against failure all of fopen, fprintf, fclose (on failure, consider using perror). And you might want to call (sometimes) fflush.

Be sure to read the documentation of every function I am mentioning here.

BTW, you can generate some file path, e.g.

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); };
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Do you want to be declaring a two-dimensional array for your paths? It looks like you're passing your whole first row (three strings) to fopen().

See here: https://stackoverflow.com/a/1095006/5085985

Community
  • 1
  • 1
Seth
  • 43
  • 1
  • 6
  • Yes I am, in the actual program I need some container for all the paths I am using, basically because they are a lot and I don't want to create lots and lots of variables just to insert them in fopen(). Thank you for responding! – alemootasa Jul 22 '15 at 17:28