Thread: Simple Structure program

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, I wouldn't just put it down entirely to a bug. Sometimes various compilers and systems protect you from yourself. If you compiled what I posted, and it still doesn't work, let me know. Make sure we're compiling the same program and using the same file.

    Edit: Make sure there's a newline at the end of the file you use. Otherwise, you'll use an EOF char.

  2. #17
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Thanks again for the help, so far. When I compile and run this:
    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <string.h>
    
    typedef struct wildroom{
    
            int x;
    
            int y;
    
            char symbol;
    
    } wr;
    
    int main(){
    
            FILE *minimap;
    
            wr wilddata[6][6];
    
            int x,y;
    
           
    
            minimap = fopen("./minimap.txt","r");
    
           
    
            if(minimap==NULL)
    
            {
    
                    printf("Error in File Read\n");
    
                    exit(0);
    
            }
    
           
    
            for(x=0;x<6;x++){
    
                    for(y=0;y<6;y++){
    
                            wilddata[x][y].x=x;
    
                            wilddata[x][y].y=y;
    
                            wilddata[x][y].symbol = fgetc(minimap);
    
                    }
    
            }
    
           
    
            for(x=0;x<6;x++){
    
                    for(y=0;y<6;y++){
    
                            printf("%c",wilddata[x][y].symbol);
    
                    }
    
                    /*printf("\n");
    
                    */
    
            }
    
                           
    
            fclose(minimap);
    
           
    
            return(1);
    
    }
    On a text file that looks like this:

    Code:
    01234
    56789
    abcde
    fghij
    klmno
    It spits out this:

    Code:
    01234
    56789
    abcde
    fghij
    klmno
    ÿ
    That happens in CYGWIN after compiling with gcc. However, when I use the online compiler here:

    http://www.groovyweb.uklinux.net/?pa...ry=onlinetools,

    I get a dos file that runs perfectly and outputs the file perfectly. Any ideas on what could be causing the extra 'garbage'character?

  3. #18
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Shouldn't this line:
    Code:
     for(x=0;x<6;x++){
    read:
    Code:
     for(x=0;x<5;x++){
    If your text file is:
    01234
    56789
    abcde
    fghij
    klmno
    That's only 5 lines, numbered 0-4.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  4. #19
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Quote Originally Posted by Bajanine View Post
    Shouldn't this line:
    Code:
     for(x=0;x<6;x++){
    read:
    Code:
     for(x=0;x<5;x++){
    If your text file is:


    That's only 5 lines, numbered 0-4.
    The recommended change ends me up with:

    Code:
    01234
    56789
    abcde
    fghi
    And that's it. I lose one character on the fourth line, and the entire fifth line. Again, when I compiled it 'as-is' it runs fine under dos. If you look a bit higher in the thread, it runs fine on another's linux system.

    Is anyone else using CYGWIN, or familiar with this type of problem?

  5. #20
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Don't blame cygwin. Many people use it, and I seriously doubt it has anything to do with this.

    When you're new, you should pick a decent compiler and then be absolutely against the notion of your compiler being the source of any problems. Put the blame on yourself, because you'll probably be the source of the trouble 99&#37; of the time.

    Did you make sure that your file has an extra '\n' in it like I stated?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 07:40 AM
  5. Simple program structure.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-15-2002, 04:36 AM