Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-21-2002, 01:41 AM   #1
LQ Newbie
Registered: Feb 2002
Location: New Hampshire
Distribution: redhat 7.2
Posts: 24

Rep: Reputation: 15
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.

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.
Old 02-21-2002, 02:09 AM   #2
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
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 */

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write a simple c prog to call a script RajRed Programming 1 10-05-2005 06:10 PM
how to write a simple c prog to call a script RajRed Linux - Software 1 10-05-2005 03:57 PM
Howto write XF86 Input Module? ZX_SA Programming 0 05-11-2005 10:11 AM
read the input file and write in the given format suchi_s Programming 8 12-17-2004 02:12 AM
Im trying to write a script or prog (newbie needs help desperatly!) bripage Programming 19 08-10-2002 10:06 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration