473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Fstream problem

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

Jul 19 '05 #1
5 3232
"jbruno4000" <jb********@aol.com> wrote in message
news:20***************************@mb-m27.aol.com...
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.
Jul 19 '05 #2
Jacek Dziedzic wrote:
"jbruno4000" <jb********@aol.com> wrote in message
news:20***************************@mb-m27.aol.com...
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

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #3
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:3F**************@austin.rr.com...
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.

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.


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.
Jul 19 '05 #4
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;
}
Jul 19 '05 #5
jbruno4000 wrote:
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'.
Jul 19 '05 #6

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

Similar topics

3
by: Frédéric Manzanares | last post by:
hello, my problem: I want to habe one Class with write and read in a file. i have overloaded the operator >> and <<. class c_File { public : fstream fs;
6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
3
by: David Blasdell | last post by:
This appears very strange to me, I'm having a problem with a fstream object inside a class which is not being called directly from main (another class calls the class that contains the fstream...
3
by: ratzeel | last post by:
The following snippet code throws an error while compiling on SUN os.. Any idea how to resolve this... #include <iostream.h> #include <fstream.h> #include <math.h> #include <algorithm>...
2
by: NewToCPP | last post by:
I am having some trouble including "iostream" "fstream" to my code. I tried the following ways and base times it did not work. What is the problem? test.cc: ======= ..... #include...
5
by: ehui928 | last post by:
The following program is used to open a file in mode both read and append, but it has a problem that the file can't be opened. #include <iostream> #include <fstream> #include <cstdlib> ...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
2
by: jjcp | last post by:
I would like to say thanks in advance for insight anyone can shed on this for me. Long story short from time to time I need to us C++ to take a list of file and make an index out of them in...
3
by: shyam | last post by:
Hi All I want to know if there is any problem with using fstream.getLine(char*, int ) function. My problem is that when I read a file using it, the program aborts when it reads the...
3
by: Ioannis Vranos | last post by:
Hi, I am experimenting with fstream type, I have written the following code: #include <iostream> #include <fstream> #include <string> int main() { using namespace std;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.