Welcome to Dream.In.Code
Click Here
Getting C++ Help is Easy!

Join 117,523 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 2,053 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Formatting output to file in C

 
Reply to this topicStart new topic

Formatting output to file in C

webmin
post 3 Jul, 2008 - 09:54 AM
Post #1


D.I.C Head

**
Joined: 21 May, 2008
Posts: 95


My Contributions


Hi guys, I've run into a bit of a problem when it comes to outputting my info into a txt file.

I want to print a title so that people know what they're are looking at when they open this file my program creates. However, I have several rows/columns of info to be printed to file, and it seems to want to print the title with each row.

This is what my program is doing now:
CODE

Welcome to the payroll journal.0101 41 8.11 49 Y 397.39

Welcome to the payroll journal.0722 32 7.22 40 N 288.80

Welcome to the payroll journal.1273 23 5.43 39 Y 211.77

Welcome to the payroll journal.2584 14 6.74 45 N 303.30

Welcome to the payroll journal.4885 21 8.00 38 Y 304.00

! Note this is the output file

This is what I want my program to print out:
CODE

Welcome to the payroll journal.
0101 41 8.11 49 Y 397.39

0722 32 7.22 40 N 288.80

1273 23 5.43 39 Y 211.77

2584 14 6.74 45 N 303.30

4885 21 8.00 38 Y 304.00

I'd also like to print labels to each of the columns, but that's not as important.

I have the code that I'm using for this here:
CODE

/*=========================Write Payroll Journal=======================
    Finally, we will create a file for the payroll journal
*/
int createpay (FILE* payjournal, int empnum, int dept,
               float payrt, char exmpt, int hours, float paygross)
{
//Statements
    fprintf(payjournal, "Welcome to the payroll journal.");
    fprintf(payjournal, "%04d %2d %.2f %2d %c %.2f\n\n",
        empnum, dept, payrt, hours, exmpt, paygross);
    printf("%04d\t\t %2d\t$%.2f\t %2d\t  %c\t $%.2f\n\n", empnum, dept, payrt, hours, exmpt, paygross);

    return 0;
}


If someone can please point out what I'm doing wrong and show me the solution specifically in C (not C++), I'd appreciate it.

User is offlineProfile CardPM

Go to the top of the page


Cerolobo
post 3 Jul, 2008 - 10:49 AM
Post #2


D.I.C Regular

Group Icon
Joined: 5 Apr, 2008
Posts: 440



Thanked 30 times
My Contributions


Well, you are missing a new line

CODE
fprintf(payjournal, "Welcome to the payroll journal.\n");


Other then that, it seems you are calling createpay() multiple times, to write each record. If that is the case, then it will add "Welcome to the payroll journal.\n" each time.

If you only want that to show up once, the easy thing to do, would be to move that fprintf() to right after you open the file. IE, you open the file, check to see if the file pointer is valid, write "Welcome to the payroll journal.\n", and then call createpay() for each record.

As for the column titles, you would basically put something like this under the "Welcome to the payroll journal.\n" fprintf().

CODE
fprintf(payjournal, "empnum dept payrt hours exmpt paygross");


and then you'll have to fix your alignment in createpay()

CODE
fprintf(payjournal, "%06d %4d %5.2f %5d   %c   %8.2f\n\n",
        empnum, dept, payrt, hours, exmpt, paygross);


This post has been edited by Cerolobo: 3 Jul, 2008 - 10:54 AM
User is offlineProfile CardPM

Go to the top of the page

webmin
post 3 Jul, 2008 - 05:59 PM
Post #3


D.I.C Head

**
Joined: 21 May, 2008
Posts: 95


My Contributions


Thanks for your help. that's what I needed.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/7/08 03:17PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month