473,396 Members | 2,011 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,396 software developers and data experts.

Help on File Operations

Hi All,

Can someone tell why the program is crashing in the call to Fun2. I could
able to create the filename, write into it and read it in Fun1 but the same
file if i open in Fun2 it is crashing. Is something that needs to be done to
NewFile object like destroying it, if so can someone tell how to do that. I
used ~NewFile() but it is throwing syntax error.

void main()
{
Fun1(filename);
Fun2(filename);
}

void CINSTALL :: Fun1(const std::string filename)
{
fstream NewFile;

if(!NewFile)
{
//prints error
}

NewFile.Open(filename.c_str(), ios::out);

//Now i write into the file.
....
....

//Now i can read the new file contents.

......

//I close the file
NewFile.close();

}

void INSTALL :: fun2(const std::string &fileName)
{

const char* fn = fileName.c_str();
int size = fileName.length();
std::string file = "";
for ( int i = 0; i < size; i++ )
{
if ( fn[i] == '\\' )
file += "\\\\";
else
file += fn[i];
}

ifstream _istream;
_istream.open(file.c_str()); //This statement is failing and the program
is getting crashed.

if(_istream)
{

}

}



Jul 22 '05 #1
1 1279
Venkat wrote:
Hi All,

Can someone tell why the program is crashing in the call to Fun2. I could
able to create the filename, write into it and read it in Fun1 but the same
file if i open in Fun2 it is crashing. Is something that needs to be done to
NewFile object like destroying it, if so can someone tell how to do that. I
used ~NewFile() but it is throwing syntax error.
You don't need to destroy it. It will automaticly be destroyed at the
end of scope (at the end of function, in this case).
void main()
int main()
const char* fn = fileName.c_str();
int size = fileName.length();
std::string file = "";
for ( int i = 0; i < size; i++ )
{
if ( fn[i] == '\\' )
file += "\\\\";
else
file += fn[i];
}
Remove the code above, you don't need it and it makes your program work
incorrectly. ( Why did you even think to open the same file with two
different names? )
_istream.open(file.c_str()); //This statement is failing and the program


And convert the line above with this:

_istream.open(fileName.c_str());
Jul 22 '05 #2

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

Similar topics

3
by: Peter | last post by:
Hello, Two newbie questions: 1) I have a javascript file with a function in it. From this function I want to access a variable in another javascript file -which is not inside a function. I...
1
by: AKF | last post by:
I am trying to use data structures to open a file (void display_Stock_Item(Stock_Item si);) from a text file. I also need to be able to write to the file and also to delete from the file using...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: treelife | last post by:
I'm getting and internal server error when | run the following mod_python script. I am actually trying to run Django. Script: from mod_python import apache def handler(req):...
1
by: klubbhead | last post by:
This part of my program needs to read from 4 parameter files and then displays them on the screen by clicking a different radio button. The program itself works, but my professor says that I have to...
8
by: ilikenwf | last post by:
I get 27 errors when I try to compile, none of which make sense to me, since the code looks good. I have my header file included, and all of my function definitions are in the class contained in the...
7
by: Steve Chow | last post by:
hi, i'm reading a binary data file, which is basically a list of 7296 offsets pointing to data blocks which represent scanlines containing three numbers; leftx, id, rightx. i can read these fine...
4
by: jjh5030 | last post by:
This is a programming assignment. You are asked to work with pointers. Be aware that error messages are often not very helpful when your pointers point to bad locations. Therefore, reserve...
0
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement...
2
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.