Thread: Printf not printing all data

  1. #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?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    == 1);
    fscanf returns the number of data items successfully read, which it looks like that will be not 1.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #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?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What's the garbage?
    Read Salem's post; he explains where the garbage is coming from.

  9. #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.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #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.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    How many lines are there in the input file? Is the return statement a part of the while loop?

  14. #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.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I have some questions :(
    By geekrockergal in forum C Programming
    Replies: 19
    Last Post: 02-01-2009, 09:44 AM
  2. whats wrong with this?
    By petedee in forum C Programming
    Replies: 32
    Last Post: 01-06-2004, 10:28 PM
  3. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM