473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

sprintf equivalent for the string class <string>

emaghero
Morning all,

I am currently processing some data that is contained in several files in several different locations. The filenames contain descriptions of the data that I am working with, for example rect_W_7.2_H_3.8.txt

I am using the STL string class on MSVS to name the directories storing the files.
Expand|Select|Wrap|Line Numbers
  1. string drive="c:\\"
  2. string dir1="Output_Files\\";
  3. string dir2="Calculation_Data\\";
  4. string wg1="Rectangular\\";
  5. string loc=drive.append(dir1).append(dir2);
  6.  
Thus far I have been using sprintf_s to open the file that I want to work on, for example
Expand|Select|Wrap|Line Numbers
  1. char file[100];
  2.  
  3. sprintf_s(file,100,rect_W_%0.1f_H_%0.1f.txt,width,height);
  4.  
  5. string current=loc;
  6.  
  7. current.append(file);
  8.  
  9. ofstream data.open(current);
  10.  
  11.  
This works fine and I havn't had any problems with it.

I was wondering if there was a sprintf equivalent for strings. Since I will be working with files of variable name length I don't want to have to count into the filename and find the position of %0.1f and use insert every time.

Thanks
Apr 16 '10 #1
6 3273
Banfa
9,065 Expert Mod 8TB
The equivilent is to use a stringstream which work much like cout/cin or any other file stream something like

Expand|Select|Wrap|Line Numbers
  1. #include <sstream>
  2. #include <iomanip>
  3.  
  4. ...
  5.  
  6. ostringstream ss;
  7.  
  8. ss << loc << "rect_W_" << fixed << setprecision(1) << width << "_H_"  << fixed << setprecision(1) << height << ".txt";
  9.  
  10. current = ss.str();
  11.  
Apr 16 '10 #2
@Banfa
Cheers, thanks very much.
Apr 16 '10 #3
@Banfa
On a related note,

Suppose I want to open a binary file for import / export using

Expand|Select|Wrap|Line Numbers
  1. int file;
  2. errno_t err=_sopen_s(&file,overlapfile,_O_BINARY,_SH_DENYNO,_S_IREAD);
  3.  
  4. if(file==-1){
  5.     perror("Failed to open file");
  6. }
  7. else{
  8.     //Import data
  9. }
  10.  
The parameter overlapfile is of type
Expand|Select|Wrap|Line Numbers
  1. const char *
Is there a way to convert a string to this type after overlapfile has been declared as

Expand|Select|Wrap|Line Numbers
  1. char overlapfile[500];
  2.  
or
Expand|Select|Wrap|Line Numbers
  1. char *overlapfile;
  2.  
I have tried various approaches, the latest being

Expand|Select|Wrap|Line Numbers
  1. string f1;
  2. //Assign something to f1
  3.  
  4. char *overlapfile=new(char[static_cast<int>(f1.size())]);
  5.  
  6. for(int i=0;i<static_cast<int>(f1.size());i++) overlapfile[i]=f1[i];
  7.  
this works but you get some junk at the end of overlapfile.
Apr 16 '10 #4
@emaghero
Just found a really easy way to get this done
Expand|Select|Wrap|Line Numbers
  1. char *overlapfile;
  2.  
  3. overlapfile=(char *)(f1.c_str());
  4.  
Thanks for all the help.
Apr 16 '10 #5
Banfa
9,065 Expert Mod 8TB
Good :D

The thing to remember is that all this basic C stuff is required by C++ too so there is nearly always a way to do it through the C++ standard library and STL
Apr 16 '10 #6
weaknessforcats
9,208 Expert Mod 8TB
This code is dangerous:

Expand|Select|Wrap|Line Numbers
  1. char *overlapfile; 
  2.  
  3. overlapfile=(char *)(f1.c_str()); 
The basic_string<char>::c_str() function returns a const char* so you can't use that pointer to hack into the string.

The user of overlapfile sees it as a char* and if that user tries to change the string pointed at by overlapfile a run-time crash will occur.

overlapfile should be defined as a const char* and there should be no cast.
Apr 16 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew Robinson | last post by:
Any easy answer what is wrong here? private List<string> BodyWords = new List<string>(); string word = "Andrew"; the following causes a compilation error:
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
5
by: Gary Wessle | last post by:
whats an efficient way to copy a string to a vector<string>? how about this? #include <iostream> #include <string> #include <vector> Using namespace std;
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
4
by: Baby Lion | last post by:
it just makes me feel confusing ... what does ^ mean , and how to use it . thanks,
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
8
by: Carmen Sei | last post by:
it seem to me that when doing include - #include <string.h- is CRT #inlcude <string- is C++ standard library Is that true those header with .h extension is CRT and those without extension...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.