Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home C-tutorials C file open example

Related Tutorials


 
 

Share on Google+Share on Google+

C file open example

Advertisement
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:

FILEOPEN.C

#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.");
  fclose(fp;
}

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.

MYFILE.txt

Download Source Code:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 5, 2009

Related Tutorials

Discuss: C file open example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
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.
Emmanuel
February 7, 2013
thnks

i got started with this, good intro
DMCA.com