這是一個C作業。我沒有要求任何人爲我這樣做,我只是碰壁了。這是明天到期的,我不知道如何繼續。我是一個初學者,我的頭開始受傷C編程assignmnt help:讀入並打印格式化
編寫一個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:'error:invalid'+ =';你的意思是'='? size_t total_length + = word_length;' – freitazm
+ =僅在更改值時有效。它意味着'total_length = total_length + word_length'。 '+ ='在初始化程序中無效。 –
非常感謝你的幫助! – freitazm