C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2009, 09:00 PM   #1
Registered User
 
Join Date: Jul 2009
Posts: 15
Printf not printing all data

The data in this program comes from a text file. It seems to read fine but when I try to print it only a portion of the data prints. I think I'm overlooking something but I can't put my eye on it.

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);
While I have this printf statement in main printing.

Code:
printf("%4d %2d %4f %1c %2d", EmployNum1, Dep1, PayRte1, Exempt1, Hours1);

So what's the problem here?
murjax is offline   Reply With Quote
Old 07-10-2009, 09:04 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 07-10-2009, 09:05 PM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
== 1);
fscanf returns the number of data items successfully read, which it looks like that will be not 1.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 07-10-2009, 10:17 PM   #4
Registered User
 
Join Date: Jul 2009
Posts: 15
Quote:
Originally Posted by MK27 View Post
fscanf returns the number of data items successfully read, which it looks like that will be not 1.
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   Reply With Quote
Old 07-10-2009, 10:57 PM   #5
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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);
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-11-2009, 11:50 AM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,379
Quote:
Originally Posted by murjax View Post
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.
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   Reply With Quote
Old 07-17-2009, 01:55 PM   #7
Registered User
 
Join Date: Jul 2009
Posts: 15
Quote:
Originally Posted by brewbuck View Post
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.
What's the garbage? The contents of the file?
murjax is offline   Reply With Quote
Old 07-17-2009, 01:58 PM   #8
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
What's the garbage?
Read Salem's post; he explains where the garbage is coming from.
bithub is offline   Reply With Quote
Old 07-20-2009, 10:09 AM   #9
Registered User
 
Join Date: Jul 2009
Posts: 15
Quote:
Originally Posted by bithub View Post
Read Salem's post; he explains where the garbage is coming from.
Removing the ; from the while statement and changing 1 to the number of items being read (20 in this case) doesn't change a thing.
murjax is offline   Reply With Quote
Old 07-20-2009, 10:20 AM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 07-20-2009, 10:22 AM   #11
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
But posting your latest code might....
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-20-2009, 12:19 PM   #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;
}
I know I haven't structured it properly yet, but I just want to see it work first. If the problem isn't in here then I guess I'll post the whole code even though I thought the rest worked fine.
murjax is offline   Reply With Quote
Old 07-20-2009, 12:24 PM   #13
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
How many lines are there in the input file? Is the return statement a part of the while loop?
itCbitC is offline   Reply With Quote
Old 07-20-2009, 12:45 PM   #14
Registered User
 
Join Date: Jul 2009
Posts: 15
Quote:
Originally Posted by itCbitC View Post
How many lines are there in the input file? Is the return statement a part of the while loop?
There are 4 lines and no, the return isn't supposed to be part of the loop, which is why I originally had a ; at the end of the last line.
murjax is offline   Reply With Quote
Old 07-20-2009, 01:01 PM   #15
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by murjax View Post
There are 4 lines and no, the return isn't supposed to be part of the loop, which is why I originally had a ; at the end of the last line.
Whoops!

You should throw a printf, including the return value of fscanf, into the while loop so you can see what actually is happening.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:12 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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