Never used Sprintf before. My program crashes once the sprintf should begin

This is a discussion on Never used Sprintf before. My program crashes once the sprintf should begin within the C Programming forums, part of the General Programming Boards category; The purpose of this code is to let the user enter a name of a song into a string, which ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    28

    Never used Sprintf before. My program crashes once the sprintf should begin

    The purpose of this code is to let the user enter a name of a song into a string, which can then be buffered and played into a program called 'mplayer'.

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <unistd.h>
    
    int main(void)
    {
        int menu, option, playchoice;       // for switch case- ignore this.
        char song[20];                            // string for song name
    
                                       printf("Which song?"); 
                                       scanf("%s", &song);    // sets song name to 'song'
                                       sprintf("buffer, system(%s)", song);        
                                       system("buffer");
                                       sprintf("buffer.mplayer %s", song);
    system("pause"); 
    }
    I have just selected the bits relating to this code as the other code (switch statements) are not the problem.

    This is for a program called 'mplayer'. There are no compiling errors. It just stops responding and Windows closes it. I am new to programming and have had difficulties getting this far.

    Thanks for any help!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Code:
    char buffer[100];
    sprintf( buffer, "mplayer %s", song );
    system( buffer );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    28
    Thank you SO much Salem! This makes more sense than the rubbish I wrote lol.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    You should also prevent buffer overflow by modifying your scanf line to have something like "%19s" rather than just "%s".
    Of course I would make the song buffer much larger first, perhaps MAX_PATH+1 in size.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    28
    Thank you, iMalc!. I'll look into this. We haven't even learnt about buffering yet, but I have a rough idea, so this will be useful when I've looked into it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Doesnt crash in debug mode, crashes normally
    By chubigans in forum C Programming
    Replies: 8
    Last Post: 11-10-2009, 10:53 PM
  2. Loop crashes program
    By Akkernight in forum C++ Programming
    Replies: 12
    Last Post: 01-22-2009, 12:58 PM
  3. HttpSendRequest crashes program
    By Anubis208 in forum Windows Programming
    Replies: 8
    Last Post: 10-15-2008, 01:58 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 11:26 AM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 05:33 PM

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