C file open example

The header file provides several functions related to file operations. Here we are going to open a file using library function fopen().

C file open example


This section demonstrates you how to open a file in C. 

The header file <stdio.h> provides several functions related to file operations. Here we are going to open a file using library function fopen(). You can see in the given example, we have used the type FILE in order to store the information related to file stream. Then we create a file pointer and call the function fopen("MYFILE.txt", "a") using append mode a. This will open the file MYFILE.txt and allow to perform write operations. After that the fprintf() function write the string to the file MYFILE.txt

Here i am using TurboC++ IDE and my source file is in the C:\TC\BIN directory and the output file MYFILE.txt will generate in the same bin directory.

Here is the code:


#include <stdio.h>
#include <conio.h>
void main() {
  FILE *fp;
  fp = fopen("MYFILE.txt""a");
  fprintf(fp, "%s\n ""Hello World, Where there is will, there is a way.");

When you run the above example, you will find that the file MYFILE.txt  is created and contains string as 
Hello World
Where there is will, there is a way.


Download Source Code:

Share on Google+Share on Google+

C file open example

Posted on: February 5, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: C file open example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Tebogo Makola
April 17, 2011
Programming in C

Write a C program that reads in the Y values of a function provided in data.txt. The Y values correspond to an X domain ranging from -3 to 6 with a step size of 0.01. The program must use a function that performs the numerical differentiation method described below to calculate the derivative of the input data and write the resulting X and Y values to a CSV file. Corresponding X and Y values must be separated by a comma and subsequent data points must be separated by new lines. In our case the size of h is our step size and if we perform this calculation between each data point we will get the derivative of the function. â?¢ A function must be used to calculate the derivative between 2 values, it must accept the 2 Y values currently being evaluated and return the derivative at that point. â?¢ When a file is opened it must be confirmed that the file was opened correctly. â?¢ The lower and upper bound values and step size must be defined as constants Next the produced CSV file must be imported into Microsoft Excel and the X and Y data must be plotted in a chart. The axes must be labelled appropriately and the chart must be given a title. Once the data is plotted a trendline of the appropriate type must be fitted to the data. Finally you must calculate the variance of the Y values imported into Excel without using the built in variance function. You may only use the built in SUM and AVERAGE functions.
February 7, 2013

i got started with this, good intro