Here I have attached C code which will create Pascal triangle of any size and write that triangle to text file.
Download code :- http://www.4shared.com/file/bcjZbGtm/Second.html
This is customized & generalized code. You can select the length of pascal triangle to be created.Using this code, You will be able to understand file writing,looping & pointer concepts.
here is the output,
(1) Pointer & Dynamic Memory Allocation
In this code I am first asking user the length of pascal triangle and counting no of elements of pascal triangle. And then after doing dynamic memory allocation for that many elements.
(2) Looping concept
There are two loops,1 st loop will traverse through no of rows of pascal triangle & second row will traverse through no. of elements of pascal triangle.
Conditions ae like this, 1st & last element of each row is 1,and middle elements are sum of elements of previous row. i.e. if 2nd row is 1 2 1 then 3rd row will be 1 3 3 1.
(3) Writing to File
For Writing to file, first you have to open file in write mode and then using the file pointer reference you can write to file.
Data pointed by p will be printed to file having pointer fp.
(4) Reading From File
For Writing to file, first you have to open file in read mode and then can read using fscanf function.
fp : – file pointer for reading the specific file
and current byte or char or integer will be stored in temporary data variable once you execute fscanf function.