Results 1 to 3 of 3

Thread: C Program that prints formated table

  1. #1
    Join Date
    Dec 2008
    Location
    UK
    Beans
    438
    Distro
    Ubuntu 10.04 Lucid Lynx

    C Program that prints formated table

    I have this code:
    Code:
    #include <stdio.h>
    #include "useful_functions.h"
    
    int main(int argc, char *argv[]){
    	//[row][column][chars]
    	char table[300][20][200];
    	//longest entry in column
    	int max[20];
    	int row,column,chars;
    	row = column = chars = 0;
    	int ocolumn;
    
    	FILE *fp = fopen(*++argv, "r");
    	char c;
    
    	if (fp != NULL){
    		while ((c=getc(fp)) != EOF){
    			if (c == '\n'){
    				row++;
    				chars = 0;
    				ocolumn = column;
    				column = 0;
    			}else if (c == ' '){
    				int length = strlen(table[row][column]);
    				if (length > max[column]){
    					max[column] = length;
    				}
    				column++;
    				chars = 0;
    			}else{
    				table[row][column][chars++] = c;
    			}
    		}
    		int i,j;
    		for (i=0;i < row; i++){
    			for (j=0;j <= ocolumn;j++){
    				printf("| %s ", table[i][j]);
    				pad(max[j] - strlen(table[i][j]));
    			}
    			printf("|\n");
    		}
    	}else{
    		printf("Could not open file\n");
    	}
    }
    "useful_functions":
    Code:
    void pad(int numofspaces){
    	int i;
    	for (i=0;i<numofspaces;i++){
    		printf(" ");
    	}
    }
    when passed this file:
    Code:
    Name Score
    1 1
    2 3
    it works, apart from it doesn't pad the second field.
    When passed this file:
    Code:
    Name Age Score
    Matthew 100 12
    it prints out loads of spaces/newlines...

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: C Program that prints formated table

    learn to use a debugger to solve problems yourself!
    you can't come to this forum for every single program which doesn't work on first try.

    also you don't have to pad by hand, printf can already do that
    http://www.cplusplus.com/reference/c...cstdio/printf/

  3. #3
    Join Date
    May 2006
    Beans
    1,787

    Re: C Program that prints formated table

    Quote Originally Posted by matmatmat View Post
    I have this code:
    Code:
    #include <stdio.h>
    #include "useful_functions.h"
    
    int main(int argc, char *argv[]){
    	//[row][column][chars]
    	char table[300][20][200];
    	//longest entry in column
    	int max[20];
    	int row,column,chars;
    	row = column = chars = 0;
    	int ocolumn;
    
    	FILE *fp = fopen(*++argv, "r");
    	char c;
    
    	if (fp != NULL){
    		while ((c=getc(fp)) != EOF){
    it prints out loads of spaces/newlines...
    I'll point out one thing which won't help you at all right now: as far as I know, C allows 'char' to be either signed or unsigned - it's up to the implementation. If unsigned, your code won't work, because then c can't be equal to -1. If you look at the documentation for getc, you will see that it returns an int, so that's what the type of c should be.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •