Link to home
Start Free TrialLog in
Avatar of siunix
siunix

asked on

write to a file

Hi,
   How can I write a couple of lines to a file using c?
ex:  I want a simple c program which will write three lines to a file call
/tmp/file1 under solaris.

after I run the c program, it should write the three lines to the /tmp/file1

/tmp/file1 contains:
#! is line one
line2
line3 is good

thanks in advance
Avatar of kotan
kotan
Flag of Malaysia image

FILE *fptr;

fptr = fopen("w", "/tmp/file1");

fprintf(fptr, "#! is line one");
fprintf(fptr, "line2");
fprintf(fptr, "line3 is good");

fclose(fptr);
Avatar of Shivshankar
Shivshankar

void g()
{
  FILE * f;
  f = fopen("/tmp/file1", "w");
  //Code for writing into the file
  //goes here...
  fclose(f);
}
ASKER CERTIFIED SOLUTION
Avatar of tapasmondal
tapasmondal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of siunix

ASKER

great answer.
thanks for all your help.
Avatar of siunix

ASKER

I try:
fprintf(fptr, "fi");

and it does not write "fi" to the file.
is there a fix for this?


thanks