Sequence Generator - Starting from zero?

This is a discussion on Sequence Generator - Starting from zero? within the C Programming forums, part of the General Programming Boards category; Good evening! I'm trying this code to generate a sequence of 8 digit numbers starting from 00000000 to 99999999. With ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Question Sequence Generator - Starting from zero?

    Good evening!

    I'm trying this code to generate a sequence of 8 digit numbers starting from 00000000 to 99999999. With a breaking up of files at intervals of 11111111 lines, naming them file_00000000.txt up to file_88888888.txt

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	FILE * fp;
    	int i=0;
    	char buffer[20];
    	
    	sprintf(buffer, "file_%08d.txt", i);
    	fp=fopen(buffer, "w");
    
    	for(i=0;i<100000000;i++)
    	{
    			sprintf(buffer, "%08d\n",i);
    			fprintf(fp, buffer); // Generate and write first zeros (I think :))
    
    		if(i % 11111111 == 0) // 0 is skipped ^ (or not?...)
    		{
    			printf("%d reached!\n",i);
    			sprintf(buffer, "file_%08d.txt", i);
    			fp=fopen(buffer, "w");
    		}
    		else
    		{
    	
    		}
    
    	}
    
    		
    	
    	return 0;
    }

    The problem with the code is that the first number written to file is 00000001 instead of 00000000. Should I write 00000000 separately outside the loop? Or is there a way?

    Compiler: MSVC++ 2008 Express, Running under Windows 7.

    Thanks for your help!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    I've not compiled or run your code, but the most obvious error is that you do not initialize the buffer to zero, and using character based printout device you can suffer from any kind unpredicatble error.

    use

    Code:
    memset((void*)buffer, 0x00, sizeof(buffer));
    You should always do this, by pure reflex.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,168
    Quote Originally Posted by glennik View Post
    I've not compiled or run your code, but the most obvious error is that you do not initialize the buffer to zero, and using character based printout device you can suffer from any kind unpredicatble error.

    use

    Code:
    memset((void*)buffer, 0x00, sizeof(buffer));
    You should always do this, by pure reflex.
    He sets buffer to something meaningful immediately after declaring it, so he doesn't need to memset() it, and it's certainly not an error the way he's doing it:
    Code:
    	char buffer[20];
    	
    	sprintf(buffer, "file_%08d.txt", i);
    @OP: Have you tried using a much shorter loop? Maybe 10 iterations instead of 100000000?
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >The problem with the code is that the first number written to file is 00000001 instead of 00000000. Should I write 00000000 separately outside the loop? Or is there a way?
    The only place were you'r writting to file is here
    Code:
    fprintf(fp, buffer);
    And this should write to file?

    could you try printing the buffer before writing them to the file? So that you could make sure what the control is handling.

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    2
    2itsme86: Yes, much shorter - still skips the zeros, and in fact, ssharish2005's suggestion of printing it out and itsme86's suggestion of shortening the loop helped me understand the bug.


    Code:
    Output:
    
    0 reached! (DOES NOT ACTUALLY FPRINT TO FILE)
    00000001
    00000002
    00000003
    00000004
    00000005 (These all print to file)
    00000006
    00000007
    00000008
    00000009
    10 reached! (THIS NUMBER IS NOT FPRINTED! There was no instruction to do so...)
    00000011
    00000012
    00000013
    00000014
    00000015
    00000016
    00000017
    00000018
    00000019
    20 reached!
    00000021...
    When using a much smaller loop, I found out that 0, 10, 20, 30 - were missing. The bug is in the if statement in the for loop..

    Code:
    if(i % 11111111 == 0) // 0 is skipped ^ (or not?...)
    		{
    			printf("%d reached!\n",i);
    			sprintf(buffer, "file_%08d.txt", i);
    			fp=fopen(buffer, "w");
    		}
    		else
    		{
    	
    		}
    If i % 10 == 0 we open a new file for writing and print to screen "10 reached", but I did not have an actual write the 10 to file command, that why all the MOD == 0 numbers were actually missing... adding sprintf(buffer,"%08d",i); and fprintf(fp, buffer); in the IF statement should make things right...

    Thanks for your help guys, appreciate it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 05:02 AM
  2. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 11:52 AM
  3. sequence
    By braddy in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 01:15 PM
  4. wsprintf and format specifiers
    By incognito in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 09:00 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM

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