• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Whenever you search in PBworks, Dokkio Sidebar (from the makers of PBworks) will run the same search in your Drive, Dropbox, OneDrive, Gmail, and Slack. Now you can find what you're looking for wherever it lives. Try Dokkio Sidebar for free.



This version was saved 14 years, 7 months ago View current version     Page history
Saved by Kenneth Finnegan
on April 22, 2008 at 10:52:07 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.



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

// This part uses stdio.h for the file io

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

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


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

system("nano tempfile"); 

// Now read back in the file



system() is a function that passes the string passed to it to the outside environment.  This is useful because so many other people have put countless hours of development into text editors.  Because of it there is NO reason why you should waste your time with this. 



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!

////////// OR //////////

char *text_editor = "nano";

char stringbuff[1000];

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



Generate a random filename to try and prevent any kind of file colision. 

sprintf(filename, "temp%d.txt", rand());



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

Comments (0)

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