Instructions: Trailing blanks and tabs are defined as blanks and tabs that precede a '\n' character or an EOF to terminate a line, without any intervening characters of other kinds. You should copy a getline() function of the kind you see in the text to read in lines from stdin into an array, trim the line, and then write out the trimmed line to stdout, continuing until you encounter an EOF.
Note: If there are 4 non-blank lines in the input to trim, there should be 4 lines in the output, but lines consisting of only blanks, and tabs, should NOT be output. The trim function should not affect any blanks or tabs that do not come at the end of the line.
I have a runtime error. I octal dumped the input file and used the input file with the code I wrote to produce an output file and then octal dumped it. However, my octal dump of my out file is completely wrong! Somewhere in my program there is a non terminating loop; because I had to control C.
Here is the execution:
blade71(136)% gcc trim_3.c
blade71(137)% od -x trim.in
0000000 0909 4e6f 7720 6973 2074 6865 2020 2020
0000020 2020 200a 0909 5469 6d65 2066 6f72 0909
0000040 0a09 0961 6c6c 2067 6f6f 6420 6d65 6e20
0000060 746f 2020 2020 0a09 0963 6f6d 6520 746f
0000100 2074 6865 2061 6964 206f 6620 7468 6520
0000120 7061 7274 792e 2020 2020 2020 0a0a
0000136
blade71(138)% gcc -o trim trim_3.c
blade71(144)% dir
In_Files core trim trim.c trim.in trim_2.c trim_3.c
blade71(145)% trim<trim.in>trim.out
^C
blade71(146)% od -x trim.out
0000000 ffff ffff ffff ffff ffff ffff ffff ffff
*
12260000
Here is the final source:
blade71(171)% cat trim_3.c
#include<stdio.h>
#define MAXLINE 1000
int getline (char line[], int maxline);
int copy_NW(char to[], char from [], int NWS);
main ()
{
int NL, WS, len;
char line[MAXLINE];
char line_NW[MAXLINE];
while ((len = getline(line, MAXLINE))>0)
{
WS++;
}
while(WS>1)
{
copy_NW(line, line_NW, WS);
}
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i<lim -1 && (c=getchar())!= EOF && c!='\n'; ++i)
s[i]= c;
if (c=='\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int copy_NW(char line[], char line_NW[], int NL)
{
int c, d;
int i = 0;
while(NL != 0 && (c=getchar())!=EOF)
line[i] = line_NW[i];
if(c==' ' || c=='\n' || c=='\t')
NL--;
d=putchar(c);
line_NW[i] = d;
return d;
}