Formatting output to screen from a file

This is a discussion on Formatting output to screen from a file within the C Programming forums, part of the General Programming Boards category; Hi again, Working on my final assignemnt still and having issues getting my output to the screen the way it ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    16

    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.

    Code:
    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("\n\n");
    		printf("	 Course Code | Subject Name		 | First Mark | Second Mark \n");
    		printf("	 -------------------------------------------------------------------\n");
    
    		//Opening the file for reading
    		fp = fopen(MarkFile, "r");
    
    		//Loop
    		while (fgets(line, sizeof(line), fp))
    		{
    			fieldCOUNT = fieldCOUNT + 1;
                printf("%s		", line);
    
    			if (fieldCOUNT == 4)
    			{
    				printf("\n\n");
    				fieldCOUNT = 0;
    			}
    		}
    
    		getchar();
    		getchar();
    
    return 0;
    I get all my data but the out put is not what I had hoped for.

    http://www.caricsolutions.com/progra...een_output.bmp

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    Remember fgets() stores a newline.

    This FAQ shows you how to remove that newline.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    YOU DA MAN

    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


    clearrtc.

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 Cprogramming.com
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21