写一个文本格式化程序可以处理一个输入文件,它按照用户指定的下列版面规格参数重排版面:实现页内调整、分段、分页等文本处理功能,排版结果存入输出文本文件中。
(1)输出文件中字与字之间只留一个空格符,即实现多余空格符的压缩。
(2)输出文件中,任何完整的字不能分割在两行,行尾可以不齐,但行首要对齐。
(3)将页号印在页底空行中第2行的中间位置上。
(4) 版面要求的参数要包含:
页长(Page Length)——每页内文字(不计页号)的行数。
页宽(Page Wedth)——每行内文字所占最大字符数。
左空白(Left Margin)——每行文字前的固定格数。
头长(Heading Length)——每页页顶所空行数。
脚长(Footing Length)——每页页底所空行数(含页号行)
起始页号(Starting Page Number)——首页的页号。
我写的程序老是段错误,但代码看了好多遍看不出问题出在哪里,请大家指教:
#include<stdio.h>
#define WORD_MAX 20
int main()
{int foot_length;
int head_length;
int page_wedth;
int page_length;
int left_margin;
char start_num;
int c;
char word[WORD_MAX];
int i;
int j;
int char_counter=0;
int line_counter=0;
foot_length=3;
head_length=2;
page_wedth=70;
page_length=5;
left_margin=2;
start_num=1+'0';
FILE *fp1=NULL;
FILE *fp2=NULL;
fp1=fopen("old","r");
fp2=fopen("formatone","w");
for(j=0;j<head_length;j++) //输出头长
fputc('\n',fp2);
for(j=0;j<left_margin;j++) //输出左空白
{fputc(' ',fp2);
char_counter++;
}
c=fgetc(fp1);
while(c!=EOF)
{
i=0;
while(c==' ' || c=='\n')
c=fgetc(fp1);
while(c!=' ' && c!='\n') //将一个单词放入数组word[]
{word[i++]=c;
c=fgetc(fp1);
}
if(i<=page_wedth-char_counter) //如果剩余长度足够输出单词,则输出
{for(j=0;j<i;j++)
{fputc(word[j],fp2);
char_counter++;
}
fputc(' ',fp2);
char_counter++;
}
else //剩余长度不够输出单词
{
fputc('\n',fp2);
line_counter++;
char_counter=0;
if(line_counter<page_length) //此页未结束。
{ for(j=0;j<left_margin;j++)
{fputc(' ',fp2);
char_counter++;
}
for(j=0;j<i;j++)
{fputc(word[j],fp2);
char_counter++;
}
fputc(' ',fp2);
char_counter++;
}
else //此页已结束。
{fputc('\n',fp2);
for(j=0;j<(page_wedth/2);j++)
fputc(' ',fp2);
fputc(start_num++,fp2);
for(j=2;j<foot_length;j++)
fputc('\n',fp2);
line_counter=0;
for(j=0;j<head_length;j++)
fputc('\n',fp2);
for(j=0;j<left_margin;j++)
{fputc(' ',fp2);
char_counter++;
}
for(j=0;j<i;j++)
{fputc(word[j],fp2);
char_counter++;
}
fputc(' ',fp2);
char_counter++;
} //small else,此页已结束那个。
} //big else
c=fgetc(fp1);
} //while(c!=EOF)
printf("its the end!\n");
fclose(fp1);
fclose(fp2);
return 0;
}
gcc可以编译,但执行时显示“段错误”。gdb提示
Program received signal SIGSEGV, Segmentation fault.
0x08048756 in main () at main.c:44
44 {word[i++]=c;
(gdb)
另,若在small else那个}之前加break;让它执行完就跳出,可以正常输出第一页和第二页的第一个单词。
附:我old文本的内容:
The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.
In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.
To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.
In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.
To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.
In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.
To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.
In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.
To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.
In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.
To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.
In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.
To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.
In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.
To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.
In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.
To most people in the United States, silence The use of silence