动态包括用C可执行文件中的文本 [英] dynamically include text in C executable file

查看:79
本文介绍了动态包括用C可执行文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常新的C和汇编语言。我需要主要包括动态文本,从这个code另一个文件中的行:

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

我已经使用#include链接到文本文件试过,这个工程然而,当在文件链接文本更改它显然是编译的可执行文件不会改变。有没有简单的方法来做到这一点?

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?

推荐答案

的#include 指令简单的拷贝之前的另一个文件的内容到源$ C ​​$ c将其转换成C编译器的可执行文件。换句话说,这是一个一次性的过程,烤成你的code中的其他文件。

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.

如果您需要重新加载一个文件的内容动态每次程序运行时,你需要使用C 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;

}

这code应该主要是不言自明的,但也有值得指出的几件事情:

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


  • 该文件正在 RB 打开(读二进制)模式 - 您可能需要使用研究(阅读文本),而不是取决于你在做什么。如果是这样,你可能会想用一个普通的存储数据的char * 而非无符号字符* 作为我已经在这里完成的。

  • 一个额外的字节分配给字符串的零空值终止符。

  • 缓冲器被存储在动态分配的存储器。既然你似乎更熟悉动态语言如Python或Ruby,我要提醒你,你将需要免费()已分配的内存一旦你完成用吧。

  • 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.

这篇关于动态包括用C可执行文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