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



Home C-tutorials C file read example

Related Tutorials


 
 

Share on Google+Share on Google+

C file read example

Advertisement
This section demonstrates you to read a line from the file. You can see in the given example, we prompt the user to enter the name of the file to read.

C file read example

     

This section demonstrates you to read a line from the file. You can see in the given example, we prompt the user to enter the name of the file to read. The library function gets() get the entered file from the console. The type FILE stores the information related to file stream. The file pointer *f calls the function fopen() which will open the entered file. If no error occurred while opening the file then fgets(st, 60, p)  reads the data from the file. In case if error occurred, it will displays the error message.

strlen(st)- This function determines the length of the string .

Here is the code:

FILEREAD.C

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(char st[80]) {

  FILE *p;
  char file[80];
  printf("Open File: ", &file);
  gets(file);
  p = fopen(file, "r");
  if (p) {
  fgets(st, 60, p);
  st[strlen(st)] 0;
  printf("The data of the file is: %s\n", st);
  getch();
  else {
  printf("File does not exist.\n");
  }
  fclose(p);
}

Output will be displayed as:

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 read example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Vess Georg
February 12, 2013
Example don't work

1. The output from source code and showed output are different: - in source code function printf("Open file: ..... is before filename reading with gets(file). - showed output hasn't a line with input a filename - gets() is a function with echo and character input is displaying on the screen. 2. In source code has an error with printf - this printf doesn't output filename: - no %s - need to be printf("Open file: %s", ...) - %s need a parameter of type *char but the current parameter is a **char (&file is a pointer to parameter 'file' and 'file' is a *char). As a result we will have output a sequence of symbols, started from address of 'file' variable in memory and nobody can know the length of sequence (somewhere program will find a zero-byte and will stop).
charmin
January 28, 2012
c progamming

thank u for giving me the source code
Pawan Kumar
January 31, 2012
FileRead.c

This above file read code doesn't work.Please help me to correct it.
Anubhab91
February 20, 2012
your post

Thank you. Your post was useful.
falco
September 1, 2012
c language programming

how can I write a programme that will create a file that can store CV 's.
ali
December 30, 2012
help me

if I want to make a map for a C program how I can make pages for my program map.please,help me how I can go to next page of map.very very thanks:D
DMCA.com