LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-13-2009, 04:50 AM   #1
Dralnu
Member
 
Registered: Jul 2005
Distribution: Arch Linux
Posts: 328

Rep: Reputation: 32
Strange output when running a C program over itself


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).

Last edited by Dralnu; 05-13-2009 at 04:52 AM.
 
Old 05-13-2009, 05:46 AM   #2
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 51
Try this:
Code:
#define LENGTH      80
#define MAX_LINE    1024

int main(void) {

int c;
int char_count;
int cur_length;

char cur_line[MAX_LINE];

char_count=0;
cur_length=0;

    while(((c=getchar()) != EOF) && (cur_length < MAX_LINE)) {

        cur_line[cur_length]=c;
        ++cur_length;

        if (c=='\n') {
            if(char_count>=LENGTH) {
                cur_line[char_count] = '\0';
                printf("%s\n", cur_line);
            }
            cur_length=0;
            char_count=0;
        }
        else if (c != ' ' || c != '\t') {
            ++char_count;
        }

    }
}
I get this:
Quote:
* 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.
Seems correct.

Basically you were printing whole 1024 char array, which is wrong. You only need to print cur_length length string.

Last edited by Alien_Hominid; 05-13-2009 at 05:54 AM.
 
Old 05-13-2009, 01:08 PM   #3
Dralnu
Member
 
Registered: Jul 2005
Distribution: Arch Linux
Posts: 328

Original Poster
Rep: Reputation: 32
So the garbage output was what had filled the rest of the array, but wasn't something the terminal could translate into human-readable format? That makes sense.
 
Old 05-13-2009, 01:19 PM   #4
Dralnu
Member
 
Registered: Jul 2005
Distribution: Arch Linux
Posts: 328

Original Poster
Rep: Reputation: 32
I just ran your version (had to include #include <stdio.h> at the beginning, however), and it produced output when run like:

./a.out < file.c

And this was when cut and pasted.

However, I replaced MAX_LENGTH with cur_length in the for loop, and it works fine now. Why printf("%s",array) doesn't seem to do what it is supposed to is beyond me (cc (Debian 4.3.3-8) 4.3.3), even though thats the method they are using in K&R

Last edited by Dralnu; 05-13-2009 at 01:22 PM.
 
Old 05-13-2009, 04:11 PM   #5
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 51
String must end with \0 (zero byte) in array in order for printf to work. Google for ASCIIZ/C string.

Last edited by Alien_Hominid; 05-13-2009 at 04:12 PM.
 
Old 05-13-2009, 04:15 PM   #6
Dralnu
Member
 
Registered: Jul 2005
Distribution: Arch Linux
Posts: 328

Original Poster
Rep: Reputation: 32
thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
After running my program, it fails and gives my terminal strange characters that I ca RHLinuxGUY Programming 4 08-31-2006 04:24 AM
Strange output from C program Gins Programming 8 07-11-2006 10:29 PM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
Running a program inside my QT application does not produce needed output files??? keos Programming 0 02-22-2004 11:37 PM


All times are GMT -5. The time now is 05:29 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration