C 如何将N行文本居中对齐
显然,C语言中没有标准化的文本居中对齐方式,所以我 我想知道的是如何编写一个if语句来对齐接下来的N行 文本的格式。例如,如果程序在文本文件中找到C 如何将N行文本居中对齐,c,formatting,user-input,C,Formatting,User Input,显然,C语言中没有标准化的文本居中对齐方式,所以我 我想知道的是如何编写一个if语句来对齐接下来的N行 文本的格式。例如,如果程序在文本文件中找到.ce2,它应该 在页面中央打印下两行,然后按正常方式进行 .ce2 This is my heading Lesson 1 Task 2 输出: This is my heading Lesson 1 任务2 下面是我编写的代码,我实现了.br功
.ce2
,它应该
在页面中央打印下两行,然后按正常方式进行
.ce2 This is my heading
Lesson 1
Task 2
输出:
This is my heading
Lesson 1
任务2
下面是我编写的代码,我实现了.br
功能来打破
语句和.sp
功能,在文本中添加空行
int main(void) {
FILE *fp = NULL;
char file_name[257] = {'\0'};
char line[61] = {'\0'};
char word[61] = {'\0'};
int out = 0;
int blanks;
int space;
printf ( "Enter file name:\n");
scanf ( " %256[^\n]", file_name);
if ( ( fp = fopen ( file_name, "r")) == NULL) {
printf ( "could not open file\n");
return 1;
}
while ( ( fscanf ( fp, "%60s", word)) == 1) { //breaks the sentence after .br
if ( strcmp ( word, ".br") == 0) {
printf ( "%s\n", line);
line[0] = '\0';
out = 1;
}
if ( strncmp ( word, ".sp", 3) == 0) { // creates n amount of spaces after .sp
if ( ( sscanf ( &word[3], "%d", &blanks)) == 1) {
printf ( "%s\n", line);
while ( blanks) {
blanks--;
printf ( "\n");
}
line[0] = '\0';
out = 1;
}
if ( strncmp ( word, ".ce", 3) == 0) { // centre the next n lines
if ( ( sscanf ( &word[3], "%d", &space)) == 1) {
//this is the if statement I am stuck at on what to include
}
line[0] = '\0';
out = 1;
}
printf
不提供文本居中的机制,但它提供了右对齐文本(字段宽度)的机制和将字段宽度指定为参数的机制。将这两者放在一起,很容易使文本居中:
int print_centered(FILE* f, const char* str, int width) {
int len = strlen(str);
if (len < width)
return fprintf(f, "%*s\n", (width + len) / 2, str);
else /* Line is too long to fit */
return fprintf(f, "%s\n", str);
}
有关更多详细信息,请参阅。此函数将获取要居中文本的缓冲区宽度、格式和printf接受的任何其他参数,如果宽度小于输出缓冲区,它将截断
#include <stdarg.h>
/***********************************
* This function will center a *
* buffer of size 'width', and *
* it will truncate larger strings *
**********************************/
int center_printf(int width, const char* format, ...)
{
char *tmp = NULL, *output = NULL;
int length, leading_spaces, ret;
va_list args;
if ( width <= 0 ) // Abort on incorrect width.
{
ret = -1;
goto cleanup;
}
tmp = (char *)malloc ( width );
if (tmp == NULL)
{
ret = -1;
goto cleanup;
}
output = (char *)malloc ( width );
if ( output == NULL )
{
ret = -1;
goto cleanup;
}
va_start ( args, format );
memset ( output, 0x20, width ); // Fill the output buffer with spaces.
length = vsnprintf ( tmp, width, format, args );
leading_spaces = ( length >= width ) ? 0 : (width - length)/ 2;
memcpy ( output + leading_spaces, tmp, length );
ret = printf ( output );
va_end( args );
cleanup:
if ( tmp != NULL )
free ( tmp );
if ( output != NULL )
free ( output );
return ret;
}
如果文本不能完全居中,该怎么办?另外,请格式化您的代码。到处都是大括号,读起来有点难。什么是
行
?在哪里装的?请以标准方式格式化您的代码,以便我们阅读。@PaulOgilvie我添加了代码的开头,以使其更易于理解您的代码可以工作,但我认为这与我的问题无关,因为我有一个给定的文件,其中包含.ce2
,这意味着接下来的两行应该居中,但您的代码没有任何更改对我现有的code@jayjay:根据您对它的看法,它提供了一个函数,您可以调用该函数来居中一条直线,该函数只允许您使用代码来读取要居中的直线,或者它提供了一个模型来创建代码。它没有提供“请为我编写我的代码”,而这并不是问题所在。如果(我认为这种方法对我来说太复杂了,我正在寻找一种更简单的方法,类似于我实现.sp
和的方法。br
这个解决方案是不必要的复杂,是的。它还有两个明显的错误:(1)包含width
字符的字符串需要malloc(width+1)
(两个位置,尽管不需要第二个副本,因为可以使用printf输出必要的空格);以及(2)使用任意字符串作为printf
的格式是不安全的。使用fputs(str,stdout)
或printf(“%s”,str)
(如果要打印的字符串中有百分比会发生什么情况?)我使用您的注释和答案修复了我的函数。感谢您的输入,我实际使用了它,我的要求之一是接受格式参数以及发送的任何其他变量,就像它是一个普通的printf函数一样。啊。我明白您通过像\%这样转义的百分号是什么意思。下面的printf将出现问题。谢谢.正在努力。如果我有这些观点,我会对你的评论投赞成票。
#include <stdarg.h>
/***********************************
* This function will center a *
* buffer of size 'width', and *
* it will truncate larger strings *
**********************************/
int center_printf(int width, const char* format, ...)
{
char *tmp = NULL, *output = NULL;
int length, leading_spaces, ret;
va_list args;
if ( width <= 0 ) // Abort on incorrect width.
{
ret = -1;
goto cleanup;
}
tmp = (char *)malloc ( width );
if (tmp == NULL)
{
ret = -1;
goto cleanup;
}
output = (char *)malloc ( width );
if ( output == NULL )
{
ret = -1;
goto cleanup;
}
va_start ( args, format );
memset ( output, 0x20, width ); // Fill the output buffer with spaces.
length = vsnprintf ( tmp, width, format, args );
leading_spaces = ( length >= width ) ? 0 : (width - length)/ 2;
memcpy ( output + leading_spaces, tmp, length );
ret = printf ( output );
va_end( args );
cleanup:
if ( tmp != NULL )
free ( tmp );
if ( output != NULL )
free ( output );
return ret;
}
int center_printf(int width, const char* format, ...)
{
char *output = NULL;
int length, ret;
va_list args;
if ( width <= 0 ) // Abort on incorrect width.
{
ret = -1;
goto cleanup;
}
output = (char *)malloc ( width + 1 );
if (output == NULL)
{
ret = -1;
goto cleanup;
}
va_start ( args, format );
length = vsnprintf ( output, width + 1, format, args );
ret = printf ( "%*s", ( length >= width ) ? 0 : (width + length)/2 , output );
va_end( args );
cleanup:
if ( output != NULL )
free ( output );
return ret;
}
center_printf (50, "%s: testing 123", __FUNCTION__);