Unable to write to memory error

M

mattsniderppl

Hi, i'm relatively new to C++ from java and am having a difficult time
with pointers. I'm sure there is something simple that I am doing
wrong, but I can't seem to write this in a way that doesn't give me the
error "Unable to write to memmory." This is a static function that
receives a string that is the name of a file and an ifstream pointer.
It then concatenates the path string using the string.h function
strcat(char *, const char *) and then opens the infile. However, both
the strcat call and the open call returns the error message. Any ideas
what i'm doing wrong here? Thanks.

void STATICFUNCTIONS::OPENFILE(char * name, ifstream * infile) {
char * path = "C:\\Dev-Cpp\\projects\\exe\\";
strcat(path, name);
infile->open(path, ios::in);

if (!infile->is_open()) throw strcat("Error Opening ", path);
try {

} catch (char * str) {
cout<<str<<endl<<"Program will exit now"<<endl;
system("PAUSE");
exit(1);
}
}
 
H

Howard

Hi, i'm relatively new to C++ from java and am having a difficult time
with pointers. I'm sure there is something simple that I am doing
wrong, but I can't seem to write this in a way that doesn't give me the
error "Unable to write to memmory." This is a static function that
receives a string that is the name of a file and an ifstream pointer.
It then concatenates the path string using the string.h function
strcat(char *, const char *) and then opens the infile. However, both
the strcat call and the open call returns the error message. Any ideas
what i'm doing wrong here? Thanks.

I'm not sure what you mean that "both [calls] return the error message"...?
Neither call "returns" an error message. Did you mean that Windows (or
whatever OS) displays an error window?

In any case, see below:
void STATICFUNCTIONS::OPENFILE(char * name, ifstream * infile) {
char * path = "C:\\Dev-Cpp\\projects\\exe\\";

This defines a character array of a specific size, pointed to by the "path"
pointer.
strcat(path, name);

Now, you're trying to add data to the end of that string. But it's not big
enough!

You need to declare "path" as an array of char (not a char*), and make it
large enough to hold the largest path you'll expect to see. (Or even
better, a std::string!)
infile->open(path, ios::in);

Is there a reason you're passing in the file stream pointer, but opening it
here? Instead of having this whole function, why not just use a local file
stream variable wherever it is you need to use the file, and pass the path
to its constructor. That opens it for you (no need to call "open"), plus,
it will close it automatically when it goes out of scope.

-Howard
 
D

Dan Cernat

I'm not sure what you mean that "both [calls] return the error message"...?
Neither call "returns" an error message. Did you mean that Windows (or
whatever OS) displays an error window?

