Home Forums C Programming FILE HANDLING

Viewing 1 reply thread
  • Author
    Posts
    • #2197
      TessaAliyd
      Participant

      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’

    • #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;
      }


Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.