Thread: Dealing with whitespaces whilst reading a file

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    Dealing with whitespaces whilst reading a file

    The aim of my code is to read in a code in a text file and discard comments, i.e. any characters after '#'. So for example, line like this:
    Code:
    main: addi  $3,$0,40   # j  = 40
    is transformed into:
    Code:
    main:addi$3,$0,40
    I have written a code that does just that. However, if the line only contains a comment I end up with an array of empty characters. Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define LEN 60
    
    void comment_remove (char word[LEN])
    {
        char temp[LEN];
        int i, j;
        for (i = 0; i < LEN; i++){
            if (word[i] != '#'){
                temp[i] = word[i];
            }else{
                temp[i] = '\0';
            }
        }
        i = 0, j = 0;
        
        while (i < LEN){
            if (temp[i] != ' '){
                word[j] = temp[i];
                i++; 
                j++;
            }else{
                i++;
            }
        }
    }
    
    static FILE *open_file (char *file, char *mode) 
    {
        FILE *fptr = fopen (file, mode); 
    
        if (fptr == NULL){			
            perror ("Unable to open file");	
            exit (EXIT_FAILURE);
        }
        return fptr;	
    }
        
    int main (int argc, char *argv[])
    {
        int array_pos = 0;
        char letter, word[LEN];
        FILE *fptr;
        fptr = open_file (argv[1], "r");
        do{
            letter = fgetc (fptr);
            if (letter != '\n'){
                word[array_pos] = letter;
                array_pos++;
            }else{
                word[array_pos] = '\0';
                array_pos = 0;
                comment_remove (word);
                printf ("%s\n", word);
            }
        } while (letter != EOF);
        fclose (fptr);
        return 0;
    }
    So for example, text file like this:
    Code:
                           # register allocation:
                           # 
                           # $3 = j
                           # $4 = a = i - 0
                           # $5 = b = i - 1
                           # $6 = c = i - 2
                           # $7 = t1
                           # $8 = t2
                           # $9 = t3
    
    main: addi  $3,$0,40   # j  = 40
    comes out like this:
    Code:
    
    
    
    
    
    
    
    main:addi$3,$0,40
    My question is how can I deal with the empty arrays in the beginning of the file?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgetc returns int - so make letter int
    Check for EOF before processing the character


    why not to read file using fgets line by line, and if th efirst char read is # just skip to reading the next line?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM