Thread: Insert int into string

  1. #1
    Registered User
    Join Date
    Mar 2004

    Insert int into string

    I'm trying to get this program working. Everything works fine but I can't figure out how to insert an int into a filename. The user inputs some stuff at the beginning of the program, one of those being a number, say 5. Then before the program ends it is suppose to write the file to something like


    where # is the number they entered before, 5 in this case.

    I can't get it to insert 5 into the filename, it just always writes it as run-.txt. Any suggestions?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    char filename[100];
    sprintf( filename, "run-%d.txt", number );
    fp = fopen( filename, "r" );
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM