-1

very new to C and compiled languages. I need to basically include a line of dynamic text from another file in this code:

#if T2T_NDEFFILE_PREDEF == URI
const uint8_t T2T_DATA_DEF[] = {
#include "/home/link"
      // Terminator TLV
      0xF3
};
#endif

I've tried using #include to link to the text file, this works however when the text in the file 'link' changes it obviously doesn't change in the compiled executable file. Is there any simple way to do this?

Ripwinder
  • 210
  • 3
  • 11
  • 1
    Couldn't you just write a program that opens and reads the text file? What exactly is the problem here? – r3mainer Nov 01 '14 at 23:31
  • The problem is as I said I don't know any C at all, I only need to include this file in a dynamic way. If you can give an example that would be great. – Ripwinder Nov 01 '14 at 23:40
  • Give an example of *what*, exactly? Unfortunately I really don't have a clue what you're trying to achieve here. Did you try searching online for C programming tutorials? [Here's one I found in about 10 seconds.](http://www.cprogramming.com/tutorial/cfileio.html) – r3mainer Nov 01 '14 at 23:52
  • You might want to try something like http://stackoverflow.com/a/4158997/12711 Though I'm not sure if it meets your 'dynamic' requirement since it's not very clear how dynamic you need it to be (for example, you might need to do as @squeamishossifrage suggests). – Michael Burr Nov 02 '14 at 00:13

1 Answers1

2

The #include directive simply copies the contents of another file into your source code before it is converted into an executable by the C compiler. In other words, it's a one time process, with the other file "baked into" your code.

If you need to re-load the contents of a file "dynamically" each time the program is run, you'll need to load it in yourself using C code. Here's an example, pulled from one of my own projects:

/*
[PUBLIC] Load the contents of a file into a malloc()'d buffer.
*/

unsigned char *txtLoadFile(const char *file_name, long *length)
{

  FILE          *fsrc = NULL;
  unsigned char *data = NULL;
  long          size  = 0;

  /* Attempt to open the requested file. */

  fsrc = fopen(file_name, "rb");
  if (!fsrc) { return NULL; }

  /* Get the length of the file in bytes. */

  fseek(fsrc, 0, SEEK_END);
  size = (long)ftell(fsrc);
  rewind(fsrc);

  /* Copy the data into memory (with an extra zero byte, in case it's text). */

  data = (unsigned char*)malloc(size + 1);

  if (data)
  {
    memset(data, 0, size + 1);
    fread(data, 1, size, fsrc);
  }

  fclose(fsrc);

  /* Return the result. */

  if (length) { *length = size; }

  return data;

}

This code should be largely self-explanatory, but there are a few things worth pointing out:

  • The file is being opened in rb (read-binary) mode - you may need to use r (read-text) instead, depending on what you're doing. If so, you'll probably want to store the data using a plain char* rather than an unsigned char * as I've done here.
  • An extra byte is allocated for the zero NULL-terminator character of the string.
  • The buffer is being stored in dynamically-allocated memory. Since you seem to be more familiar with dynamic languages such as Python or Ruby, I should remind you that you will need to free() the allocated memory once you're done with it.
GoBusto
  • 4,632
  • 6
  • 28
  • 45
  • Just a few (minor) notes: [no need to cast `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc); [no 'text' mode in Unix](http://stackoverflow.com/questions/2174889/whats-the-differences-between-r-and-rb-in-fopen); no real need to zero out the entire malloc'ed memory, `data[size]=0;` is enough here. – Jongware Nov 02 '14 at 01:41