CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2015
    Posts
    515

    dump a large c arrays of short and float type

    Hello,

    Im pulled into some new work, where i am asked to dump a large number of c arrays (some are float, and some short) to text file. All of them have same size.

    Ive written following code. Not sure it looks very simple.

    Any comments is very much appreciated. Im not sure whats the best way to dump, visualise and store such huge arrays.

    Thanks a ;lot
    pdk
    Last edited by pdk5; February 1st, 2025 at 04:01 PM.

  2. #2
    Join Date
    Nov 2018
    Posts
    142

    Re: dump a large c arrays of short and float type

    > unsigned long long llArraySize
    Make all these size_t, not unsigned long long
    It's the preferred type for measuring sizes of things, such as array lengths.

    Then make your loop subscripts the same type.

    > const std::string sFileName
    Make this a reference parameter, like const std::string &sFileName

    > myfile << "\t " << (short)pArray[i];
    Remove the cast.
    A somewhat moot point on your array of short, but on your array of float, it's a disaster of data loss.

    It might be a text file, but if you have a single line of millions of characters, most other text processing programs (like an editor) will baulk at trying to deal with it.

    This takes up no more space (unless you're on Windows, then it's +1 char per line), but is far easier to deal with in other programs.
    myfile << pArray[i] << "\n";

  3. #3
    Join Date
    May 2015
    Posts
    515

    Re: dump a large c arrays of short and float type

    Thankyou very much Salem for the comments.. Very helpful

    Btw, regarding making "unsigned long long" to size_t, is it safe to do so ? Because i read somewhere size_t can hold upto "unsigned long int"
    Last edited by pdk5; February 1st, 2025 at 09:04 AM.

  4. #4
    Join Date
    Nov 2018
    Posts
    142

    Re: dump a large c arrays of short and float type

    > Because i read somewhere size_t can hold upto "unsigned long int"
    That depends entirely on your machine and compiler.

    Even if unsigned long long had a massive numeric range, your entire standard library (malloc / new / STL containers) are restricted by your compiler vendors choice of size_t.

  5. #5
    Join Date
    May 2015
    Posts
    515

    Re: dump a large c arrays of short and float type

    I am planning to add template, to reduce the repeated code.

    Code:
    class DumpEXAMPLE
    {
    private:
        DumpEXAMPLE() {}
    
    public:
        static DumpEXAMPLE& getInstance()
    	{
            static DumpEXAMPLE theInstance;
            return theInstance;
        }
    
    	void DumpAllParams
    	(
    		const BSparams & bs, const short *pTerrainHeightMAP, const short *pClassMAP, const short *pHgtMAP, const short *pDnsMAP, 
    		const float *pLossdB, const float *pAngle, const float *pEarthSurfaceAttenuationWithoutAknifesMAP,const unsigned long long llArraySize
    	) const;
    
    	void DumpBSBSparams(const BSparams & bs) const;
    	void DumpUArray(const short *pUArray, const unsigned long long llArraySize) const;
    	void DumpVArray(const short * pVArray, const unsigned long long llArraySize) const;
    	void DumpWArray(const short * pWArray, const unsigned long long llArraySize) const;
    	void DumpXArray(const short *pXArray, const unsigned long long llArraySize) const;
    	void DumpYArray(const float *pYArray, const unsigned long long llArraySize) const;
    	void DumpZAraay(const float *pZAraay, const unsigned long long llArraySize) const;
    	void DumpQArray(const float *pQArray, const unsigned long long llArraySize) const;
    	
    	//template <class T> void DumpBasicArray(const T *pArray, const unsigned long long llArraySize, const std::string sFileName ) const;
    	void DumpToShortArray(const short *pArray, const unsigned long long llArraySize, const std::string sFileName) const;
    	void DumpToFloatArray(const float *pArray, const unsigned long long llArraySize, const std::string sFileName) const;
    };
    
    void DumpEXAMPLE::DumpAllParamsPredictArray(
    	const BSparameters &bs, const short * pTerrainHeightMAP, const short * pClassMAP, const short * pHgtMAP, const short * pDnsMAP, const float * pLossdB,
    	const float * pAngle, const float * pEarthSurfaceAttenuationWithoutAknifesMAP, const unsigned long long llArraySize)	const
    {
    	DumpBSparams(bs);
    	DumpUArray(pTerrainHeightMAP, llArraySize);
    	DumpVArray(pClassMAP, llArraySize);
    	DumpWArray(pHgtMAP, llArraySize);
    	DumpXArray(pDnsMAP, llArraySize);
    	DumpYArray(pLossdB, llArraySize);
    	DumpZAraay(pAngle, llArraySize);
    	DumpQArray(pEarthSurfaceAttenuationWithoutAknifesMAP, llArraySize);
    }
    void DumpEXAMPLE::DumpBSParamaters(const BSparams &BSparams) const
    {
    
    }
    void DumpEXAMPLE::DumpUArray(const short * UArray, const unsigned long long llArraySize) const
    {
    	string sMyFileNameFullPath = "C:\\UArray.txt";
    	DumpToShortArray(UArray, llArraySize, sMyFileNameFullPath);
    }
    void DumpEXAMPLE::DumpVArray(const short * pVArray, const unsigned long long llArraySize) const
    {
    	string sMyFileNameFullPath = "C:\\VArray.txt";
    	DumpToShortArray(pVArray, llArraySize, sMyFileNameFullPath);
    }
    void DumpEXAMPLE::DumpWArray(const short * pWArray, const unsigned long long llArraySize)	 const
    {
    	string sMyFileNameFullPath = "C:\\WArray.txt";
    	DumpToShortArray(pWArray, llArraySize, sMyFileNameFullPath);
    }
    void DumpEXAMPLE::DumpXArray(const short *pXArray, const unsigned long long llArraySize)  const
    {
    	string sMyFileNameFullPath = "C:\\XArray.txt";
    	DumpToShortArray(pXArray, llArraySize, sMyFileNameFullPath);
    }
    void DumpEXAMPLE::DumpYArray(const float* pLoss, const unsigned long long llArraySize)  const
    {
    	string sMyFileNameFullPath = "C:\\YArray.txt";
    	DumpToFloatArray(pYArray, llArraySize, sMyFileNameFullPath);
    }
    void DumpEXAMPLE::DumpZAraay(const float *pZAraay, const unsigned long long llArraySize)  const
    {
    	string sMyFileNameFullPath = "C:\\ZAraay.txt";
    	DumpToFloatArray(pAngle, llArraySize, sMyFileNameFullPath);
    }
    void DumpEXAMPLE::DumpQArray(const float *pQArray, const unsigned long long llArraySize)  const
    {
    	string sMyFileNameFullPath = "C:\\QArray.txt";
    	DumpToFloatArray(pQArray, llArraySize, sMyFileNameFullPath);
    }
    
    template <class T> void DumpEXAMPLE::DumpBasicArray(const T *pArray, const unsigned long long llArraySize, const std::string sFileName) const
    {
      
    	std::ofstream myfile(sFileName, std::ios::app);
    	if (myfile.is_open())
    	{
    		for (unsigned long long i = 0; i < llArraySize; i++)
    		{
    			myfile <<  pArray[i];
    		}
    	}
    	myfile.close();
    }
    
    void DumpEXAMPLE::DumpToShortArray(const short * pArray, const unsigned long long llArraySize, const std::string sFileName) const
    {
    	std::ofstream myfile(sFileName, std::ios::app);
    	if (myfile.is_open())
    	{
    		for (unsigned long long i = 0; i < llArraySize; i++)
    		{
    			myfile <<  pArray[i];
    		}
    	}
    	myfile.close();
    }
    
    void DumpEXAMPLE::DumpToFloatArray(const float *pArray, const unsigned long long llArraySize, const std::string sFileName) const
    {
    	std::ofstream myfile(sFileName, std::ios::app);
    	if (myfile.is_open())
    	{
    		for (unsigned long long i = 0; i < llArraySize; i++)
    		{
    			myfile <<  pArray[i];
    		}
    	}
    	myfile.close();
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured