Logging to another directory in Linux

S

subaruwrx88011

Hello,
I am having alot of trouble. I am trying to log some information in
text file form to directory /data/logs.

Here is pseudo code

std::string const M_LOG_PATH = "\\data\\logs\\";

std::string m_log_file_name;
std::string filename = "log.txt"

m_log_file_name = M_LOG_PATH + filename;

std::fstream file;

file.open(m_log_file_name.c_str());

file << "text" << std::endl;

file.close();

So everything works except it puts the file in the executable
directory under the filename \data\logs\log.txt. I want it to put
logs.txt in directory /data/logs.

Can anyone help please???

Thanks
 
I

Ian Collins

subaruwrx88011 said:
Hello,
I am having alot of trouble. I am trying to log some information in
text file form to directory /data/logs.

Here is pseudo code

std::string const M_LOG_PATH = "\\data\\logs\\";
Why are you using back-slashes?
 
K

Kai-Uwe Bux

subaruwrx88011 said:
Hello,
I am having alot of trouble. I am trying to log some information in
text file form to directory /data/logs.

Here is pseudo code

std::string const M_LOG_PATH = "\\data\\logs\\";

Uhm, what about something like:

std::string const M_LOG_PATH = "/data/logs/";


BTW, it is a good practice to reserve all caps for macros.

std::string m_log_file_name;
std::string filename = "log.txt"

m_log_file_name = M_LOG_PATH + filename;

std::fstream file;

file.open(m_log_file_name.c_str());

file << "text" << std::endl;

file.close();

So everything works except it puts the file in the executable
directory under the filename \data\logs\log.txt.

That should not surprise you given that m_log_file_name reads something
like "\data\logs\logs.txt".
I want it to put logs.txt in directory /data/logs.

See above.


Best

Kai-Uwe Bux
 
R

Richard James

subaruwrx88011 said:
Hello,
I am having alot of trouble. I am trying to log some information in
text file form to directory /data/logs.

Here is pseudo code

std::string const M_LOG_PATH = "\\data\\logs\\";

std::string m_log_file_name;
std::string filename = "log.txt"

m_log_file_name = M_LOG_PATH + filename;

std::fstream file;

file.open(m_log_file_name.c_str());

file << "text" << std::endl;

file.close();

So everything works except it puts the file in the executable
directory under the filename \data\logs\log.txt. I want it to put
logs.txt in directory /data/logs.

Can anyone help please???

Thanks

The directory delimiters / and \ are not interchangeable between a
DOS/WINDOWS system and a UNIX system. You have to pick the right one for
your system. Since Linux is a form of UNIX it uses the / as the directory
delimiter. So your code should say
std::string const M_LOG_PATH="/data/logs/";
you don't need to escape a / slash.

It wrote it as a file "\data\logs\log.txt" because the file system sees the
\ as part of the file name not as command to change the directory.

If you intend for your program to be portable i.e. run on say Windows then
you might want to look into a library like boost.

Oh and please use alt.test to test your newsreader not a normal newsgroup.

Richard James
 
R

red floyd

Richard said:
The directory delimiters / and \ are not interchangeable between a
DOS/WINDOWS system and a UNIX system. You have to pick the right one for
your system. Since Linux is a form of UNIX it uses the / as the directory
delimiter. So your code should say
std::string const M_LOG_PATH="/data/logs/";
you don't need to escape a / slash.

It wrote it as a file "\data\logs\log.txt" because the file system sees the
\ as part of the file name not as command to change the directory.

[OT SYSTEM SPECIFIC]
Actually, most Windows I/O libraries will translate '/' separators, so
just use '/' for all systems.
[/OT SYSTEM SPECIFIC]
 
J

James Kanze

[OT SYSTEM SPECIFIC]
Actually, most Windows I/O libraries will translate '/' separators, so
just use '/' for all systems.
[/OT SYSTEM SPECIFIC]

That's not true. I don't think any I/O library translates the
filenames. They're just passed on to the OS, which interprets
it according to whatever its conventions are.

The convention which MS Windows uses is that either '/' or '\\'
are considered directory separators. Unless you pass the name
to a program which interprets the '/' differently, it works
exactly as under Unix. (And always has: this feature dates from
MS-DOS 2.0.)

In practice, I wouldn't hesitate to use '/' for system calls.
For invoking other programs, I'd be more careful, because
historically, '/' has also been the flag for options; if the
filename starts with a '/', most Windows programs will treat it
as an option, and not as a filename, and even if there is a '/'
in the filename, a lot of programs will truncate the filename
at the '/', and treat everything which follows as a file name.

Because of this, it's probably better to parametrize the
"preferred directory separator" somehow, and use '\' when
generating filenames under Windows, and '/' under Unix. (On the
other hand, things like ``#include "dir/header.hh"'' work well
on both systems.)
 
J

Juha Nieminen

subaruwrx88011 said:
So everything works except it puts the file in the executable
directory under the filename \data\logs\log.txt. I want it to put
logs.txt in directory /data/logs.

The problem and its solution seems so clearly obvious that I don't
even understand why you are asking. In fact, you answered your own
question in that sentence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,812
Messages
2,569,694
Members
45,478
Latest member
dontilydondon

Latest Threads

Top