Fstream problem

J

jbruno4000

I'm having 2 problems and hope you can help:

Fist Problem: Please see the code segment bellow. For this application I need
to access an input file and also an output file, however, when I include the
statements to access to access the output file, I get an access violation
error. When I comment out the output file statements, the program runs fine.

Code segment:

int main()
{
int choice = 1; // To store menu option selected by user
ifstream infile; // input file access
// ofstream outfile;
char* inputFile; // input file
char* outputFile; // outputFile
NodePtr head; // External pointer to head of linked
list
NodePtr currPtr; // Points to current node
NodePtr newNodePtr; // points to newest node
char noun[30], partNum[20], locationID[10]; // temp storage for file
input data
int qtyInStock, restockL; // " " "

inputFile = "mystock.txt";
outputFile = "update.txt";
infile.open(inputFile);
if(!infile)
{
cout << "Could not open file: " << inputFile << endl;
return 1;
}
// outfile.open(outputFile);
// if(!outfile)
// {
// cout << "Could not open file: " << outputFile << endl;
// return 1;
// }

SecondProblem : In reading from the intput file, I simply want to say:

infile >> data;
while(infile)
{
......
.......
infile >> next data;
}

When I do it this way, I get stuck in an infinite loop. It reads all the data
in the file but instead of exiting the loop, it continues to input the last
line indefinitely. To overcome the problem I've had to enter a '-1' for two
integer data items on the last line of the file and use that as the test for
end of file. I need to be able to do it the right way, so please help! Here's
the code segment:

infile >> noun >> partNum >> locationID >> qtyInStock >> restockL;
while((restockL != -1) && (qtyInStock != -1))
{
newNodePtr = new SupplyItem;
strcpy(newNodePtr->nomenclature, noun);
strcpy(newNodePtr->partNumber, partNum);
strcpy(newNodePtr->locID,locationID);
newNodePtr->quantityInStock = qtyInStock;
newNodePtr->restockLevel = restockL;
currPtr->link = newNodePtr;
currPtr = newNodePtr;
infile >> noun >> partNum >> locationID >> qtyInStock >> restockL;
}
currPtr->link = NULL;

Thanks for taking the time.

Josh
 
J

Jacek Dziedzic

jbruno4000 said:
I'm having 2 problems and hope you can help:
[...]
I get an access violation error.
Definitely.

When I comment out the output file statements, the
program runs fine.

By sheer chance. You forget to allocate memory for
your inputFile and outputFile character strings. By saying
"char* inputFile" (same for outputFile) you declare a
pointer to a char, nothing more. You should use arrays
like in your latter variables like noun[30], or, better,
switch to std::string's instead.

Also you can't assign (C-style) strings with "=", like
you do in inputFile="mystock.txt". What you're doing
now is copying pointers to (I think) temporaries, not the
strings themselves as you have probably intended.

The bottom line is... read on std::strings or on
C-style strings (null-terminated character arrays).

HTH,
- J.
 
A

Artie Gold

Jacek said:
I'm having 2 problems and hope you can help:
[...]
I get an access violation error.

Definitely.


When I comment out the output file statements, the
program runs fine.


By sheer chance. You forget to allocate memory for
your inputFile and outputFile character strings. By saying
"char* inputFile" (same for outputFile) you declare a
pointer to a char, nothing more. You should use arrays
like in your latter variables like noun[30], or, better,
switch to std::string's instead.

Erm, that's not correct. The OP was assigning the address of a
string literal to a char * -- which is not a problem at all.
Also you can't assign (C-style) strings with "=", like
you do in inputFile="mystock.txt". What you're doing
now is copying pointers to (I think) temporaries, not the
strings themselves as you have probably intended.

See above.
The bottom line is... read on std::strings or on
C-style strings (null-terminated character arrays).

HTH,
--ag
 
J

Jacek Dziedzic

Artie Gold said:
Jacek Dziedzic wrote:
[...]

Erm, that's not correct. The OP was assigning the address of a
string literal to a char * -- which is not a problem at all.

Right, my bad. I somehow thought that the string literal
dies the next moment, but I guess it doesn't.
See above.

What I meant is you copy pointers to C-style strings
that way, not the C-style strings themselves.

The bottom line is... I "misthought" the string literal
was a temporary, which makes my reasoning incorrect
and the original question open. Sorry for the mess.

- J.
 
J

jbruno4000

The problem is worst then I thought. I'm using borland 5.5 and I've been having
problems handling strings, so I entered the simple code bellow and when I run
it, I get an access violation and it crashes!

Is it a problem with my compiler? Is it possible somethings wrong with my
'string.h'? I've done work with strings on this same compiler and there were no
problems.

Any ideas?

~~~~~~~~~~~~~~~~~~~~~~~~

#include <iostream>
#include <string>

using namespace std;

int main()
{
string str_1;
string str_2;
string str_3;

str_1 = "Today";
str_2 = " is Sunday";
str_3 = str_1 + str_2;
cout << str_3 << endl;

return 0;
}
 
?

=?ISO-8859-1?Q?Christian_Brechb=FChler?=

jbruno4000 said:
The problem is worst then I thought. I'm using borland 5.5 and I've been having
problems handling strings, so I entered the simple code bellow and when I run
it, I get an access violation and it crashes!

Is it a problem with my compiler? Is it possible somethings wrong with my
'string.h'? I've done work with strings on this same compiler and there were no
problems.

Any ideas?

~~~~~~~~~~~~~~~~~~~~~~~~

#include <iostream>
#include <string>

using namespace std;

int main()
{
string str_1;
string str_2;
string str_3;

str_1 = "Today";
str_2 = " is Sunday";
str_3 = str_1 + str_2;
cout << str_3 << endl;

return 0;
}

With g++ 3.2, your program compiles and runs fine; it outputs "Today is Sunday
". Something must be wrong with your implementation (or installation). BTW, in
my case, the file is called '/usr/include/c++/3.2/string' -- no '.h'.
 

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,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top