crashing on fstream open file
other posts talk about the fopen function and don't really give any solutions to this
the var that has the file path is a char* and has been converted from a system::string^
it is completely valid when I paste it in windows bar.
Code:
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
if(textBox1->Text[0] == (char)'\0' || textBox2->Text[0] == (char)'\0'){
MessageBox::Show("Please select a valid m3u and path", "Problem",
MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
}
else{
button3->Text = "Working...";
ReadList();
}
}
public: int Form1::ReadList(){
ifstream pile;
int count = 0;
char inputbuff[100] ={'\0'},*tok,*folder;
String^ poo = textBox1->Text;
progressBar1->Maximum = File_Quant(poo);
pile.open(ConvertString(poo),ifstream::in);
public: char* Form1::ConvertString(String^ cnv){
int length = cnv->Length;
char *out = new char[length+1];
for(int i = 0;i < length;i++){
out[i] = (char) cnv[i];
out[length] = '\0';
}
return out;
}
it crashes right at the pile.open() function call when I put breakpoints in it says , debug assertion failed
and shows me the fclose.c file location and Expression: (stream != NULL)
I know there are other posts similiar but not exactly like this and they don't offer much in the way of a solution.
Thanks.
Re: crashing on fstream open file
Code:
String^ poo = textBox1->Text;
progressBar1->Maximum = File_Quant(poo);
pile.open(ConvertString(poo),ifstream::in);
What is the value of "poo" prior to "pile.open" ?
Re: crashing on fstream open file
C:\Users\V\Desktop\poo.m3u
which works when I type in windows explorer bar.
I thought that maybe you had to put the double slashes but thats only for string literals.
Re: crashing on fstream open file
Quote:
Originally Posted by
shadolink
other posts talk about the fopen function and don't really give any solutions to this
There are no "solutions" you just can look up on google. Did you really expect to write a program and not ever need to debug it? You need to debug your program -- that is how you find the solution.
Second, the code is not C++, at least not traditional C++ that relates to this forum. You are using Managed C++, and this is not the correct forum. This forum is for traditional/ANSI C++ using the Visual C++ compiler and libraries associated with Visual C++ such as MFC. There is a separate forum for Managed C++ questions.
Quote:
the var that has the file path is a char* and has been converted from a system::string^
There is no such thing as "system::string^" in this forum, again, go to the Managed C++ forum.
Code:
pile.open(ConvertString(poo),ifstream::in)
//...
int length = cnv->Length;
char *out = new char[length+1];
for(int i = 0;i < length;i++){
out[i] = (char) cnv[i];
out[length] = '\0';
}
return out;
This code, if it is to be looked at in terms of traditional C++, leaks memory, unless "open" somehow retrieves the pointer passed in and issues a "delete[]" somewhere. My bet is that it doesn't.
How are you going to call "delete[]" for the memory you allocated with "new[]"? When you call pile.open(), that pointer to the memory you allocated is gone and can never be retrieved, thus a memory leak.
Regards,
Paul McKenzie
Re: crashing on fstream open file
I've been debugging it
the problem is with fstream function call which is regular c++
I free fil later and did not post that part of the function
and fil has the right path at time of .open() call
and if I try a string literal its the same crash status
Re: crashing on fstream open file
Quote:
Originally Posted by
shadolink
I've been debugging it
the problem is with fstream function call which is regular c++
You are using Managed C++ to do string handling. If you just posted C++ code, then that is a different story, but you didn't. You are doing something to this strange "String^" that supposedly copies a character to a char array. As stated, there is no such thing as a "String^" in this forum, so showing code that uses it to describe your problem isn't going to help.
Quote:
I free fil later and did not post that part of the function
There is no way you can free that memory with the code you posted, unless inside of the .open() function, you are grabbing that pointer and calling delete[].
You are passing a temporary pointer to dynamically allocated memory as the first parameter to open(). Once that .open() function ends, that pointer is gone and can no longer delete[] what was allocated.
Quote:
if I try a string literal its the same crash status
So how would dynamically allocating the name change anything? The only thing it would able you to do is to alter the string within the function, otherwise you're not accomplishing anything by using a string literal or dynamically allocating memory. You actually made the problem worse by causing a memory leak, on top of the code crashing on you.
Regards,
Paul McKenzie