Home › Forums › C Programming › FILE HANDLING
- This topic has 1 reply, 2 voices, and was last updated 15 years, 7 months ago by GWILouisaxwzkla.
Viewing 1 reply thread
- AuthorPosts
- May 15, 2009 at 1:20 am #2197TessaAliydParticipant
Can someone plz!! assist me on how to write data from an integer array to a file and how to read data from a file into an integer array. Please I really Need help asap. Oh the language is C or C++, BUT Preferably ‘C’
- May 16, 2009 at 4:48 pm #3577GWILouisaxwzklaParticipant
could do in C++ ( C is similar ):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124<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 />
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.