| 
View
 

TextEditing

This version was saved 16 years, 6 months ago View current version     Page history
Saved by Kenneth Finnegan
on April 22, 2008 at 10:45:25 pm
 

This trick is useful when you're expecting a variable number of lines of input from the user and want to let them go back and edit it as they like.  It's also great when you want to have something in the input field to start with, but can be edited as well.

 

Code:

// Initialize what you want the user to be presented with

// This part uses stdio.h

FILE *fp = fopen("tempfile", "w");

fprintf(fp, "Write your life story here");

fclose(fp);

 

// include stdlib.h for system(char *)

system("nano tempfile"); 

 


Extensions:

The text editor nano is hard coded in this version, a better implementation would be to make it a macro or string so a user could easily change it to their favorite text editor (ie vim or emacs).

#define TEXT_EDITOR "nano "

system(TEXT_EDITOR "filename");

// NOTE: This uses the string concatenation trick, don't freak out!

 

char *text_editor = "nano";

char stringbuff[1000];

sprintf(stringbuff, "%s filename", text_editor);

system(stringbuff);

 


Sources:

The C Programming Language 2nd Ed., Kernighan & Ritchie, pg. 160-162, 253.

Comments (0)

You don't have permission to comment on this page.