Help on File Operations

V

Venkat

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::eek:ut);

//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 == '\\' )
file += "\\\\";
else
file += fn;
}

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

if(_istream)
{

}

}
 
A

Aggro

Venkat said:
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 == '\\' )
file += "\\\\";
else
file += fn;
}


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

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top