sprintf funcionality

This is a discussion on sprintf funcionality within the C Programming forums, part of the General Programming Boards category; Hi, i already googled it but i couldn't found an adequate answer so here it goes. Im trying to replace ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Post sprintf funcionality

    Hi,

    i already googled it but i couldn't found an adequate answer so here it goes.

    Im trying to replace the instruction:

    sprintf (command, "rename %s_TypeA_%04d %s_TypeA_%04d.yuv\n", videofile, i, videofile, i);

    which is a windows system command to rename files and i intend to run it for a long sequence of frames.
    The objective here is to replace the manually inserted %04d for a variable which tells you the length of the sufix, something like as follows:

    sprintf (command, "rename %s_TypeA_%0%dd %s_Base_%0%dd.yuv\n", videofile, i, sufix, videofile, i, sufix);

    I cant get this working as it returns an error at output.c "Unhandled exception at 0x6642984f (msvcr90d.dll) in my_program.exe: 0xC0000005: Access violation reading location 0x00000001."

    I am hoping that someone can give me an advice to solve this.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You mean this?
    Code:
    sprintf(command, "rename %s_TypeA_%0*d %s_Base_%0*d.yuv\n",
            videofile, i, sufix, videofile, i, sufix);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    Quote Originally Posted by hk_mp5kpdw View Post
    You mean this?
    Code:
    sprintf(command, "rename %s_TypeA_%0*d %s_Base_%0*d.yuv\n",
            videofile, i, sufix, videofile, i, sufix);

    No, that will output if sufix=2(along the for cycle and if you replace sprintf with a printf):
    rename videofile_TypeA_2 videofile_TypeA_2.yuv
    rename videofile_TypeA_02 videofile_TypeA_02.yuv
    rename videofile_TypeA_002 videofile_TypeA_002.yuv
    rename videofile_TypeA_0002 videofile_TypeA_0002.yuv


    What i want to do:
    rename videofile_TypeA_00 videofile_TypeA_00.yuv
    rename videofile_TypeA_01 videofile_TypeA_01.yuv
    rename videofile_TypeA_02 videofile_TypeA_02.yuv
    rename videofile_TypeA_03 videofile_TypeA_03.yuv

    or

    rename videofile_TypeA_000 videofile_TypeA_000.yuv
    rename videofile_TypeA_001 videofile_TypeA_001.yuv
    rename videofile_TypeA_002 videofile_TypeA_002.yuv
    rename videofile_TypeA_003 videofile_TypeA_003.yuv

    or

    rename videofile_TypeA_0000 videofile_TypeA_0000.yuv
    rename videofile_TypeA_0001 videofile_TypeA_0001.yuv
    rename videofile_TypeA_0002 videofile_TypeA_0002.yuv
    rename videofile_TypeA_0003 videofile_TypeA_0003.yuv

    according to the value of sufix.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Switch suffix and i in your print statement, then.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First question... why would you use a slow and error prone system call and not use the MoveFile() function built into windows?

    Combine this with FindFirstFile() FindNextFile() and FindClose() and it's a pretty simple task to sweep through a folder, locate files and rename them.

    And it's going to be 5 or 10 times faster than system calls...

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    3

    Thumbs up Thank You

    Thanks for the answers.

    I already switched it. when I answered you i was in a hurry and just experimented your code and didn't even analyzed it as i should've, because i had never used * operand inside sprintf. So switching them ocurred me in a class I was attending as a pretty simple thing to do and i was already thinking i would be mocked by that when i come back here

    Above the second answer, i dont know those functions but i will certanly take a look at them.

    So thank you all for the tips. It was the first time I used this forum and I'm very pleased with it. I'll certanly come back to help and be helped if I need so.
    Last edited by pintoxpto; 05-19-2011 at 02:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-12-2010, 07:02 PM
  2. sprintf()
    By BMathis in forum C Programming
    Replies: 2
    Last Post: 02-16-2009, 05:13 PM
  3. sprintf
    By Bladactania in forum C Programming
    Replies: 1
    Last Post: 02-13-2009, 11:08 AM
  4. sprintf
    By rocketman03 in forum C Programming
    Replies: 3
    Last Post: 10-25-2008, 03:11 PM
  5. sprintf
    By krappykoder in forum C++ Programming
    Replies: 12
    Last Post: 11-18-2004, 04:45 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