이것은 C 지정입니다. 나는 나를 위해 이것을 할 사람을 요구하지 않고있다, 나는 단지 벽에 부딪쳤다. 내일 끝날 예정이며 진행 방법을 모르겠습니다. 나는 초보자이고 상처를 입기 시작한다.C 프로그래밍 할당 도움말 : 읽기 및 인쇄 형식
주어진 수의 열에 잘 맞도록 텍스트의 서식을 지정하는 ANSI C 프로그램을 작성한다. 텍스트 포맷터는 한 가지 예외를 제외하고 오른쪽 여백이 직선으로 정렬되도록 입력 텍스트 파일을 오른쪽 정렬해야합니다. 마지막 줄은 올바르지 않습니다. 또한 단락은 함께 병합되지 않습니다. 출력 선 사이의 간격은 균등해야합니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define IN 1
#define OUT 0
/*
This is the start of the main pgoram which originated from the K & R word counter...
we comment to understand each part...
*/
int main()
{
/*
* This is the pointer to the file object we will be readin in
* from...
*/
FILE *ptr_file;
char *outputbuf;
/*
* This variable will hold the maximum width of the line we are to
* output
*/
int width;
char eatspace;
char c; /* We read each character invidually */
int state = OUT;
int nc = 0; /* This is the total count of all words in the document */
int nl = 0; /* This is the total count of newlines in the document */
int nw = 0;
int lw = 0; /* Count the total whitespaces spaces per line */
int buff_offset = 0; /* Keep track of how many letters we are into the current output line */
/* Opens a file stream for the .txt file to be read in */
ptr_file = fopen("hollo_man.txt", "r");
if ((fopen("hollo_man.txt", "r")) != NULL) {
/*
* This loop reads in one character at a time until the end
* of file
*/
/* Read the first line to get the width of the output */
fscanf (ptr_file, "%i", &width);
outputbuf = (char*) malloc(width + 1);
//fscanf(ptr_file, "%c", &eatspace);
int prev_char_was_space = 0;
while ((c = fgetc(ptr_file)) != EOF)
{
++nc;
if (c == '\n' || strlen(outputbuf) == width)
{
outputbuf[buff_offset] = '\0';
++nl;
// printf("Saw a newline, newline count is now: %i\n", nl);
/* Our buffer needs to be ended since we saw a newline */
for(int i = 0; i < (width - buff_offset); i++)
{
printf(" ");
}
printf("%s\n", outputbuf);
memset(outputbuf, width, '\0');
buff_offset = 0;
prev_char_was_space = 0;
}
/* This more verbose check is to see if there is other whitespace */
else if (isspace(c))
{
/* We only store one space between words in the output, this allows us to easily and evenly pad with white space later */
if (!prev_char_was_space)
{
outputbuf[buff_offset] = c;
outputbuf[buff_offset + 1] = '\0';
buff_offset++;
lw++;
prev_char_was_space = 1;
}
}
else /* This was not a whitespace character so store it in the current line buffer */
{
prev_char_was_space = 0; /* Keep track that we didnt have a whitespace for the next iteration */
outputbuf[buff_offset] = c;
buff_offset++;
++nw;
}
} /* End reading each character */
/* This line should indeed print output to console for now */
//fprintf(stderr, "ORIG LINE COUNT: %d\nORIG WORD COUNT: %d\nORIG CHAR COUNT: %d\n", nl, lw, nc);
/* Close our file and clean up */
fclose(ptr_file);
}
return 0;
}
빈 줄이 모두 인쇄됩니다. 나는 다른 버퍼가 필요하다고 생각하지만 실제로는 전혀 모른다. 어떻게 인쇄하고 패딩 된 공백으로 단어 간격을 고르게할까요? 또한 할당 된 너비로 각 줄을 인쇄하는 방법을 잘 모르겠습니다. 어떤 도움이라도 대단히 감사하겠습니다!
total_length + = word_length를 할당하려고하면 다음과 같은 'error : program1.c : 61 : 23 :'오류가 발생합니다 : 선언의 끝에서'+ = '이 잘못되었습니다. '='을 의미 했습니까? size_t total_length + = word_length; ' – freitazm
+ =은 값을 변경할 때만 유효합니다. 'total_length = total_length + word_length'를 의미합니다. '+ ='는 초기화 프로그램에서 유효하지 않습니다. –
모든 도움을 주셔서 대단히 감사합니다! – freitazm