在 C 中使用 snprintf 时如何插入换行符('\n')
How can i insert newline character('\n') when i use snprintf in C
我试过这个代码
void print_formatted(void) {
char buffer[100];
char line[15];
FILE* fp;
char* message = "Hello World in C language"
fp = fopen("test.txt","w");
snprintf(line, 10, "%s\n", message);
strcpy(buffer, line);
buffer += 11;
snprintf(line, 10, "%s\n", message + 10);
strcpy(buffer, line);
fwrite(buffer, sizeof(buffer[0]), 20, fp);
}
预期结果(test.txt):
Hello World in
C language
但实际结果是(test.txt) :
Hello World in C language
这里是缓冲存储器:
.
.
[10] = '[=13=]'
[11] = 'C'
[12] = ' '
[13] = 'l'
.
.
如何插入“\n”数据而不是“\0”。
我怎样才能打印格式化..
发布的代码不可能是您的真实代码。发布的代码无法编译。
这里:
char* message = "Hello World in C language"
^
Missing ;
这里:
buffer += 11;
error: assignment to expression with array type
无论如何 - 你的问题似乎是对 snprintf
的误解
从http://man7.org/linux/man-pages/man3/fprintf.3p.html我们有
The snprintf() function shall be equivalent to sprintf(), with the
addition of the n argument which states the size of the buffer
referred to by s. If n is zero, nothing shall be written and s may
be a null pointer. Otherwise, output bytes beyond the n‐1st shall be
discarded instead of being written to the array, and a null byte is
written at the end of the bytes actually written into the array.
那么这是什么意思?
假设你有:
char line[15000];
snprintf(line, 10000, "%s\n", message);
在这种情况下,目标缓冲区中有足够的空间,因此 line
将是
"Hello World in C language\n"
这是 message
中的所有字符加上格式字符串中的 '\n'
。
当您将代码更改为:
char line[15];
snprintf(line, 10, "%s\n", message);
您只会得到上述字符串的前 9 个字符 - 因此您会在 line
中得到以下值:
"Hello Wor"
所以 '\n'
连同 message
的一部分被切掉了。
有很多方法可以添加 '\n'
- 这里是一个:
char line[15];
int n = snprintf(line, 10, "%s\n", message);
if (n > 9)
{
line[8] = '\n';
}
else if (n > 0)
{
line[n-1] = '\n';
}
在您的情况下,这将导致 line
为:
"Hello Wo\n"
我试过这个代码
void print_formatted(void) {
char buffer[100];
char line[15];
FILE* fp;
char* message = "Hello World in C language"
fp = fopen("test.txt","w");
snprintf(line, 10, "%s\n", message);
strcpy(buffer, line);
buffer += 11;
snprintf(line, 10, "%s\n", message + 10);
strcpy(buffer, line);
fwrite(buffer, sizeof(buffer[0]), 20, fp);
}
预期结果(test.txt):
Hello World in
C language
但实际结果是(test.txt) :
Hello World in C language
这里是缓冲存储器:
.
.
[10] = '[=13=]'
[11] = 'C'
[12] = ' '
[13] = 'l'
.
.
如何插入“\n”数据而不是“\0”。 我怎样才能打印格式化..
发布的代码不可能是您的真实代码。发布的代码无法编译。
这里:
char* message = "Hello World in C language"
^
Missing ;
这里:
buffer += 11;
error: assignment to expression with array type
无论如何 - 你的问题似乎是对 snprintf
从http://man7.org/linux/man-pages/man3/fprintf.3p.html我们有
The snprintf() function shall be equivalent to sprintf(), with the addition of the n argument which states the size of the buffer referred to by s. If n is zero, nothing shall be written and s may be a null pointer. Otherwise, output bytes beyond the n‐1st shall be discarded instead of being written to the array, and a null byte is written at the end of the bytes actually written into the array.
那么这是什么意思?
假设你有:
char line[15000];
snprintf(line, 10000, "%s\n", message);
在这种情况下,目标缓冲区中有足够的空间,因此 line
将是
"Hello World in C language\n"
这是 message
中的所有字符加上格式字符串中的 '\n'
。
当您将代码更改为:
char line[15];
snprintf(line, 10, "%s\n", message);
您只会得到上述字符串的前 9 个字符 - 因此您会在 line
中得到以下值:
"Hello Wor"
所以 '\n'
连同 message
的一部分被切掉了。
有很多方法可以添加 '\n'
- 这里是一个:
char line[15];
int n = snprintf(line, 10, "%s\n", message);
if (n > 9)
{
line[8] = '\n';
}
else if (n > 0)
{
line[n-1] = '\n';
}
在您的情况下,这将导致 line
为:
"Hello Wo\n"