473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ofstream input wchar_t

75 New Member
dont freak out by length of code! its commented out befiore each block so you dont have 2 read it all..

so anyway! i have a fairly simple question i wld say, all i need is a little guide,

i am trying to write an input stream to write to files, but the file being opened needs to change with each for loop, so i did this

Expand|Select|Wrap|Line Numbers
  1. ofstream out;
  2.  
  3.  
  4. for (int j=0;j<24;j++){
  5.  
  6.     /////////int to string
  7.             string jAsString;
  8.             stringstream smStrngStrm;
  9.             smStrngStrm << j;
  10.             jAsString = smStrngStrm.str();
  11.  
  12.  
  13.     //file path in string format, to be cnverted to wchar_t
  14.     string filePath = "geo";
  15.     filePath += jAsString;
  16.     filePath += ".cache";
  17.  
  18.  
  19.  
  20.     // First convert to a char*
  21.     string cstring = filePath;
  22.     const size_t newsize = 100;
  23.     char pathChar[newsize];
  24.     strcpy_s(pathChar, cstring.c_str());
  25.     strcat_s(pathChar, " (char *)");
  26.  
  27.     // Then convert to a wchar_t*
  28.     size_t origsize = strlen(cstring.c_str()) + 1;
  29.     size_t convertedChars = 0;
  30.     wchar_t wcstring[newsize];
  31.     mbstowcs_s(&convertedChars, wcstring, origsize, cstring.c_str(), _TRUNCATE);
  32.     wcscat_s(wcstring, L" (wchar_t *)");
  33.     const char *pathAsChar = filePath.c_str();
  34.  
  35.  
  36.     out.open(wcstring, ios::out);
  37.  
  38.         out<<wcstring[0]<<"\n";
  39.     out<< sizeof(wcstring)<<"\n";
  40.  
  41.  
  42.         for(int i=0;i!=meshVertices.length();++i) {
  43.  
  44.  
  45.  
  46.                 out        //<< pathToGeo
  47.                         << i << " "
  48.                         << meshVertices[i].x <<" "
  49.                         << meshVertices[i].y <<" "
  50.                         << meshVertices[i].z <<"\n";
  51.  
  52.                 }
  53.  
  54.  
  55.     out.close();
  56.  
  57.     }



but it doesnt seem to be opening the file with the string name wcstring, and when i try to print it out, i mean the variable, i get some hexadecimal stuff, does anyone have a clue???? thanks
Sep 16 '07 #1
1 4293
ilikepython
844 Recognized Expert Contributor
dont freak out by length of code! its commented out befiore each block so you dont have 2 read it all..

so anyway! i have a fairly simple question i wld say, all i need is a little guide,

i am trying to write an input stream to write to files, but the file being opened needs to change with each for loop, so i did this

ofstream out;


for (int j=0;j<24;j++){

/////////int to string
string jAsString;
stringstream smStrngStrm;
smStrngStrm << j;
jAsString = smStrngStrm.str ();


//file path in string format, to be cnverted to wchar_t
string filePath = "geo";
filePath += jAsString;
filePath += ".cache";



// First convert to a char*
string cstring = filePath;
const size_t newsize = 100;
char pathChar[newsize];
strcpy_s(pathCh ar, cstring.c_str() );
strcat_s(pathCh ar, " (char *)");

// Then convert to a wchar_t*
size_t origsize = strlen(cstring. c_str()) + 1;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&con vertedChars, wcstring, origsize, cstring.c_str() , _TRUNCATE);
wcscat_s(wcstri ng, L" (wchar_t *)");
const char *pathAsChar = filePath.c_str( );


out.open(wcstri ng, ios::out);

out<<wcstring[0]<<"\n";
out<< sizeof(wcstring )<<"\n";


for(int i=0;i!=meshVert ices.length();+ +i) {



out //<< pathToGeo
<< i << " "
<< meshVertices[i].x <<" "
<< meshVertices[i].y <<" "
<< meshVertices[i].z <<"\n";

}


out.close();

}




but it doesnt seem to be opening the file with the string name wcstring, and when i try to print it out, i mean the variable, i get some hexadecimal stuff, does anyone have a clue???? thanks
I don't think you need to do all of that code to open the file:
Expand|Select|Wrap|Line Numbers
  1.     string filePath = "geo";
  2.     filePath += jAsString;
  3.     filePath += ".cache";
  4.  
  5.     out.open(filePath.c_str(), ios::out);
  6.  
Sep 16 '07 #2

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

Similar topics

6
3219
by: ma740988 | last post by:
There's no way to use the STL algorithm copy to print an outfile (essentially an ofstream)? So now: int main() { std::ifstream InFile( "exercise15.txt"); std::ofstream ToFile( "NewFile.txt" ); //////////////////////////// // Alternative 1
1
1632
by: phoenix | last post by:
How can i read a numbers from file.. I created this file like this: int dane, i; std::ofstream plik_z; plik_z.open(nazwa, std::ios::out | std::ios::trunc | std::ios::binary); for (i = 0; i < 15; i++) plik_z << dane; plik_z.close(); Is it any way to read from this file all numbers ?.. If so - how can i do
3
9188
by: jois.de.vivre | last post by:
Hi, I'm trying to write to an ofstream, and for some reason it fails. I know I can check it with fail() or bad(), but it gives me no useful information as to why it fails. Are there any C++ functions that do this or is there any way I can check manually? Thanks, Prashant
15
6959
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the list of ofstreams. I need to have them open simultaneously for writing -- I have millions of rows of data to write to them so opening and closing all the time will be unacceptable in efficiency. The following program compiles but failed to run....
8
14332
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does ifstream. Something I found shows that, I need to have a proper codecvt to set it. I need more information, maybe a small piece of code sample. Thank you!
5
14703
by: shyam | last post by:
HI I have a logging application wherin I create a ofstream object say, ofstream logger; by doing this logger.open(/* some path*/,ios_base:app);
15
2482
by: rEvolution27 | last post by:
I'm a c++ newbie here, trying out some stuff and when I try to compile this: void create() { char name; cout << "Creating a new timetable /n Please type a name for this timetable"; cin >name; ofstream editFile; editFile.open (name, ios::out | ios::app);
3
1324
by: askalottaqs | last post by:
i want to open an ofstream but with a variable, i keep being told that i cant convert from string to const wchar_t * , has anyone ever faced this problem? i tried all types of conversions, but i figured there must be an easier way out, thanks
14
4120
by: Ioannis Vranos | last post by:
The following code does not work as expected: #include <wchar.h> #include <locale.h> #include <stdio.h> #include <stddef.h> int main() {
0
10156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.