Thread: Creating a file with random numbers question??

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    Creating a file with random numbers question??

    Hey guys. I need some help..bad. I have to write a program that creates a file of randomly distributed numbers. I have the functions figured out and some of the main(). Could you guys take a look at it and just try to steer me in the right direction. I'm just confused on how to get a filename from user, and how many numbers they want to see, then acutally do it. I just need some help getting started.

    Heres the code:

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void get_info(char *filename, int *n_ptr);
    
    FILE *cfopen(char *filename, char *mode);
    
    FILE *gfopen(char *filename, char *mode);
    
    int main( int argc, char **argv)
    {
           FILE  *ofp;
           int      i;
    
           // I don't know if I've declared all that I should, and I'm not
           // sure how to call the functions.
    
    
    
           for(i=1; i<=n; ++i){
               fprintf(ofp, "%12d", rand());
               if(i%6 == 0 || i == n)
                  fprintf(ofp, "\n");
    }
    
    
    void get_info(char *filename, int *n_ptr)
    {
        prinf("\n%S\n\n%s",
    	"This progam creates a file of random numbers.",
    	"How many random numbers would you like?   "),
        scanf("%d", n_ptr);
        printf("\nIn what file would you like them?  ");
        scanf("%s", filename);
    }
    
    
    
    
    FILE *cfopen(char *filename, char *mode)
    {
        char  reply[2];
        FILE  *fp;
     
        if(strcmp(mode, "w") == 0
          && (fp = fopen(filename, "r")) != NULL){
            fclose(fp);
    	printf("\nFile exists.  Overwrite it? ");
            scanf("%1s", reply);
            if(*reply != 'y' && *reply != 'Y') {
            printf("\nBye!\n\n");
    	exit(1);
    	}
          fp = gfopen(filename, mode);
          return fp;
    }
    
    
    
    FILE *gfopen(char *filename, char *mode)
    {
        FILE *fp;
        
        if((fp = fopen(filename,mode)) == NULL){
        fprintf(stderr, "Cannot open %s - bye!\n", filename);   
        exit(1);
        }
        return fp;
    }

    Any help would be greatly appreciated!!!!!

  2. #2
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    okay, I just realized that I don't need to use argc and argv in this, but I still am not sure what all I have to Initialize and then pass to the funtions. I think I have to include an array so that the numbers will be printed by the fprintf statements, but I'm not sure how to set that up either. Any advice - just advice?

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    first things first, you need to learn how to generate a good set of
    random numbers. you need to seed rand first like this:

    Code:
    #include <time.h>
    
    int main (void)
    {
          int i, j, k;
    .
    .
    .
    
          srand (time(0));
    
          printf ("How many random numbers would you like: ");
          scanf ("%d", &i);
    
          for (j=0; j<i; j++)
          {
                   fprintf (filepointer, "%d\n", rand ());
          }
    call srand only once and you get good random numbers.

    next thing, reading in from the user: use fgets to read in strings,
    its probably the best function to use. see this

    now IMO it would be better to write this program as just 1
    function, that way you can avoid using pointers because you
    dont seem to be too confident with how they work, and also
    this program is not too complex either, although it may seem
    challenging at the moment.

    file i/o should be ok for you, just read any of the tutorials on
    it on this site, like this

    id like to help more but i dont want to end up writing the bulk
    of the code for you because this is a good learing exercise
    for file i/o. i just recommend reading up on the things i highlighted
    and making sure that you understand them clearly. If you
    run into specific problems with stuff you dont get then come back
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    Thanks Richie. I totally agree with what you said about the random numbers, fgets, and that fact that I don't need to be using so many functions. The problem is that I'm required to use these functions, and the for loop in main() to generate and print these random numbers to the file the user types. I just don't know how to call those functions correctly.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    Am I anywhere close with this:

    Code:
    int main(void)
    {
         FILE  *ofp;
         int     i,n=0;
         char  filename;
    
         get_info(filename, n);
         ofp = cfopen(filename, "r+");
    
          for(i=1; i<=n; ++i){
               fprintf(ofp, "%12d", rand());
               if(i%6 == 0 || i == n)
                  fprintf(ofp, "\n");
         }
    }
    Am I calling get_info correctly? I don't have a PC with a compiler right now so I can't use that. I'm just trying to get close right at the moment.

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    i was in the middle of a reply but i've decided to work with your
    last post.

    the problem is that you're calling you're function get_info
    and passing it parameters but the parameters have no values -
    not good. also, the parameters in get_info are pointers, they cant
    take actual data, only memory addresses. change to:

    get_info(&filename, &n);

    what is the function gfopen supposed to do?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    I am suppose to use these three functions in this program. They give cfopen, and we are to write gfopen. I think I have it correct. Should it not be there or is it incorrect?

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    well IMO the code for cfopen isn't that hot, but if you have to use
    it then i guess you have to. so gfopen is probably supposed
    to write to the file you specified. i mean, this line

    Code:
    if(strcmp(mode, "w") == 0 && (fp = fopen(filename, "r")) != NULL){
    i think that's trying to test to see if the file exits - the problem
    is that it will probably create the file if it doesn't exist.

    i have to go for about an hour so leave it with me and i'll try
    and figure out what it is you need.

    just so we are clear - you have to generate a user defined
    number of random numbers, and write these to a text file -
    nothing else?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    yes, sir. to a text file that the user also defines.
    Last edited by Hoser83; 02-14-2006 at 01:15 PM.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    This is what I have so far:





    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void get_info(char *filename, int *n_ptr);
    
    FILE *cfopen(char *filename, char *mode);
    
    FILE *gfopen(char *filename, char *mode);
    
    int main(void)
    {
         FILE  *ofp;
         int     i,n=0;
         char  filename[120];
    
         get_info(filename, &n);
         ofp = cfopen(filename, "r+");
    
          for(i=1; i<=n; ++i){
               fprintf(ofp, "%12d", rand());
               if(i%6 == 0 || i == n)
                  fprintf(ofp, "\n");
         }
    }
    
    void get_info(char *filename, int *n_ptr)
    {
        prinf("\n%s\n\n%s",
    	"This progam creates a file of random numbers.",
    	"How many random numbers would you like?   ");
        scanf("%d", n_ptr);
        printf("\nIn what file would you like them?  ");
        scanf("%s", filename);
    }
    
    
    
    
    FILE *cfopen(char *filename, char *mode)
    {
        char  reply[2];
        FILE  *fp;
     
        if(strcmp(mode, "w") == 0
          && (fp = fopen(filename, "r")) != NULL){
            fclose(fp);
    	printf("\nFile exists.  Overwrite it? ");
            scanf("%1s", reply);
            if(*reply != 'y' && *reply != 'Y') {
            printf("\nBye!\n\n");
    	exit(1);
    	}
          fp = gfopen(filename, mode);
          return fp;
    }
    
    
    
    FILE *gfopen(char *filename, char *mode)
    {
        FILE *fp;
        
        if((fp = fopen(filename,mode)) == NULL){
        fprintf(stderr, "Cannot open %s - bye!\n", filename);   
        exit(1);
        }
        return fp;
    }
    Last edited by Hoser83; 02-14-2006 at 02:16 PM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char filename;

    > get_info(&filename, &n);

    You need a char array to hold the filename, "char filename" is just one char. Also you don't need the address (&) for passing filename, that's the default, so:
    Code:
         char  filename[120];
    
         get_info(filename, &n);

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Code:
    prinf("\n%S\n\n%s",
    what's prinf?
    %S is not a valid format specifier

    you have unbalanced parantheses in function cfopen

    You are trying to store a string in filename, but you've only allocated space for a single character.
    You need a character array.

    If you change filename to an array, you do not need the address operator '&' when passing to functions.

    You need to include string.h if you plan to use string functions

    Edit: string stuff already mentioned by Swoopy

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use srand() for better random numbers. See Richie T's first post.

    Code:
    if(strcmp(mode, "w") == 0 && (fp = fopen(filename, "r")) != NULL){
    i think that's trying to test to see if the file exits - the problem
    is that it will probably create the file if it doesn't exist.
    If you open a file in mode "r", the file isn't created. fopen() returns 0 in that case.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    I fixed up the printf statement. The %s was wrong, and the other parentheses is at the end of the second sentence: "How many would you like to see?" I also changed the filename to an array.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Does it work now? Post your code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM