![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 15
| Printf not printing all data I have this function reading the data. Code: void GetFile (int* EmployNum1, int* Dep1, float* PayRte1, char* Exempt1, int* Hours1, int* EmployNum2, int* Dep2, float* PayRte2, char* Exempt2, int* Hours2, int* EmployNum3, int* Dep3, float* PayRte3, char* Exempt3, int* Hours3, int* EmployNum4, int* Dep4, float* PayRte4, char* Exempt4, int* Hours4)
{
FILE* spData;
spData = fopen("C:\\employeefile.txt", "r");
if (spData == NULL)
{
printf("ERROR\n");
exit (101);
}
while ((fscanf(spData, "%4d%2d%4f%1c%2d%4d%2d%4f%1c%2d%4d%2d%4f%1c%2d%4d%2d%4f%1c%2d", EmployNum1, Dep1, PayRte1, Exempt1, Hours1, EmployNum2, Dep2, PayRte2, Exempt2, Hours2, EmployNum3, Dep3, PayRte3, Exempt3, Hours3, EmployNum4, Dep4, PayRte4, Exempt4, Hours4)) == 1);
Code: printf("%4d %2d %4f %1c %2d", EmployNum1, Dep1, PayRte1, Exempt1, Hours1);
So what's the problem here? |
| murjax is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| What do you want to have happen? Right now I would guess you're reading in four groups of things and printing out one. fscanf returns the number of %<whatever> that are matched, so the odds of you actually getting a one, given your format string, are very very low. |
| tabstop is offline | |
| | #3 | |
| Eternally Earnest Join Date: Jul 2008 Location: SE Queens
Posts: 4,608
| Quote:
__________________ BEING HELPFUL SHOULD MEAN SHARING KNOWLEDGE, NOT WITHHOLDING IT. ==mk27 ![]() A truly creative person rids him or her self of all self-imposed limitations. ==recent fortune cookie | |
| MK27 is offline | |
| | #4 |
| Registered User Join Date: Jul 2009
Posts: 15
| Well here's the thing, it prints the first 3 items, skips the character item after it, and then prints a few random numbers for the 5th item which was supposed to be a 2 integer number. |
| murjax is offline | |
| | #5 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,407
| > So what's the problem here? That the while loop in your function has a ; at the end of it. So it zooms to the end of the file (or whenever fscanf returns 1 - see MK27's post), and leaves garbage data in anything it doesn't assign. Oh, and learn about structs, so you can pass 1 pointer to a struct, and not 20 pointers to individual data items. Also, formatting. You don't need everything on one line. Code: fscanf(spData, "%4d %2d %4f"
"%1c%2d %4d"
"%2d %4f%1c"
"%2d %4d %2d"
"%4f%1c%2d"
"%4d %2d %4f"
"%1c%2d",
EmployNum1, Dep1, PayRte1,
Exempt1, Hours1, EmployNum2,
Dep2, PayRte2, Exempt2,
Hours2, EmployNum3, Dep3,
PayRte3, Exempt3, Hours3,
EmployNum4, Dep4, PayRte4,
Exempt4, Hours4);
|
| Salem is offline | |
| | #6 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,664
| Makes sense. You're feeding it garbage, it's printing garbage. The problem is not printf() but how you are reading the data in the first place.
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #7 |
| Registered User Join Date: Jul 2009
Posts: 15
| |
| murjax is offline | |
| | #8 | |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,013
| Quote:
| |
| bithub is offline | |
| | #9 |
| Registered User Join Date: Jul 2009
Posts: 15
| |
| murjax is offline | |
| | #10 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Well, it changes a lot of things, in the sense that everything (that you've told us about) now works. Apparently you still have some other bug, but you haven't told us about that one yet. |
| tabstop is offline | |
| | #11 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,407
| But posting your latest code might.... |
| Salem is offline | |
| | #12 |
| Registered User Join Date: Jul 2009
Posts: 15
| Here's the current piece of code. Code: void GetFile (int* EmployNum1, int* Dep1, float* PayRte1, char* Exempt1, int* Hours1, int* EmployNum2, int* Dep2, float* PayRte2, char* Exempt2, int* Hours2, int* EmployNum3, int* Dep3, float* PayRte3, char* Exempt3, int* Hours3, int* EmployNum4, int* Dep4, float* PayRte4, char* Exempt4, int* Hours4)
{
FILE* spData;
spData = fopen("C:\\employeefile.txt", "r");
if (spData == NULL)
{
printf("ERROR\n");
exit (101);
}
while ((fscanf(spData, "%4d%2d%4f%1c%2d%4d%2d%4f%1c%2d%4d%2d%4f%1c%2d%4d%2d%4f%1c%2d", EmployNum1, Dep1, PayRte1, Exempt1, Hours1, EmployNum2, Dep2, PayRte2, Exempt2, Hours2, EmployNum3, Dep3, PayRte3, Exempt3, Hours3, EmployNum4, Dep4, PayRte4, Exempt4, Hours4)) == 20)
return;
}
|
| murjax is offline | |
| | #13 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,368
| How many lines are there in the input file? Is the return statement a part of the while loop? |
| itCbitC is offline | |
| | #14 |
| Registered User Join Date: Jul 2009
Posts: 15
| |
| murjax is offline | |
| | #15 | |
| Eternally Earnest Join Date: Jul 2008 Location: SE Queens
Posts: 4,608
| Quote:
You should throw a printf, including the return value of fscanf, into the while loop so you can see what actually is happening.
__________________ BEING HELPFUL SHOULD MEAN SHARING KNOWLEDGE, NOT WITHHOLDING IT. ==mk27 ![]() A truly creative person rids him or her self of all self-imposed limitations. ==recent fortune cookie | |
| MK27 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| I have some questions :( | geekrockergal | C Programming | 19 | 02-01-2009 09:44 AM |
| whats wrong with this? | petedee | C Programming | 32 | 01-06-2004 10:28 PM |
| error in program???? | SpEkTrE | C Programming | 5 | 11-24-2003 06:16 PM |
| Contest Results - May 27, 2002 | ygfperson | A Brief History of Cprogramming.com | 18 | 06-18-2002 01:27 PM |
| Warnings, warnings, warnings? | spentdome | C Programming | 25 | 05-27-2002 06:49 PM |