Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z
faqs.org - Internet FAQ Archives

Motif FAQ (Part 4 of 9)
Section - 104) How can I use a file as the text source for a Text widget?

( Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Part8 - Part9 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Property taxes ]


Top Document: Motif FAQ (Part 4 of 9)
Previous Document: 103) What if I have problems with the backspace/delete keys?
Next Document: 105) How can put Text in overstrike mode instead of insert?
See reader questions & answers on this topic! - Help others by sharing your knowledge

Answer: You can't do it directly like you can with the Athena Text widget.
Instead, read the text from the file into a string (all of it!) and then use
XmTextSetString.  Alternatively, read blocks of characters and add them at the
end of the text using XmTextInsertString.  The following is an excerpt from
Dan Heller's "file_browser.c":

/* file_browser.c -- use a ScrolledText object to view the
* contents of arbitrary files chosen by the user from a
* FileSelectionDialog or from a single-line text widget.
*/

struct stat statb;

/* make sure the file is a regular text file and open it */
if (stat(filename, &statb) == -1 ||
    (statb.st_mode & S_IFMT) != S_IFREG ||
    !(fp = fopen(filename, "r"))) {
if ((statb.st_mode & S_IFMT) == S_IFREG)
    perror(filename); /* send to stderr why we can't read it */
else
    fprintf(stderr, "%s: not a regular file\n", filename);
XtFree(filename);
return;
}

/* put the contents of the file in the Text widget by allocating
* enough space for the entire file, reading the file into the
* allocated space, and using XmTextFieldSetString() to show the file.
*/
if (!(text = XtMalloc((unsigned)(statb.st_size+1)))) {
fprintf(stderr, "Can't alloc enough space for %s", filename);
XtFree(filename);
fclose(fp);
return;
}

if (!fread(text, sizeof(char), statb.st_size+1, fp))
fprintf(stderr, "Warning: may not have read entire file!\n");

text[statb.st_size] = 0; /* be sure to NULL-terminate */

/* insert file contents in Text widget */
XmTextSetString(text_w, text);


User Contributions:

Comment about this article, ask questions, or add new information about this topic:




Top Document: Motif FAQ (Part 4 of 9)
Previous Document: 103) What if I have problems with the backspace/delete keys?
Next Document: 105) How can put Text in overstrike mode instead of insert?

Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Part8 - Part9 - Single Page

[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
kenton@rahul.net (Ken Lee)





Last Update March 27 2014 @ 02:11 PM