Solved

write to a file

Posted on 2002-04-29
5
209 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
great answer.
thanks for all your help.
0
 

Author Comment

by:siunix
Comment Utility
I try:
fprintf(fptr, "fi");

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


thanks


0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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…
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.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now