I've been trying to get back into writing C, and wrote this short program to print any line with length greater than LENGTH. The code is simple, but produces some strange output. The source follows:
Code:
#include <stdio.h>
/*
* Program to display all lines with length greater than LENGTH chars
*/
/*
* Enter each character into an array, ending at \n while counting the number of non-white space characters. If this line exceds LENGTH chars, print the array before blanking the array and restarting.
*/
#define LENGTH 80
#define MAX_LINE 1024
main() {
int c;
int i;
int char_count;
int cur_length;
int cur_line[MAX_LINE];
i=0;
c=0;
char_count=0;
cur_length=0;
while((c=getchar()) != EOF) {
++cur_length;
cur_line[cur_length-1]=c;
if (c=='\n') {
if(char_count>=LENGTH) {
for(i=0;i<=MAX_LINE;++i) {
printf("%c", cur_line[i]);
}
}
char_count=0;
for(i=0;i<=cur_length;++i) {
cur_line[i]=0;
}
cur_length=0;
}
else if (c != ' ' || c != '\t') {
++char_count;
}
}
}
and the output:
Code:
* Enter each character into an array, ending at \n while counting the number of non-white space characters. If this line exceds LENGTH chars, print the array before blanking the array and restarting.
�H���(p2����p��f$�/H<H�o�8l
�
�H���D��`
D8hHS4 hh�r,Hr������p�re� H����e�8`K�`
h`�4t4
C444@@������p�����ttt ,PDDQK�HT������Hr���|�l�"��HT�H���`��H����� h�X�L���b��
XL�t����p������>���� ��t|��`t|���h�P���������(`| P��$�����0U� ��H��\�Ch�TTht|���?8HD��4��.Hq��\H-���. ���Ph-�l���
I've reinstalled gcc to (hopefully) make sure it wasn't the issue, catted the file from one file to another (futile attempt to try and clean the file), opened, modified, then saved the file from vim (was coded first in leafpad then scite) to see if it was something specific that one of the programs left behind, and even copied the source into a new file via cut+paste, with not change.
Anyone have an idea what the problem is?
Edit:
This output is send after the end of the file (when printf("%c",c) is placed after cur_line(cur_length[i-1]=c), it shows the final } before the troublesome output).