?
Solved

write to a file

Posted on 2002-04-29
5
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

741 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