I think it's with the Heap...but is it...

This is a discussion on I think it's with the Heap...but is it... within the C++ Programming forums, part of the General Programming Boards category; I have some code that is supposed to dig right into a file and return the text it finds (it's ...

  1. #1
    Meganan
    Join Date
    Oct 2005
    Posts
    13

    Lightbulb I think it's with the Heap...but is it...(solved)

    I have some code that is supposed to dig right into a file and return the text it finds (it's a big file). Unfortunately for me it compiles properly but crashes in the actual program; this is what leads me to beleive the problem is with the heap. My code is below:

    Code:
    char* file_get(char* fdest)
    {
    	fstream file(fdest,ios::in);
    	
    	char* temp = new char;
    	*temp = 0;
    	if(file.is_open())
    		while(!file.eof())
    			file.getline(temp, 1000000);
    	file.close();
    	return temp;
    }
    
    int main()
    {
            char* temp = file_get(dest);
            cout << temp;
            delete temp;
            return 0;
    }
    I have included a required headers etc. etc. So what am i doing wrong?
    Last edited by Meganan; 10-05-2005 at 10:33 PM.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Quote Originally Posted by Meganan
    I have some code that is supposed to dig right into a file and return the text it finds (it's a big file). Unfortunately for me it compiles properly but crashes in the actual program; this is what leads me to beleive the problem is with the heap. My code is below:

    Code:
    char* file_get(char* fdest)
    {
    	fstream file(fdest,ios::in);
    	
    	char* temp = new char;
    	*temp = 0;
    	if(file.is_open())
    		while(!file.eof())
    			file.getline(temp, 1000000);
    	file.close();
    	return temp;
    }
    
    int main()
    {
            char* temp = file_get(dest);
            cout << temp;
            return 0;
    }
    I have included a required headers etc. etc. So what am i doing wrong?
    wow, I think you're right. Everything looks fine, the only thing that caught my attention was getline(temp, 1000000)..try putting a simple 10 there and see if it works, if it doesn't its the heap.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Meganan
    Join Date
    Oct 2005
    Posts
    13
    Just as i expected it didn't work. that's because if you say getline(temp, 10) you're never going to reach the eof() are you? You're just going to keep on copying the same 10 char's to the same pointer. I'ts an infinite loop. But this is good because the debugger and now this have told me that the problem resides in returning temp.

    Please i still need to solve this there has to be a way to make this work.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    It looks pretty broken to me.

    Code:
    char *temp = new char;
    This allocates 1 char, not 1000000. How do you plan on putting 100000 into room for 1 char?
    Not to mention your eof check is wrong. Even if you allocated temp correctly you would just rewrite temp each iteration since getline does not append. You also don't ensure that you have enough room allocated for your entire file.
    Finally, you never define and give 'dest' a value.

  5. #5
    Meganan
    Join Date
    Oct 2005
    Posts
    13
    Good points although dest has been declared globally and there is enough room for the entire file. So how would i go about fixing this? i'm new to txt files in C++. Should i declare a string pointer??

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You might enjoy the simple examples presented here:
    http://www.cplusplus.com/doc/tutorial/tut6-1.html

  7. #7
    Meganan
    Join Date
    Oct 2005
    Posts
    13
    I've already looked there and it didn't help me or i didn't understand it.

  8. #8
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Code:
    char* file_get(char* fdest)
    {
    	fstream file(fdest,ios::in);
    	
    	char* temp = new char; // You allocated one char and 
    below you are trying to put 1000000 chars in there. 
    This wont work. 
    	*temp = 0;
    	if(file.is_open())
    // if this is the same with C. this line has some issues. Read on the FAQ section about feof()
    		while(!file.eof())
    			file.getline(temp, 1000000); // I dont think this will work even if you allocte 10000000 chars in memory its just too big. 
    	file.close();
    	return temp;
    }
    
    // Evrything is fine here/
    int main()
    {
            char* temp = file_get(dest);
            cout << temp;         
            delete temp;
            return 0;
    }

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    ...
    ...
    ...

    So cry about it. Come on show us some effort. How could it not help you and why are you not asking relavent questions if it didn't. Here's the exact program for what your's intended, straight from the site:

    Code:
    // reading binary file
    #include <iostream>
    #include <fstream>
    
    const char * filename = "example.txt";
    
    int main () {
    	char * buffer;
    	long size;
    
    	ifstream file (filename, 
    		ios::in | 
    		ios::binary | 
    		ios::ate);
    
    	// ios::ate is end-of-file pointer
    	// so tellg will now give yield size
    	size = file.tellg();
    	file.seekg(0, ios::beg);
    
    	// dynamically allocate space
    	// on the "heap" if you will
    	buffer = new char [size];
    
    	// read() the file into your buffer
    	file.read (buffer, size);
    
    	// clean ........ up
    	file.close();
    	delete[] buffer;
    	return 0;
    }

  10. #10
    Meganan
    Join Date
    Oct 2005
    Posts
    13
    really give me a minute...oohhh wait no the important part is that it is placed inside a function. I got it to work myself when it was just in the main(). Mabye i'll try to see if i can convert that into a function...i've done after just a few minutes of work. thankyou all i can't beleive i was so blind.
    Last edited by Meganan; 10-05-2005 at 10:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-10-2007, 10:49 PM
  2. Heap Work
    By AndyBomstad in forum C++ Programming
    Replies: 1
    Last Post: 05-16-2005, 12:09 PM
  3. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 04:03 AM
  5. stach and heap.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21