Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I'm trying to save files with different names within a loop in C++, I've found a number of solutions, but unfortunately it doesn't fit my work.
My function saves a QRcode as an image (.jpg). the syntax of the statement which does the saving is as follows:
C++
ERRCODE _stdcall BCSaveImageA  ( t_BarCode *  pBarCode,  
  LPCSTR  lpszFileName,  
  e_IMType  eImageType,  
  LONG  lXSize,  
  LONG  lYSize,  
  DOUBLE  dXRes,  
  DOUBLE  dYRes  
 )

where:
Parameters:
[in] pBarCode Pointer to barcode structure.
[in] lpszFileName Filename
[in] eImageType Enumeration for the type of the image.
[in] lXSize X-Size of the image [pixel]. For Vector-EPS the unit is [0.001 mms]
[in] lYSize Y-Size of the image [pixel]. For Vector-EPS the unit is [0.001 mms]
[in] dXRes X-Resolution of the image [pixels / inch].
[in] dYRes Y-Resolution of the image [pixels / inch].

which is used successfully to generate a single file in my program as follows:
C++
eCode = BCSaveImageA(pBC,"C://webcontent/QR12.jpg", 4, LBarcodeWidth,LBarcodeHieght, dpi, dpi);

The problem is how can I save a number of image files (.jpg) with different names? I've used a for loop and tried to write the name of the new file in the above statement as follows:
C++
eCode = BCSaveImageA(pBC,"C://webcontent/QR"+IntToStr(i)+".jpg", 4, LBarcodeWidth,LBarcodeHieght, dpi, dpi);

where IntToStr is a function that converts int to string
C++
string IntToStr(int n) 
{
    stringstream result;
    result << n;
    return result.str();
}

Thank you & Best Regards.
Rania
Posted
Updated 28-Jan-15 10:19am
v2

the loop looks OK, but can't see other code.

one mistake is the filename needs LPCSTR which is a C style string so to convert the C++ string you'd need
C++
str.c_str()


second mistake is concatenation with + works with string but not C strings.
so one idea is to adapt method:

string IntToStr(int n) 
{
    stringstream result;
    result<< "C://webcontent/QR"<<n<<".jpg";
    return result.str();
}


then convert string to
LPCSTR 
 
Share this answer
 
Your question is really about constructing a LPCSTR.

Try the following

C++
#include<string>
using namespace std;


C#
for(int n = 0; n < 20; n++)
{
        string filename = "C://webcontent/QR"+ to_string((_ULonglong)n) +".jpg";
        eCode = BCSaveImageA(pBC, filename.c_str(), 4, LBarcodeWidth,LBarcodeHieght, dpi, dpi);
}


If your compiler is C++11 compliant you wont need the _ULonglong cast.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900