Home › Forums › C Programming › FILE HANDLING › Re: Re: FILE HANDLING
May 16, 2009 at 4:48 pm
#3577
GWILouisaxwzkla
Participant
could do in C++ ( C is similar ):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | <br /> /****************************************************************<br /> * File Name : c:programstempCG.cpp<br /> * Date : May 16, 2009<br /> * Comments : new project<br /> * Compiler/Assembler :<br /> * Modifications :<br /> *<br /> *<br /> *<br /> *<br /> *<br /> * Program Shell Generated At: 12:40:41 a.m.<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> <br /> #include <br /> #include <br /> //#include <br /> //#include <br /> //#include <br /> //#include <br /> <br /> using namespace std;<br /> <br /> <br /> //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /> <br /> void readArrayFromFile ( int * inputArray , char * fileName , int arrayLength );<br /> void writeArrayToFile ( int * outputArray , char * fileName , int arrayLength );<br /> <br /> //##################################################################################<br /> <br /> <br /> //main function ******************************<br /> <br /> int main ( )<br /> {<br /> int array [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };<br /> <br /> <br /> writeArrayToFile ( array , "c:\programs\data.txt" , 10 );<br /> readArrayFromFile ( array , "c:\programs\data.txt" , 10 );<br /> <br /> <br /> <br /> return 0 ;<br /> }<br /> <br /> <br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : readArrayFromFile<br /> Parameters :<br /> <br /> inputArray a(n) int * ( int * ) ,<br /> fileName a(n) char * ( char * )<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void readArrayFromFile ( int * inputArray , char * fileName , int arrayLength )<br /> {<br /> <br /> fstream file ( fileName );<br /> <br /> if ( file.fail() )<br /> {<br /> cout << "input file did not open" << endl;<br /> return;<br /> }<br /> <br /> int i = 0;<br /> while ( i < arrayLength )<br /> {<br /> file >> inputArray [ i ];<br /> i ++;<br /> }<br /> file.close();<br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : writeArrayToFile<br /> Parameters :<br /> <br /> outputArray a(n) int * ( int * ) ,<br /> fileName a(n) char * ( char * )<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void writeArrayToFile ( int * outputArray , char * fileName , int arrayLength )<br /> {<br /> <br /> ofstream file ( fileName );<br /> <br /> if ( file.fail() )<br /> {<br /> cout << "output file did not open" << endl;<br /> return;<br /> }<br /> <br /> int i = 0;<br /> while ( i < arrayLength )<br /> {<br /> file << outputArray [ i ] << " ";<br /> i ++;<br /> }<br /> file.close();<br /> <br /> return;<br /> }<br /> <br /> <br /> |