Constant strings

19 12 2011

Usually I need to open a file stream or a file descriptor to write something in that.

I used to create a new string, concatenating the directory (usually some macro) with the file name.

Something like this:

#define DIR    "/tmp/alice/files/"
  
FILE *fs = NULL;
char filename[500] = {0};
  
snprintf(filename, 500, "%s/file.txt", DIR);
fs = fopen(filename, "a+");

But an easy way to do that is to use the facility of constant strings:
#define DIR    "/tmp/alice/files/"
  
FILE *fs = NULL;
  
fs = fopen(DIR "file.txt", "a+");

In my opinion, this way is easier to read and certainly faster.

Bruno Soares.


Actions

Information

Leave a comment