Like Tree3Likes
  • 1 Post By CommonTater
  • 1 Post By Adak
  • 1 Post By CommonTater

C file creation and strings

This is a discussion on C file creation and strings within the C Programming forums, part of the General Programming Boards category; I need to create a function that prompts the user to enter a string and use that string to create ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    46

    C file creation and strings

    I need to create a function that prompts the user to enter a string and use that string to create a file to which I will store data

    this runs, but does not create the file as I wish:
    Code:
    void saveFile (char** list, int size, char* filename)
    {
         int i, length;
    
         printf("\nPlease enter the name of the file to which you would like to save your output:\n"); 
         printf("\tOnly use alphabetical characters, numbers, or '_'\n");
         printf("\t\tMax of 15 characters per name\n");
         scanf("%15s", filename);
         FLUSH;
    
        strcat(filename, ".txt");
        fp2 = fopen(filename, "w");
        if(!fp2)
                MEM_ERROR;
                
        for(i = 0; i < size; i++)
        {        
              fputs(list[i], fp2);
              fputs("\n", fp2);
        }
    
        printf("\n\t%s has been created succesfully\n\n", filename);
        
        fclose(fp2);
    
        return;
    }
    I appreciate all feeback

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    Nevermind, I just fixed it by using gets ^_____^

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why are you passing in a pointer to a string for your filename when you could declare that locally?

    Just a suggestion...
    Code:
    int saveFile (char** list, int size)
    {    char filename[20];
         int i, length;
          
         // simpler prompt  (Operators hate unnecessary loquatiousness) 
    
         printf("Save As (15 characters, no numbers) :"); 
         if ( scanf("%15s", filename) < 1)
           return 0;  // return failure
    
         // append .txt if no extension provided
     
        if ( ! strchr(filename, '.'))
           strcat(filename, ".txt");
     
        fp2 = fopen(filename, "w");
        if(!fp2)
          return 0; // return failure
                
        for(i = 0; i < size; i++)
        {        
              fputs(list[i], fp2);
              fputs("\n", fp2);
        }
    
        fclose(fp2);
        printf("\n\t%s has been created succesfully\n", filename);
        return 1;  // return success
    }
    jeanermand likes this.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Glad it worked for you, but gets() is one of the most unsafe methods to get a user's input. Might be OK for your input, but never use it for a general user's input. They'll tear it to pieces.
    CommonTater likes this.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Glad it worked for you, but gets() is one of the most unsafe methods to get a user's input. Might be OK for your input, but never use it for a general user's input. They'll tear it to pieces.
    You mean...

    >>SaveAs ... "I want my file to be named 'the lost barber of Saville' backwards and in chinese, thank you very much"

    LOL... I used to service systems in an office where one of the operators knew she could crash the system that way and would periodically type in invalid inputs to get a Friday afternoon off... at a cost to her employer of $220.00 per visit!
    Tclausex likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exe file creation using run prompt
    By siperi in forum C Programming
    Replies: 2
    Last Post: 03-20-2011, 09:29 AM
  2. Replies: 2
    Last Post: 12-16-2008, 01:43 PM
  3. Help on File Creation with Date...
    By mehuldoshi in forum C Programming
    Replies: 2
    Last Post: 07-29-2002, 01:28 AM
  4. File creation
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-18-2002, 08:36 PM
  5. *.dat file creation
    By Robert_Ingleby in forum C Programming
    Replies: 6
    Last Post: 03-07-2002, 04:32 PM

Tags for this Thread


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