Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The attached code works only when "test.txt" is used as name of the file.
When ""test_1.txt" is used it fails to create / open the file.
Of course it does not work when REAL file name is used.
Obviously I need the real Mc Coy to work, but so far it fails just testing it.


//#define MERGE_1 "/media/z/DEV_COPY_LABEL/ECLIPSE_FOLDER/2019-12/Eclipse_2019_12/eclipse/Workspace_2019_12/RPI_BT_LIB_ARM/MODULE/M_ARM_SOCKET/CARMSOCKET.h"
#define MERGE_1  "test_1.txt"
#define MERGE_1  "test.txt"
	std::string str(MERGE_1);
	char *cstr = new char[str.length() + 1];
	std::strcpy(cstr, str.c_str());
	cout << "cstr " << +cstr << endl;
	fstream fs;
	fs.open(cstr);
	perror_cpp("File state ");
	cout << " Merged file created OK " << endl;


What I have tried:

Flowed instruction to use c_str and it does not work with anything but alpha characters.
Posted
Updated 1-Feb-20 7:44am

Works fine for me. But why are you copying from a string to a char array? All you need is:
fs.open(str); // fstream methods can handle string variables

Also the str.c_str() will handle all characters in the ASCII set: alpha, numeric, special etc. So, whatever problem you are seeing is not clear from the above code.
 
Share this answer
 
Comments
Vaclav_ 1-Feb-20 13:48pm    
I have tried both char and string - failed same way.
Richard MacCutchan 2-Feb-20 3:43am    
It appears that if you do not specify ios_base::out in the open call, the library expects the file to exist already.
At a guess, it's a permissions problem.
You don't specify a path, which means it will try to open or create a file in eth current folder - which for an installed executable will be the application installation folder. On many systems (including Windows) those are by default write protected to reduce virus activity.

Try specifying your file with an explicit absolute path to a "safe" folder and it should work.
 
Share this answer
 
Comments
Vaclav_ 31-Jan-20 12:48pm    
Why would different name format behave this way ?
OriginalGriff 31-Jan-20 14:01pm    
it probably isn't: it may be that the folder you are working in doesn't have create file permission, or the file may be in use. The first thing to do is to find out exactly what is going on, and for that you need to explicitly specify the folder, and start checking what is happening.
If I try it code, it works - just as I'd expect it to - so it's something specific to your environment or how your app is working that I can't duplicate.
Vaclav_ 31-Jan-20 14:17pm    
To "find out what is going on" comment is superfluous. Not really helpful...As far as I remember "open" creates a file IF it does not exist then opens it for whatever mode is specified or in case of stream by the actual "command".
The file location in this case is immaterial, perhaps I will get a different error if file cannot be created "in folder". ( I'll will select non existent folder next ) I am trying to debug this in some logical way by altering analysing the ONLY thing which is changing - the file name.

Here is my latest

ofstream file_1;
file_1.open("file_1.txt"); //open a file
if (file_1.is_open())
cout << "open " << endl;
else
cout << "closed " << endl;

socket.perror_cpp("File 1 state ");
file_1 << "Hello file\n" << 75; //write to it
file_1.close(); //close it

fstream file_2;
file_2.open("file_2.txt"); //open a file
if (file_2.is_open())
cout << "open " << endl;
else
cout << "closed " << endl;
socket.perror_cpp("File 2 state ");
file_2 << "Hello file\n" << 75; //write to it
file_2.close(); //close it

The first "open" works, the second fails - consistently.
C++
<a href="http://www.cplusplus.com/reference/fstream/fstream/open/">fstream::open - C++ Reference</a>[<a href="http://www.cplusplus.com/reference/fstream/fstream/open/" target="_blank" title="New Window">^</a>]

From the above reference and its example (attached) it looks as fstream despite being advertised as "input / output"  still needs in/ out parameters. 

I am now successfully   using ifstream and ofstream. 
(May try plain fstream with  i/o parameters later )  

PS I woudl even question the necessity of usage of std::(fstream)  in the example. 

// fstream::open / fstream::close
#include <fstream>      // std::fstream

int main () {

  std::fstream fs;
  fs.open ("test.txt", std::fstream::in | std::fstream::out | std::fstream::app);
  fs << " more lorem ipsum";
  fs.close();
  return 0;
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 2-Feb-20 7:43am    
If you are creating a new file then you must use std::fstream::out, but you do not nead std::fstream::in.

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