Thread: Searching a text file backwards

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to hear, but I have a few suggestions to make:
    • Break up your main function into smaller functions that do one thing and do it well.
    • #include <vector> and change buffer to a std::vector<char> into of doing manual memory management.
    • Declare variables near first use. If possible, declare them at the point where you can provide them with sensible initial values. In fact, you should change this:
      Code:
      ifstream inFile;	// Initiate the input file stream
      inFile.open("C:\\datafile.txt", ifstream::in);
      to a one-liner that initialises the object:
      Code:
      ifstream inFile("C:\\datafile.txt");
    • You only use the non-standard getch() at the end, presumably to pause the program. Since you are not using anything else from the non-standard <conio.h>, you might as well get rid of that header inclusion and use a more portable alternative to getch() for this purpose.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Your solution of overlapping buffers is a good one. I notice you're never really concerned with using the image number as a number; you just care about it matching. You could change your search string to hold the image number as well. That would get rid of a chunk of awkward code.

    Also, you must initialize imgNum to a value. Right now it contains a garbage value when it is compared against desiredImageNumber for the first time. You don't really need to new/delete your buffer of memory every time through the loop; that causes unnecessary memory thrashing of the heap. You should handle that outside of your loop.
    Code:
    	inFile.seekg (0, ios::end);	
    	eofPtrPosn = inFile.tellg();	
    	eofPtrPosn = eofPtrPosn; /* This line does nothing; perhaps you meant currentPtrPosn? */
    	currentPtrPosn = inFile.tellg(); // IF ONLY I KNEW HOW TO DO THINGS INITIALLY THEN DO A LOOP
    	while (desiredImageNumber != imgNum){
    You can use a for-loop if you prefer to have an initialization part to your loop:
    Code:
    for(eofPtrPosn = inFile.tellg(), currentPtrPosn = eofPtrPosn; desiredImageNumber != imgNum; ) {
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #18
    Registered User
    Join Date
    Aug 2010
    Posts
    14

    Smile

    Wow, I thought I had got enough help already, but thanks laserlight for adding even more comments about my programming style. I need all the help I can get!

    Quote Originally Posted by laserlight View Post
    [*]#include <vector> and change buffer to a std::vector<char> into of doing manual memory management.[/list]
    Sorry, I don't quite understand this sentence. Could you rephrase it maybe?

    Quote Originally Posted by laserlight View Post
    [*]You only use the non-standard getch() at the end, presumably to pause the program. Since you are not using anything else from the non-standard <conio.h>, you might as well get rid of that header inclusion and use a more portable alternative to getch() for this purpose.[/list]
    Good point. The only problem is I haven't yet found an alternative. I'll try and have a look around when I think of it. For now, getch() works to pause the program for me to see the command prompt!

  4. #19
    Registered User
    Join Date
    Aug 2010
    Posts
    14

    Talking Thanks for the further suggestions!

    Quote Originally Posted by pianorain View Post
    Your solution of overlapping buffers is a good one. I notice you're never really concerned with using the image number as a number; you just care about it matching. You could change your search string to hold the image number as well. That would get rid of a chunk of awkward code.
    Yes. I was quite chuffed when I completed the code. I learnt a lot even though I lost a few hairs . That's a good point about the search string - I'll have to change that.

    Quote Originally Posted by pianorain View Post
    Also, you must initialize imgNum to a value. Right now it contains a garbage value when it is compared against desiredImageNumber for the first time.
    That came up as a warning 10 min ago when I compiled with Visual Studio 2008 when I was trying to fit this into my whole project code - something which Dev C++ isn't as good at. Good pickup!

    Quote Originally Posted by pianorain View Post
    You don't really need to new/delete your buffer of memory every time through the loop; that causes unnecessary memory thrashing of the heap. You should handle that outside of your loop.
    Thanks for the tip. I didn't know you could do that.

    And thanks also for the suggestion about the for loop - I didn't know that could be done either.

    Thanks for all the tips, I have learnt a heap through people's generous feedback!

  5. #20
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Geek10 View Post
    Quote Originally Posted by laserlight View Post
    #include <vector> and change buffer to a std::vector<char> into of doing manual memory management.
    Sorry, I don't quite understand this sentence. Could you rephrase it maybe?
    The more you learn, the more you realize that having new's and delete's scattered through your code is a bad idea. Fortunately, a lot of standard classes handle this sort of dynamic memory for you.

    laserlight is pointing out that you can use a std::vector<char> instead of your current char* buffer. The main benefit here is memory safety. Right now if an exception occurred in your while loop or you changed your code to break out of the loop before the delete, you could end up with a memory leak because delete isn't called. Using a std::vector, the memory is guaranteed to be cleaned up once the vector leaves scope.
    Code:
    	while (desiredImageNumber != imgNum){
    		std::vector<char> buffer(bufferSize); 		// to store search search block of code
    		// ...
    		// read data into buffer
    		inFile.read (&buffer[0],bufferSize);
    		// ...
    	}
    Notice that no new or delete is necessary. I left the vector inside the loop so that you could see a direct comparison with your code, but it should really be declared outside the loop so that it is only created once.
    Quote Originally Posted by Geek10 View Post
    Good point. The only problem is I haven't yet found an alternative. I'll try and have a look around when I think of it. For now, getch() works to pause the program for me to see the command prompt!
    Believe it or not, Cprogramming.com has an FAQ section. It even has a main website. (For a long time, I thought the forums were the only thing here.) One of the FAQs: How do I get my program to wait for a keypress?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  2. Help with finding word frequency in a text file.
    By aeolusaether in forum C Programming
    Replies: 15
    Last Post: 04-04-2010, 09:59 PM
  3. Searching a text file for double quotation marks "
    By willie in forum C Programming
    Replies: 4
    Last Post: 11-23-2008, 02:00 PM
  4. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM

Tags for this Thread