File Processing in C - PowerPoint PPT Presentation

About This Presentation
Title:

File Processing in C

Description:

File Processing in C++ ... check for end-of-file using the eof() member function close the file when ... that copies a content of a file into a one-dimensional array. – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 16
Provided by: Nadia154
Category:

less

Transcript and Presenter's Notes

Title: File Processing in C


1
File Processing in C
  • Data is stored in files so that it may be
    retrieved for processing when needed

2
Reading data from files using C
  • The commands for reading data from a file are
    (for the most part) very similar to those used to
    read data from the keyboard.
  • In order for your program to read from the file,
    you must
  • include the fstream header file
  • declare a variable of type ifstream
  • open the file
  • read from the file
  • after each read, check for end-of-file using the
    eof() member function
  • close the file when access is no longer needed
    (optional, but a good practice)

3
Reading data from files using C
  • includeltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • void main()
  • ifstream indata // indata is like cin
  • int num // variable for input value
  • indata.open("example.txt")
  • indata gtgt num
  • while ( !indata.eof() )
  • cout ltlt "The next number is " ltlt num
    ltlt endl
  • indata gtgt num // sets EOF flag if
    no value found
  • indata.close()
  • cout ltlt "End-of-file reached.." ltlt endl

4
Reading data from files using C
  • The result

5
Reading data from files using C
  • includeltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • void main()
  • ifstream indata // indata is like cin
  • char letter // variable for input value
  • indata.open("example.txt") // opens the
    file
  • indata gtgt letter
  • while ( letter ! 'Q' )
  • cout ltlt letter
  • indata gtgt letter
  • cout ltlt endl
  • indata.close()
  • cout ltlt "End-of-file reached.." ltlt endl

6
Reading data from files using C
  • The result

7
Writing data in the files using C
  • In order for your program to writein the file,
    you must
  • include the fstream header file
  • declare a variable of type ofstream
  • open the file
  • Writ in the file
  • close the file when access is no longer needed
    (optional, but a good practice)

8
Writing data in the files using C
  • include ltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • void main ()
  • ofstream myfile
  • myfile.open("example.dat")
  • myfile ltlt "Writing this to a file.\n"

9
Writing data in the files using C
10
Input/Output with files
  • include ltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • void main()
  • // To write in a file
  • ofstream obj_out("file_05.txt")
  • int x
  • while(cin gtgt x)
  • obj_out ltlt x ltlt endl
  • // To stop writing to a file, press CTRLZ
    then ENTER
  • // To read from a file
  • ifstream obj_in("file_05.txt")
  • while(obj_in gtgt x)
  • cout ltlt "X from the file is " ltlt x ltlt
    endl
  • coutltltendl

11
Input/Output with files
12
Example
  • Q1 Write a program that copies a content of a
    file into a one-dimensional array. Assume that
    the content of the file is integers.

13
Solution
  • includeltiostreamgt
  • includeltfstreamgt
  • using namespace std
  • void main()
  • int x, c0
  • ifstream fin1("d\\test.txt", iosin)
  • while(fin1gtgtx)
  • c
  • ifstream fin2("d\\test.txt", iosin)
  • int a new intc
  • for(int i0 iltc i)
  • fin2 gtgt ai
  • // To print the content of the array
  • for(int i0 iltc i)
  • cout ltlt ai ltlt endl

14
Example
  • Q2 Write a program that copies a content of a
    file into a one-dimensional array. Assume that
    the content of the file is string.

15
Solution
  • includeltiostreamgt
  • includeltfstreamgt
  • using namespace std
  • void main()
  • string x int c0
  • ifstream fin1("test.txt", iosin)
  • while(fin1gtgtx)
  • c
  • fin1.close()
  • ifstream fin2("test.txt", iosin)
  • string a new stringc
  • for(int i0 iltc i)
  • fin2 gtgt ai
  • // To print the content of the array
  • for(int i0 iltc i)
  • cout ltlt ai ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com