473,503 Members | 3,045 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unable to write to memory error

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);
}
}

Jul 23 '05 #1
7 4904

<ma***********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Jul 23 '05 #2


Howard wrote:
<ma***********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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."

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

Jul 23 '05 #3
ma***********@gmail.com wrote:
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
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #4

"Dan Cernat" <dc*****@excite.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...


Howard wrote:
<ma***********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
> 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."


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


Jul 23 '05 #5
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);
}
}

Jul 23 '05 #6


ma***********@gmail.com wrote:
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

Jul 23 '05 #7
ma***********@gmail.com wrote:
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::open 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.

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2308
by: lawrence | last post by:
I'm probably missing something obvious, but I'm unable to write a file with this function. I've used my FTP software to set permissions to 777 on all the files in question. I've tried r, r+, w, and...
12
2710
by: InProcess | last post by:
Let me start by saying that, yes we know v6.5 is no longer supported by Microsoft and that moving to SQL2000 might resolve our problem. Actually the migration to SQL2000 is 1 to 2 months out and in...
4
9530
by: Abi | last post by:
We able to generate this error in our test environment and were able to research this enough to understand that the issue is NOT with an abject that needs to be serialized but rather as the stack...
0
789
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
2
5439
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
6
8137
Cintury
by: Cintury | last post by:
Hi all, I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql...
0
5063
by: nimjerry | last post by:
i am using db2 udb V 9 on aix 5.3 and in db2diag.log alwas has this error occurr below is sample message 2008-03-03-09.45.34.366406+420 I306667A443 LEVEL: Warning PID : 835622 ...
3
5537
by: =?Utf-8?B?TGV3aXMgTW90ZW4=?= | last post by:
Hello. We are having a problem here trying to compile C# applications. Only one developer has a problem where they attempt to compile the application and the compiler complains about being out of...
0
2654
by: ll | last post by:
I'm working with 'pure ASP upload' script which is designed to redirect to an alert/error message, should a file larger than the set limit be attempted to be uploaded. The problem is that, while...
0
7193
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
7067
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
7264
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,...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
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...
0
5562
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,...
0
4666
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...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.