In any case, see below:
void STATICFUNCTIONS::OPENFILE(char * name, ifstream * infile) {
char * path = "C:\\Dev-Cpp\\projects\\exe\\";

This defines a character array of a specific size, pointed to by the "path"
pointer.
strcat(path, name);

Now, you're trying to add data to the end of that string. But it's not big
enough!

-Howard

Actually path, the way it is declared, is a pointer to a *const* string
and points to a ReadOnly area of memory, therefore any writing will
result in the exception encountered by the OP (Unable to write to
memmory).
Same goes for the second strcat.

Dan
 
B

Ben Pope

Hi, i'm relatively new to C++ from java and am having a difficult time
with pointers. I'm sure there is something simple that I am doing
wrong, but I can't seem to write this in a way that doesn't give me the
error "Unable to write to memmory." This is a static function that
receives a string that is the name of a file and an ifstream pointer.
It then concatenates the path string using the string.h function
strcat(char *, const char *) and then opens the infile. However, both
the strcat call and the open call returns the error message. Any ideas
what i'm doing wrong here? Thanks.

void STATICFUNCTIONS::OPENFILE(char * name, ifstream * infile) {
char * path = "C:\\Dev-Cpp\\projects\\exe\\";

This looks more like C than C++, use a std::string instead.
strcat(path, name);

Yes, this is C again. Use a std::string for concatenation. As already mentioned you're trying to modify a const char[] (the string literal), std::strings would solve the
requirement to allocate your own memory, check the buffer length for overflow, and generally faff around.
infile->open(path, ios::in);

if (!infile->is_open()) throw strcat("Error Opening ", path);
try {

What are you 'try'ing? Perhaps this should go up a line to enclose the bit that throws?
} catch (char * str) {
cout<<str<<endl<<"Program will exit now"<<endl;
system("PAUSE");
exit(1);
}
}

Ben
 
H

Howard

Dan Cernat said:
I'm not sure what you mean that "both [calls] return the error
message"...?
Neither call "returns" an error message. Did you mean that Windows (or
whatever OS) displays an error window?

In any case, see below:
void STATICFUNCTIONS::OPENFILE(char * name, ifstream * infile) {
char * path = "C:\\Dev-Cpp\\projects\\exe\\";

This defines a character array of a specific size, pointed to by the
"path"
pointer.
strcat(path, name);

Now, you're trying to add data to the end of that string. But it's not
big
enough!

-Howard

Actually path, the way it is declared, is a pointer to a *const* string
and points to a ReadOnly area of memory, therefore any writing will
result in the exception encountered by the OP (Unable to write to
memmory).
Same goes for the second strcat.

Dan


correct, thx
 
M

mattsniderppl

hm... thanks for the help.

Maybe the problem is i'm designing a pointless funciton. My program has
many files that need to be opened and read, so the goal of this
function is to streamline the open file proceedure. I want this
function to return an opened ifstream and handle the exception in case
there is an error opening the file.

I know this isn't the correct way to do this, but this is what i have
now.

void STATICFUNCTIONS::OPENFILE(string name, ifstream infile) {
string path = "C:\\Dev-Cpp\\projects\\exe\\";
path.append(name);
try {
infile.open(path.data(), ios::in);
system("PAUSE");
if (!infile.is_open()) throw path.insert(0, "Error Opening ");
} catch (string str) {
cout<<str<<endl<<"Program will exit now"<<endl;
system("PAUSE");
exit(1);
}
}
 
D

Default User

hm... thanks for the help.

Maybe the problem is i'm designing a pointless funciton. My program has
many files that need to be opened and read, so the goal of this
function is to streamline the open file proceedure. I want this
function to return an opened ifstream and handle the exception in case
there is an error opening the file.


Please quote a relevant portion of the previous message when replying.
To do so from the Google interface, don't use the Reply at the bottom
of the message. Instead, click "show options" and use the Reply shown
in the expanded headers.

Brian
 
O

Old Wolf

Maybe the problem is i'm designing a pointless funciton. My program has
many files that need to be opened and read, so the goal of this
function is to streamline the open file proceedure. I want this
function to return an opened ifstream and handle the exception in case
there is an error opening the file.

I know this isn't the correct way to do this, but this is what i have
now.

void STATICFUNCTIONS::OPENFILE(string name, ifstream infile) {

You need to pass by reference: ifstream &infile

This means that 'infile' in this function, refers to the same
ifstream as you passed in the calling function.

You say you come from Java where every non-trivial type is a
reference; so if you use references then you will find that
the behaviour is like what you are used to.

If the paramter is not a reference then it is a copy; for
example, 'name' in this function is a completely separate string
from whatever you passed in in the calling function.

The code above (ifstream infile) is actually illegal because
streams cannot be copied. But some compilers will allow it
anyway (with hard-to-predict results).
string path = "C:\\Dev-Cpp\\projects\\exe\\";
path.append(name);
try {
infile.open(path.data(), ios::in);

This is an error. path.data() points to the characters in path.
But ifstream::eek:pen requires a pointer to a C string (ie.
characters followed by a 0 to indicate the end of the string).

Also, the default opening mode for ifstream is 'in' so you do not
need to repeat that:

infile.open(path.c_str());
system("PAUSE");
if (!infile.is_open()) throw path.insert(0, "Error Opening ");
} catch (string str) {
cout<<str<<endl<<"Program will exit now"<<endl;
system("PAUSE");
exit(1);
}
}

Don't use exceptions just for the sake of it :)

infile.open(path.c_str();
if (!infile.is_open())
{
cout << "Error opening file " << path << endl;
exit(1); // Note: on some systems, 1 indicates success
}

However, calling exit() in a C++ program is usually a bad idea,
as it means objects will not be cleaned up (ie. destructors
are not called). You should exit by returning an error code from
main(), or throwing an exception (and if it is never caught then
the program will naturally abort), eg.

if (!infile.is_open())
throw runtime_error("Error opening file.");

It is good to throw exceptions that are derived from std::exception,
because then you can have a catch(exception &e) in a parent function
and it will catch all exceptions thrown by the child functions.
 

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
474,297
Messages
2,571,536
Members
48,282
Latest member
Xyprime

Latest Threads

Top