gpt4 book ai didi

c - 如何实现 `.nf`格式化功能

转载 作者:行者123 更新时间:2023-11-30 17:08:27 27 4
gpt4 key购买 nike

我编写了一个程序,用于读取用户输入的文本文件,文件内部包含 .br.sp 等格式化功能.nf.

.nf 表示不填充,也意味着当您在 .nf 之后看到任何格式化功能时,它们都应该被忽略,并且应该只以格式输出文本它最初出现在文本文件中。

例如:

.nf Hello my name is .br Jay.

输出:

Hello my name is Jay

如果 .nf 不存在,则输出将为:

Hello my name is 
Jay.

这是我的代码:

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;
int useFormats = 1;

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 (useFormats == 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 ( strcmp ( word, ".nf") == 0) {// stop filling lines (no fill)
useFormats == 0;
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;
}
else {
printf ( "%s\n", line);
line[0] = '\0';
out = 1;
}
}
else if ( strlen ( line) + strlen ( word) + 1 < 60) {
strcat ( line, " ");
strcat ( line, word);
out = 0;
}
else {
printf ( "%s\n", line);
strcpy ( line, word);
out = 1;
}
}
if ( !out) {
printf ( "%s\n", line);
}

fclose ( fp);
return 0;
}
}

我尝试创建一个名为 useFormats 的变量,该变量在运行时为 true,当它到达 .nf 功能时我将其设置为 false,但没有任何反应。我不确定是否应该删除 if 语句并创建另一个 while 循环来表示 while (useFormats == 0) 来实现.nf 功能。

最佳答案

这应该捕获“.nf” token 并将useFormats设置为零。将 if ( useFormats == 1) { 添加到其他格式条件应该会禁用它们。

#include <stdio.h>
#include <string.h>

#define WIDTH 80

int main(void) {
FILE *fp = NULL;
char file_name[257] = {'\0'};
char line[61] = {'\0'};
char word[61] = {'\0'};
int out = 0;
int blanks = 0;
int center = 0;
int useFormats = 1;
int margin = 0;

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) {// read file to end one word at a time
if ( strcmp ( word, ".nf") == 0) {// no formatting)
useFormats = 0;
center = 0;
continue;
}
if ( strncmp ( word, ".ce", 3) == 0) {
if ( useFormats == 1) {
if ( ( sscanf ( &word[3], "%d", &center)) != 1) {
center = 0;
}
}
continue;
}
if ( strncmp ( word, ".sp", 3) == 0) {
if ( useFormats == 1) {
if ( ( sscanf ( &word[3], "%d", &blanks)) == 1) {
margin = 0;
if ( center) {
margin = WIDTH - ( ( WIDTH - strlen ( line)) / 2);
center--;
}
printf ( "%*s\n", margin, line);
while ( blanks) {
blanks--;
printf ( "\n");
}
line[0] = '\0';
out = 1;
}
else {
margin = 0;
if ( center) {
margin = WIDTH - ( ( WIDTH - strlen ( line)) / 2);
center--;
}
printf ( "%*s\n", margin, line);
line[0] = '\0';
out = 1;
}
}
continue;
}
if ( strcmp ( word, ".br") == 0) {
if ( useFormats == 1) {
margin = 0;
if ( center) {
margin = WIDTH - ( ( WIDTH - strlen ( line)) / 2);
center--;
}
printf ( "%*s\n", margin, line);
line[0] = '\0';
out = 1;
}
continue;
}
if ( strlen ( line) + strlen ( word) + 1 <= 60) {
strcat ( line, " ");
strcat ( line, word);
out = 0;
}
else {
margin = 0;
if ( center) {
margin = WIDTH - ( ( WIDTH - strlen ( line)) / 2);
center--;
}
printf ( "%*s\n", margin, line);
strcpy ( line, word);
out = 1;
}
}
if ( !out) {
margin = 0;
if ( center) {
margin = ( WIDTH - strlen ( line)) / 2;
center--;
}
printf ( "%*s\n", margin, line);
}

fclose ( fp);
return 0;
}

关于c - 如何实现 `.nf`格式化功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33649520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com