1

here is my problem: I want to expand the FileName that the user gives the program in the SaveFileDialog with an index number. I get the complete Path out of the dialog and search '.' in it.

System::String^ str = saveFileDialog1->FileName;
    pin_ptr<const wchar_t> wch = PtrToStringChars(str);
    size_t convertedChars = 0;
    size_t  sizeInBytes = ((str->Length + 1) * 2);
    errno_t err = 0;
    char  *FileName = (char*)malloc(sizeInBytes);
    err = wcstombs_s(&convertedChars,
        FileName, sizeInBytes,
        wch, sizeInBytes);
for (size_t i = 0; i < sizeof((*FileName )); i++)
    {

        if (FileName [i]=='.')
        {



        }
}

and at this point I have tried different things to edit the FileName with:

insert(i-1, ("_%i",i));

Nothing I have tried, works how I want it.

I want to save different pictures of a camera and with this index it is easier for the user to find the picture he want.

Thanks in advance!

Knut

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
Knut Knut
  • 13
  • 4
  • I do not understand what are you actually trying to achieve. Could you describe complete use case from user's perspective? – Firzen Sep 26 '16 at 11:20
  • The user can make a choice how many pictures the program should take. Then the program automaticly takes the pictures with different settings of the camera. I want to locate a part of the setting in the Filename. Then it is Easier to find the picture. To increment the Number at the end of the filename is only for trying out how it could work. – Knut Knut Sep 26 '16 at 11:32
  • The user gives me for example the Path c:\user\document\filename.txt . What I want to do ist that my program save a new Image as filename_1 and Increment the number for every new image. – Knut Knut Sep 26 '16 at 11:43
  • Like it would be done here: http://stackoverflow.com/questions/13108973/creating-file-names-automatically-c But I want to edit the user given filename. And forget the .txt in the comment above it schould be a .jpg or an image file formate. – Knut Knut Sep 26 '16 at 12:16
  • And why are you searching '.' (the dot) in given path? Do you want to cut off file extension? If you will get path C:\test\photo.tiff, will output be for example C:\test\photo_XYZ.jpg ? – Firzen Sep 26 '16 at 12:49
  • Actually I only want to insert the _XYZ between the dot and photo. That the file extension is not change. – Knut Knut Sep 26 '16 at 12:56

2 Answers2

0

So, you basically need to split filename to its raw name and extension, and then join these parts with some index. You can use this function:

string get_indexed_name(string filename, string index)
{
    size_t lastindex = filename.find_last_of(".");     // get position of dot
    string rawname = filename.substr(0, lastindex);    // get string contents before dot
    string extension = filename.substr(lastindex + 1); // get string contents after dot

    return rawname + index + "." + extension;          // return indexed name
}

This way uses predefined standard functions. If you want to do (and learn) it yourself, try to implement functions find_last_of and substr. Then you can use your own implementation.

If there is some compilation problem, be sure to start your source code with these lines:

#include <iostream>
using namespace std;
Firzen
  • 1,909
  • 9
  • 28
  • 42
  • accurate when you were posted your answer I get it by my self :D. Thank you for that solution and for your questions that let me find a solution by my self. – Knut Knut Sep 26 '16 at 13:55
  • I am glad you was able to solve it yourself. Congratulations! – Firzen Sep 26 '16 at 13:59
0

Here is my Solution:

System::String^ str = saveFileDialog1->FileName;


    std::string sFilePath = msclr::interop::marshal_as< std::string >(str);
    std::string fileExtension=msclr::interop::marshal_as< std::string >(str);
    size_t pos= sFilePath.find('.', 1);
    sFilePath = sFilePath.substr(0, pos );
    fileExtension = fileExtension.substr(pos);                              
    for(size_t i=0; i<100; i++){                               
           std::string fileName;
           fileName = sFilePath + std::to_string(i) + fileExtension;}

For this solution you have to include.

#include <msclr\marshal_cppstd.h>

And if you want that your file number is with leaded zeros you have to use stringstream like this:

        string fileName;
        string fileNumber ;
        stringstream ss;
        ss <<'_'<< setfill('0') << setw(4) << std::to_string(i);
        fileNumber = ss.str();
        fileName = f_sFilePath + fileNumber + f_sFileExtension;
Knut Knut
  • 13
  • 4