0

I am building a program that reads an XML configuration file. The XML configuration file contains several variables. One of which is

<bin_file>/some/file/path/file.bin</bin_file>

In my code I have the following struct

struct buildConofig {
  char foo[3];
  char bar[2];
  char bin_file[?];
};

The code looks at the XML file and fills in the struct as it finds the matching elements in XML. I'm not sure what the length of the bin_file field should be. Is there a limit to file path size?

Or should I make bin_file a pointer and then malloc enough bytes after I check the XML field?

Thanks for your help.

Aman
  • 773
  • 2
  • 10
  • 22
  • MAX_PATH is whats usually used http://stackoverflow.com/questions/833291/is-there-an-equivalent-to-winapis-max-path-under-linux-unix – pm100 Oct 22 '14 at 17:54
  • 1
    pointer and malloc later is probably the best way of doing it, but you can always use the maximum size you will allow and add some error checking to avoid errors. – Marshall Tigerus Oct 22 '14 at 17:56
  • 1
    As a guide one, `FILENAME_MAX` in ``. (MAX_PATH is not standard) But the length of the file name in fact depends on the respective OS. – BLUEPIXY Oct 22 '14 at 17:56
  • 1
    IMO: If only a few instances of `struct buildConofig`, use `bin_file[MAX_PATH]` or equivalent. Otherwise make `bin_file` a pointer and then `malloc` enough bytes after checking the XML field. – chux - Reinstate Monica Oct 22 '14 at 18:10

2 Answers2

0

As others pointed out, using malloc() would probably be the safest way. In this particular case, it might even be dangerous relying on constants/macros like FILENAME_MAX. When reading that path from the XML document, it is still just a string, so it can be of virtually any length (and thus happily overflow your char buffer[FILENAME_MAX] if you don't cut it off manually).

Jan
  • 293
  • 1
  • 11
-2

in different system there are a little different such as:

ubuntu 12.04:
NAME_MAX: 255     //file max length
PATH_MAX: 4096    // path max length

as showed above the max size may be 255*4096= 1044480? that means you should do like

 char bin_file[1044480];

but this is not recommended.

you can include string.h and do like below

#include<string.h>
struct buildConofig {
  char foo[3];
  char bar[2];
  string bin_file;
};
Patato
  • 1,464
  • 10
  • 12
  • 3
    string.h doesnt define struct string. If you mean std::string then note that this Q is tagged C – pm100 Oct 22 '14 at 18:04
  • i made a mistake. there is no string struct in C, and PATH_MAX means max depth of the path so it is 255*4096 – Patato Oct 22 '14 at 18:22