Solved

write to a file

Posted on 2002-04-29
5
215 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:siunix
5 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6979424
FILE *fptr;

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

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

fclose(fptr);
0
 
LVL 1

Expert Comment

by:Shivshankar
ID: 6979426
void g()
{
  FILE * f;
  f = fopen("/tmp/file1", "w");
  //Code for writing into the file
  //goes here...
  fclose(f);
}
0
 
LVL 1

Accepted Solution

by:
tapasmondal earned 50 total points
ID: 6979458
There are two options:
1. You want to creat a new file which having only these threee lines.
2. You want to add these threee lines in a existing file.


for 1. you will have to open this file in write mode.
like as follow:

void file_to_write()
{
FILE *fin;
fin = fopen("/tmp/file1","w");
fprintf(fin, "#! is line one");
fprintf(fin, "line2");
fprintf(fin, "line3 is good");

fclose(fin);
}

for 2. you will have to open this file in append mode.
like as follow:

void file_to_write()
{
FILE *fin;
fin = fopen("/tmp/file1","a");
fprintf(fin, "#! is line one");
fprintf(fin, "line2");
fprintf(fin, "line3 is good");

fclose(fin);
}



If you want to creat in binary mode, you have to replace "w" by "wb" and "a" by "ab".

Thanking you

tapas

0
 

Author Comment

by:siunix
ID: 6979540
great answer.
thanks for all your help.
0
 

Author Comment

by:siunix
ID: 6982251
I try:
fprintf(fptr, "fi");

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


thanks


0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question