Thread: Ignoring New Line Characters

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Tucson, AZ
    Posts
    1

    Ignoring New Line Characters

    I was making a small program that loads a simple map from a text file and places it on a 5x5 grid. In the text file, 0 represents grass and 1 represents dirt.

    I have a function in my map.h file that takes characters from a txt file and places them into a 5x5 array like so:

    Code:
    #define T_SIZE 5
    
    char t_type[T_SIZE][T_SIZE];
    
    //Load Terrain
    bool LoadTerrain(char *t_file) {
         //Pointer to the file to be loaded
         FILE *fp;
         
         //Open the file
         if((fp=fopen(t_file,"r"))==NULL) return false;
         
         //Loop through each tile
         for(int a=0;a<T_SIZE;a++) {
                 for(int b=0;b<T_SIZE;b++) {
                         //Store the current character in the next spot in the array
                         t_type[a][b] = (char)fgetc(fp);
                 }
         }
         
         //Close the file!
         fclose(fp);
         
         return TRUE;
    }
    Now, everything works how it should when my text file reads:
    Code:
    0000000000001000000000000
    Which places grass with one square of dirt in the center.

    But when I try:
    Code:
    00000
    00000
    00100
    00000
    00000
    The dirt is no longer in the middle because the invisible \n character at the end of every line is treated as part of the map.

    My question is, how can I make the function ignore the \n character?

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Spencer Wallace
    My question is, how can I make the function ignore the \n character?
    For a start, you should be checking the return value of fgetc() and not just blindling feeding it to your array. This way you can (a) make sure you don't hit EOF prematurely and (b) check if it's a '\n' and ignore it.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Code:
        for(int a=0;a<T_SIZE;a++) 
        {
            for(int b=0;b<T_SIZE;b++) 
            {
                //Store the current character in the next spot in the array
                int ch = fgetc(fp);
                if (ch != '\n')
                {
                    t_type[a][b] = (char)ch;
                }
            }
        }
    You'll have to deal with your index values differently.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Very strange error...
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 01:37 AM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  5. Ignoring the Tab at the begining of the line..
    By NANO in forum C++ Programming
    Replies: 8
    Last Post: 05-03-2003, 10:56 PM