473,396 Members | 1,895 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,396 software developers and data experts.

ofstream input wchar_t

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 4274
ilikepython
844 Expert 512MB
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(pathChar, cstring.c_str());
strcat_s(pathChar, " (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(&convertedChars, wcstring, origsize, cstring.c_str(), _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
const char *pathAsChar = filePath.c_str();


out.open(wcstring, ios::out);

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


for(int i=0;i!=meshVertices.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
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"...
1
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 <...
3
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++...
15
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...
8
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...
5
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
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;...
3
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...
14
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.