Insert int into string

This is a discussion on Insert int into string within the C Programming forums, part of the General Programming Boards category; I'm trying to get this program working. Everything works fine but I can't figure out how to insert an int ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    8

    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

    run-#.txt

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06:57 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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