Thread: Strange characters in output to terminal

  1. #1
    Registered User
    Join Date
    Aug 2009

    Strange characters in output to terminal

    I have a text file with ManchesterUtd01Reading, I open it for reading, count the number of characters on the first line and use it in a loop, the loop will insert a single character in an array called team while the character is not a number. Then I try to print out what is in array team.

    I only get gobble, like
    Team 1 is: ܃����ȧ��I��g����(���u���0���(���u���
    Is this because the characters are not going in to the array, so it is printing something random from memory? I am just printing to the terminal in Ubuntu and my text file is in Gedit.

    #include <stdio.h>
    #include <string.h>
    int main()
    	FILE *fp;
    	char string[40];  /*This is to store the first line of the text file as a string */
    	int length;       /* This is to store the length of the string above to use in a loop */
    	char team1[20];
    	char team2[20];
    	char score[2];
    	char c;   /* Store single character from file, used in a loop */
    	fp = fopen("teamresults.txt", "r");
    	fgets(string,40,fp);   /* Read up to 40 characters from text file and put in string array */
    	length= strlen(string);  /* Count how many characters are in the text file up to 40 or new line or EOF */
    	c = fgetc( fp ) ;   /* fgetc function gets a character from file specified by what fp points to and stores it in c */
    	while (  c != EOF )/* while not at the end of the file, get a character from the file */
    		int i;
    		for (i=0;length;++i) {
    			c = fgetc( fp ) ; 
    				while ( c >64 && c <123) {   /* while character from text file is a non-number character */
    	printf("Team 1 is: %s",team1);
    	fclose (fp);

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    in front of my computer
    Are you terminating your string team1 with the null character? like

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior

    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    I would suggest you run your program through a debugger, and watch what is happening with your variables as you step through it. That should be fairly instructive for you, as you have a number of programming logic problems with this code.

    Here are a few suggestions to get you started:

    1) Where is fp pointing after the call to fgets? Is it where you want it?

    2) What does this do in your loop?
    for (i=0;length;++i)
    3) What does this do?
    while ( c >64 && c <123)
    (Hint: if the line of text was "Manchester United", would the test always be true? If so, what would that do?)

    BTW, try to use character constants, rather than decimal values. 64 would become '@' and 123 would become '{'. Alternatively, you could check out the functions available to you in <ctype.h>

    4) See BEN10's post above.
    Last edited by kermit; 08-30-2009 at 06:47 AM.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Ok I have some things to think about, thanks for the replies.

    I've started using gdb to debug, I've never done it before and it's very interesting.

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    You must do checks to make sure you don't overrun the buffers!
    And "magical numbers" such as 64 shouldn't be used. If you want to know if it's a character, then use functions such as isalpha.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange output for class
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 11-05-2006, 04:22 PM
  2. Adding extra characters to output
    By COBOL2C++ in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2003, 08:12 AM
  3. strange output
    By volk in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 11:09 AM
  4. output of characters
    By deleeuw in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:17 PM