如何在文本文件中写入"const char*"的内容?

the*_*ive -4 c file-io pointers

如果一个常量被声明为这样

const char   *http_range;
Run Code Online (Sandbox Code Playgroud)

那我怎么能在文本文件中写出它的内容或价值呢?你能告诉我语法吗?

unw*_*ind 5

首先,这不是"常数".它是指向常量字符数据的指针,即指向只读字符串的指针.您可以更改指针,但不能更改它指向的数据.例如:

FILE *out;
const char *http_range = "Accept: text/*;q=0.3, text/html;q=0.7";

if ((out = fopen("textfile.txt", "w")) != NULL)
{
  fprintf(out, "the range is '%s'\n", http_range);
  fclose(out);
}
Run Code Online (Sandbox Code Playgroud)

请注意,以上是在C中,你的问题是奇怪的双重标记所以我选择了C.

  • @the_naive你可以通过接受他的答案来感谢他,如果它对你有帮助的话. (3认同)