Solved

write to a file

Posted on 2002-04-29
5
217 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

688 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