【发布时间】:2016-02-11 12:32:48
【问题描述】:
显然在 C 语言中没有标准化的居中对齐方式,所以我
想知道我如何编写一个 if 语句来居中对齐接下来的 N 行
文本。例如,如果程序在文本文件中找到.ce2,它应该
在页面中心打印接下来的两行,然后照常进行。
.ce2 This is my heading
Lesson 1
Task 2
输出:
This is my heading
Lesson 1
Task 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;
}
【问题讨论】:
-
如果文本不能完全居中会发生什么?另外,请格式化您的代码。到处都是大括号,有点难以阅读。
-
什么是
line?它在哪里填充?请以标准方式格式化您的代码,以便我们阅读它.. -
@PaulOgilvie 我已经添加了代码的开头以使其更易于理解
标签: c formatting user-input