fprintf behaving strangely

This is a discussion on fprintf behaving strangely within the C++ Programming forums, part of the General Programming Boards category; I have an array with 1000 entries that I try to print to a file. However, only 717 make it ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    fprintf behaving strangely

    I have an array with 1000 entries that I try to print to a file. However, only 717 make it into the file, with the 718th chopped in half. However, when I switch fprintf to printf (removing the file parameter as well), the command prompt window prints all 1000 entries with no issue. Any ideas?

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you closing the file?

    Can you post a simple example of it not working?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    I'm closing the file, here's the print loop:

    Code:
    for(q=0;q<1000;q++)
    	{
    		fprintf(variancef,"%d: %s\n",q,goodslab[q]);
    	}
    prints:
    0: 211430_s_at
    1: 206509_at
    2: 215121_x_at
    3: 221671_x_at
    ...
    752: 200738_s_at
    753: 201540_at
    754: 218237_s_at
    755:

    Also, I noticed that adding a second "\n" after the first one results in 948 out of 1000 entries being printed to the file.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    We probably need a complete compilable sample of it going wrong. If you can't produce a 20-30 line piece of code to reproduce the problem, then it's most likely in your code itself that the problem lies.

    The change of behaviour with a second newline indicates that you are probably encountering "undefined behaviour".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    You could try adding an fflush command after each fprintf to see if the output file does/does not contain all the necessary data (this will slow things down so only keep it there for testing).

    Also, this is the C++ board, why are you using fprintf here?
    Last edited by hk_mp5kpdw; 07-17-2008 at 08:28 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    I'm going to guess that goodslab[x] is an array of 6 chars.

    So writing 6 chars loses the \0 and garbage is appended.

    So maybe writing 7 chars really trashes something (say the FILE *fp used with fprintf) and it all falls apart.
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's one possibility [although I think 6 should be replaced with a tad larger, as the lables are about 9 characters already]. I was also considering "return address of local array variable" as one of the possible scenarios.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  3. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 11:55 AM
  4. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 09:51 AM
  5. fprintf
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 11-16-2002, 06:58 PM

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