LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C prog..except input ,write to textfile?? (http://www.linuxquestions.org/questions/programming-9/c-prog-except-input-write-to-textfile-14750/)

kato678 02-21-2002 01:41 AM

C prog..except input ,write to textfile??
 
Hi. Really new to Linux and a wee bit familiar with C. I want to write a small code that will ask user his/her name when run and then write it to a text file called name.txt . that's it.
thnks.
brian :)


i know the C code part but I dont know how I would write to a text file on the disk. I mean I know the printf, scanf parts, etc



thnks again.

Malicious 02-21-2002 02:09 AM

scanf() will handle the input. To write to a file on disk, you must open a file and use fprintf()

char *nm = "Malicious";
FILE *nf;

/* open a file in the current working
directory named "names.txt" */
nf = fopen("names.txt", "w");
/* write a name to it */
fprintf(nf, "%s\n", nm);
/* close it */
fclose(nf);

Let me know when you need some more homework done. :-)


All times are GMT -5. The time now is 12:32 PM.