Thread: Formatting output to screen from a file

  1. #1
    Registered User
    Join Date
    May 2006

    Formatting output to screen from a file

    Hi again,

    Working on my final assignemnt still and having issues getting my output to the screen the way it is required.

    I have a text file I output my data to earlier in the program in this required format

    Course Code
    Subject Description
    First Mark
    Second Mark
    Course Code
    Subject Description
    First Mark
    Second Mark

    This is what I attempted in order to read my four fields and print out on one line.

    int printSubject()
    		//declare local variables
    		int i;
    		int fieldCOUNT = 0;
    		char line[31];
    		FILE *fp;
    		//Clears the previous screen and displays a column headers
    		system ("cls");
    		printf("\n\n					Class Mark Sheet Pro Report \n\n");
    		printf("	 Course Code | Subject Name		 | First Mark | Second Mark \n");
    		printf("	 -------------------------------------------------------------------\n");
    		//Opening the file for reading
    		fp = fopen(MarkFile, "r");
    		while (fgets(line, sizeof(line), fp))
    			fieldCOUNT = fieldCOUNT + 1;
                printf("%s		", line);
    			if (fieldCOUNT == 4)
    				fieldCOUNT = 0;
    return 0;
    I get all my data but the out put is not what I had hoped for.

    Any thoughts.

    I do appreciate all the help I have recieved here on the board, I thought I should mention most of classmates have not handed this assignemnt in, or handed it in uncompleted becuase there is alot of functionality required that was not covered in our class. The three main resources I am using for this project are my notes, my text "C Programming a Visual Guide" and this forum.

    I just don't want anyopne to think I am trying to get someone to write my assignment for me.

    Thanks again.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Remember fgets() stores a newline.

    This FAQ shows you how to remove that newline.

    > printf("%s ", line);
    Try something like
    printf("%-10s ", line);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2006
    Hey Salem,


    thanks for that link I got it working better now, just have to play with the spacing between fields now.

    I have abetter understanding of fgets() now as well.

    Thanks for all you help so far today


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM