Home Forums C Programming FILE HANDLING Re: Re: FILE HANDLING

#3577
GWILouisaxwzkla
Participant

could do in C++ ( C is similar ):


/****************************************************************
* File Name : c:programstempCG.cpp
* Date : May 16, 2009
* Comments : new project
* Compiler/Assembler :
* Modifications :
*
*
*
*
*
* Program Shell Generated At: 12:40:41 a.m.
=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


#include
#include
//#include
//#include
//#include
//#include

using namespace std;


//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@

void readArrayFromFile ( int * inputArray , char * fileName , int arrayLength );
void writeArrayToFile ( int * outputArray , char * fileName , int arrayLength );

//##################################################################################


//main function ******************************

int main ( )
{
int array [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };


writeArrayToFile ( array , "c:\programs\data.txt" , 10 );
readArrayFromFile ( array , "c:\programs\data.txt" , 10 );



return 0 ;
}


/******************************* FUNCTION DEFINITION ******************************

Name : readArrayFromFile
Parameters :

inputArray a(n) int * ( int * ) ,
fileName a(n) char * ( char * )


Returns: Void type
Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void readArrayFromFile ( int * inputArray , char * fileName , int arrayLength )
{

fstream file ( fileName );

if ( file.fail() )
{
cout << "input file did not open" << endl;
return;
}

int i = 0;
while ( i < arrayLength )
{
file >> inputArray [ i ];
i ++;
}
file.close();
return;
}
/******************************* FUNCTION DEFINITION ******************************

Name : writeArrayToFile
Parameters :

outputArray a(n) int * ( int * ) ,
fileName a(n) char * ( char * )


Returns: Void type
Comments:



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void writeArrayToFile ( int * outputArray , char * fileName , int arrayLength )
{

ofstream file ( fileName );

if ( file.fail() )
{
cout << "output file did not open" << endl;
return;
}

int i = 0;
while ( i < arrayLength )
{
file << outputArray [ i ] << " ";
i ++;
}
file.close();

return;
}