Thread: Help with fprintf function

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    10

    Help with fprintf function

    Hello Everybody.

    The purpose for my code is to print some random code to a file but it's not.
    Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    
    int main()
    {
        int keyUPPER=0, keyLOWER=0, keyINT=0, i=0, j=0;
        char access[78];
        char code1[10], code2[11];
    	FILE *read, *exec;
    
    
    	exec = fopen("key.txt", "w");
    
    
        printf("Your access code for today is: ");
        srand((int)time(0));
    
    
        for (i=0; i<26; i++){
            keyUPPER = ((rand()%25) + 65);
            access[i] = keyUPPER;
        }
        for (i=i; i<52; i++){
            keyINT = (rand()%8) + 49;
            access[i] = keyINT;
        }
        for (i=i; i<78; i++){
            keyLOWER = ((rand()%25) + 97);
            access[i] = keyLOWER;
        }
    
    
        for (i=0; i<10; i++){
    		j = rand()%78;
            printf("%c", access[j]);
            fprintf(exec, "%c", access[j]);
        }
        printf("\n");
    
    
    
    
        return 0;
    }
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Seems OK here.
    Code:
    $ ./a.out 
    Your access code for today is: 5cKSsfDr1O
    $ cat key.txt 
    5cKSsfDr1O$ 
    $ ./a.out 
    Your access code for today is: 1wVj11lkyr
    $ cat key.txt 
    1wVj11lkyr$
    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.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Thanks for the response.
    I'm using VS 2010 and every time I run it I just get a blank file. Is there anything special I need to do?

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Nevermind. Got it to work now. I forgot to take out an extra part of the code that I didn't post here, so now it works.

    Thanks though for the response.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    well i use pelles c ide for c code, and it works for me. BUT i do get the following

    (8): warning #2117: Old-style function definition for 'main'.
    (12): warning #2114: Local 'read' is not referenced.
    (11): warning #2114: Local 'code2' is not referenced.
    (11): warning #2114: Local 'code1' is not referenced.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating our own fprintf function in our library
    By baskar.v in forum C Programming
    Replies: 6
    Last Post: 04-06-2008, 03:31 PM
  2. Replies: 7
    Last Post: 03-26-2008, 03:21 AM
  3. fprintf works in one function but not in the other.
    By smegly in forum C Programming
    Replies: 11
    Last Post: 05-25-2004, 03:30 PM
  4. Ask about fprintf function?
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 09-27-2002, 11:26 PM
  5. Ask question about fprintf function
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 02:52 PM

Tags for this Thread