Thread: Creating text file with random numbers

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    Creating text file with random numbers

    Hi,
    I need to create a program that outputs a .txt file with a 20x30 array of random numbers from 0 to 9. What I have creates the .txt file, although it is empty when opened. Here is what I have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
        FILE *fp;
        int i, j;
        char list[20][30];
        
        if((fp = fopen("numbers.txt", "a+")) == NULL)
        {
            fprintf(stdout, "Can't open \"numbers\" file.\n");
            exit(1);
        }
        
        srand(time(NULL));                     //initialize random seed
            
        puts("the random numbers from 0 to 9 are:");    
        
        for (i=0; i<20; i++)
        {    
            for (j=0; j<30; j++)
            {
                list[i][j] = rand() % 10;        //random number between 0 and 9
                
                fprintf (stdout, "%d ", list[i][j]);
            }    
            
            putchar('\n');
        }
        
        if(fclose(fp) != 0)
        {
            fprintf(stderr,"Error closing file\n");
        } 
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fprintf (stdout, "%d ", list[i][j]);
    Maybe printf to fp instead?
    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
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Your actually sending the data to stdout,and not the file itself!Don't you see them at your screen?

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    I see the data on screen, but I would like to see them in the file as well.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by l_l View Post
    I see the data on screen, but I would like to see them in the file as well.
    So you should do what salem said!fprintf send the data to the stream you told her to.So if you use stdout,the data will be printed on screen.If you use fp,then the data will be sent to where fp points(fp points to your file!)
    So i you want to see them both in file and in screen ,use fprintf twice.Ok?

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    thanks for the help. I got it to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing random decimal numbers in a text file
    By warrick in forum C Programming
    Replies: 3
    Last Post: 03-08-2010, 09:32 AM
  2. Creating Random hex numbers
    By disruptor108 in forum C++ Programming
    Replies: 24
    Last Post: 06-06-2007, 11:00 AM
  3. Creating a story with random numbers!
    By Dilmerv in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2006, 01:22 AM
  4. Creating a file with random numbers question??
    By Hoser83 in forum C Programming
    Replies: 28
    Last Post: 02-16-2006, 02:11 PM
  5. Creating a UDF to create random numbers
    By rgmills in forum C++ Programming
    Replies: 5
    Last Post: 11-15-2005, 11:22 PM