Originally Posted by
kevintse
I was reading the book by Petzold, and there was indeed an example showing how to create scroll bars and how to scroll text. but the text he was using was not read from a text file, it was just an array whose number of lines was known before he run the program, and he didn't deal with word break cause the program he was demonstrating was displaying something like a table, or a spread sheet.
You can use the Sysmets3 example with a dynamic struct array instead of the static struct that he is using in the example. For instance, the following code can be used with the example. This code reads in all the lines from an ASCII text file. You would do all your preliminary processing such as word breaks etc. when loading the file into the struct array. I've already implemented the following in sysmets3 to verify that it works.
Code:
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
struct node {
TCHAR * szSentence ;
};
struct node **sysmetrics = NULL;
ULONG NUMLINES = 0;
void AddSentenceToStruct( char *pSentence)
{
sysmetrics = (struct node **)realloc(sysmetrics, (NUMLINES + 1) * sizeof(struct node *));
sysmetrics[NUMLINES] = (struct node *)malloc(sizeof(struct node));
sysmetrics[NUMLINES]->szSentence = strdup(pSentence);
NUMLINES++;
}
int main(int argc, char **argv)
{
int iIndex = 0;
FILE *hFile;
char szBuffer[MAX_PATH+1];
hFile = fopen ( argv[1], "rb" );
if ( hFile == NULL ) {
printf("Unable to open the file\n");
return -1;
}
while( fgets(szBuffer, MAX_PATH, hFile) != NULL)
{
fgets(szBuffer,MAX_PATH,hFile);
AddSentenceToStruct(szBuffer);
}
fclose (hFile);
for(iIndex = 0; iIndex < NUMLINES; iIndex++) {
printf("[%d]->str: %s", iIndex, sysmetrics[iIndex]->szSentence);
}
/* free all sysmetrics elements */
for(iIndex = 0; iIndex < NUMLINES; iIndex++) {
free(sysmetrics[iIndex]->szSentence);
free(sysmetrics[iIndex]);
}
free(sysmetrics);
return 0;
}