crashing on fstream open file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: crashing on fstream open file

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    5

    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.

  2. #2
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,538

    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" ?
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  3. #3
    Join Date
    Jan 2013
    Posts
    5

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427

    Re: crashing on fstream open file

    Quote Originally Posted by shadolink View Post
    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.
    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

  5. #5
    Join Date
    Jan 2013
    Posts
    5

    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

  6. #6
    Join Date
    Apr 1999
    Posts
    27,427

    Re: crashing on fstream open file

    Quote Originally Posted by shadolink View Post
    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.
    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.
    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
    Last edited by Paul McKenzie; January 22nd, 2013 at 04:28 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center