944,054 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5469
  • C RSS
Jul 18th, 2005
0

Declaration of dynamic pointer array puzzle.

Expand Post »
Hello, everyone.

Today, I'm working on giving myself a headache.

The reason is simple. I'm rusty on, or maybe ignorant of, dynamic declaration of arrays.

What I'm doing is MFC related, but I've got the MFC questions ironed out for the most part. Now, I've got to make a good storage system for CStrings.
I want to use an array of pointers to CStrings. The problem is, I don't know how many pointers I'll need until after I've opened a text file.
But wait, it's not that simple. I've written an int-returning '\n' counter, and it goes-a-like this:

  1. int textlinecount(FILE* fp)
  2. {
  3. int counted = 0;
  4.  
  5. while(!feof(fp))
  6. {
  7. if(getc(fp)=='\n'||feof(fp))
  8. counted++;
  9. }
  10. rewind(fp);//Return to start of file for use by textreadandupdate.
  11. return counted;
  12. }
<< moderator edit: added [code][/code] tags -- learn to do so yourself >>

That should work just fine. What is wrong, though, is that this, and the CString pointer array, are part of the same class declared at startup, before a file is loaded.
Then, I use this to read in the file information.

  1. BOOL TextReadAndUpdate(FILE* filetoread)
  2. {
  3. char str[50];
  4. while(!feof(filetoread))
  5. {
  6. for(int i=0;i<50;i++)
  7. {
  8. str[i]=getc(filetoread);
  9.  
  10. if(str[i]=='\n'||feof(filetoread))
  11. {
  12. if(feof(filetoread)&&(i<50))
  13. {
  14. while(i<50)
  15. {
  16. str[i] = ' ';
  17. i++;
  18. }
  19. }
  20. txtContents = (CString)str;
  21. UpdateAllViews(NULL);
  22. break;
  23. }
  24.  
  25. }
  26.  
  27. }
  28.  
  29. return TRUE;
  30. }
<< moderator edit: added [code][/code] tags >>

Given the nature of MFC and not wanting to make the information array exist outside this 'document' class, how do I dynamically allocate a number of pointers after the class is... er... loaded..declared..initialized... by the program?

I think I should include 'CString *lineptrs;' in the header file, but I'm not sure how to continue after that.

Thank you for your time.
Similar Threads
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 18th, 2005
0

Re: Declaration of dynamic pointer array puzzle.

The solution that comes to mind is a doubly-linked list.

That means trouble.
Lessee... head, tail, frame transverse head, frame transverse tail... yipeeskipee. There go my hopes of easy pointer arithmetic to control the view...



Upon yet further reflection:
This is the best way to implement the program, as it allows me to add more functionality that is going to be required in future updates with very little fuss.

No more whining.

Status of Puzzle:
Resolved.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: how to create a vector of type structure
Next Thread in C Forum Timeline: easy question in C...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC