CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2017
    Posts
    3

    WriteFile (Date,TimeSeries) Export dll cpp ( .csv.txt)

    extern"C"__declspec(dllexport)
    WriteFile cpp
    Can change the separator ( , (,) , (-) v.s
    Pa.dat
    C:\NSDT_out
    C:\NSDT_out\out.csv
    Date;TimeSeries

    How to read and import csv file with same method (ReadFile cpp build ???)


    Date;TimeSeries;
    4/10/2017 10:15:00 AM;1.05807995796204;
    4/10/2017 10:30:00 AM;1.05810713768005;
    4/10/2017 10:45:00 AM;1.0581442117691;
    4/10/2017 11:00:00 AM;1.05816948413849;
    4/10/2017 11:15:00 AM;1.0581511259079;
    4/10/2017 11:30:00 AM;1.05814778804779;
    4/10/2017 11:45:00 AM;1.05817842483521;
    4/10/2017 12:00:00 PM;1.0582081079483;
    4/10/2017 12:15:00 PM;1.05821871757507;
    4/10/2017 12:30:00 PM;1.05818438529968;
    Attached Files Attached Files

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: WriteFile (Date,TimeSeries) Export dll cpp ( .csv.txt)

    [Thread moved as c++/cli]

    Instead of attaching a file, it is better if the code is inserted into the thread with code tags.

    Code:
    #include "stdafx.h"
    #include <math.h>
    #include <vector>
    #using <System.dll>
    #using <System.Xml.dll>
    
    #define M1 11.574
    
    using namespace std;
    using namespace System;
    using namespace System::IO;
    using namespace System::Text;
    using namespace System::Diagnostics;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Collections::Generic;
    using namespace System::Globalization;
    
    DateTime getDate(double);
    
    void AddText( FileStream^ fs, String^ value )
    {
       array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( value );
       fs->Write( info, 0, info->Length  );
    }
    
    extern"C"__declspec(dllexport) void _cdecl DataToFile(double *Date, double *ts1, double *out, long int size)
    {long int i;
    	
    	String^ sep = ",";
    	String^ pathdat = "C:\\NeuroShell Trader 5\\Servers\\PA.dat";
    	array<String^>^ sdata = File::ReadAllLines(pathdat);
    	String^ outputdir = sdata[0];
    	String^ outputfile = sdata[1];
    	String^ zagolovok = sdata[2];
    	if (!Directory::Exists( outputdir)){
    		Directory::CreateDirectory(outputdir);
    	} 
    	if ( File::Exists( outputfile ) ){
    		File::Delete( outputfile );
    	}
    	FileStream^ fs = File::Create( outputfile );
    	try
    	{
    		AddText( fs, zagolovok );
    		AddText( fs, "\r\n" );
    		for (i =0; i < size; i++ )
    		{
    			DateTime dateTimeValue;
    			dateTimeValue = getDate(Date[i]); // ïîëó÷Ã*åì äÃ*òó èç ÷èñëÃ*
    			String^ strDT=dateTimeValue.ToString(); // äÃ*òó êîÃ*âåðòèì â ñòðîêó
    			StringBuilder^ sb = gcnew StringBuilder; // ñòðîèì ñòðîêó
    			sb = sb->Append(strDT)->Append( sep );  // äÃ*òÃ*
    			sb = sb->Append(ts1[i])->Append( sep ); // òÃ*éìñåðèÿ 1
    			sb = sb->Append("\r\n");
    			String^ str = sb->ToString();
    			AddText( fs, str ); // ïèøåì ñòðîêó â ôÃ*éë
    			out[i]=0;
    		}
    	}
    	finally
    	{
    		if ( fs )
    			delete (IDisposable^)fs;
    	}
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    double getYearsInDays(double days, int *ost, int *vis)
    {
      int Year=1900;
      double ddays=0;
      int dci;
    
      if(days<0) {Year--; dci=-1;}
      else dci=1;
    
      while(-1)
      {
         *vis=0;
         if (((Year%4) == 0) && ((Year %100)!=0) || ((Year % 400)==0))
         {
           ddays+=dci;
           *vis=1;
         }
         ddays+=(365*dci);
         if (dci>=0){if (ddays<days) Year+=dci;else break;}
         else{if (ddays>days) Year+=dci;else break;}
    
      }
      if (days<0) *ost=(int)(days+*vis - ddays);
      else *ost=(int)(days+*vis+365 - ddays);
    
      return Year;
    }
    
    int getMonthsInDays(int days, int *ost, int vis)
    {
      int Month=1;
      int iDays=0;
      int iDaysMonth;
      while(-1)
      {
         switch(Month)
         {
           case 4: case 6: case 9: case 11:
         iDaysMonth=30;
         break;
           case 2:
         iDaysMonth=28;
         if (vis==1) iDaysMonth++;
         break;
           default:
         iDaysMonth=31;
         }
         if ((iDays+iDaysMonth)>=days) break;
         iDays+=iDaysMonth;
         Month++;
      }
      *ost = days-iDays;
      return Month;
    }
    
    int getOnlyDate(double date, int part)
    {
    	int days,
    		months,
    		years;
    	int ostat,vis=1;
    
    	if (date>0) years=(int)(getYearsInDays(date-1,&ostat,&vis));
    	else years=(int)(getYearsInDays(date-1,&ostat,&vis));
    	months=(int)(getMonthsInDays(ostat,&ostat,vis));
    	days=(int)ostat;
    
    	if(part==1) return years;
    	if(part==2) return months;
    	if(part==3) return days;
    }
    
    int getOnlyTime(double time, int part)
    {
    	int hours=4,
    		minets=35,
    		seconds=17;
    	double ostat;
    	double del1,del2,del3;
    
    	time*=1000000;
    	del1=(double)60*60*M1;
    	del2=(double)60*M1;
    	del3=(double)M1;
    
    	hours=(int)(time/del1);
    	ostat=fmod(time,del1);
    	minets=(int)(ostat/del2);
    	ostat=fmod(ostat,del2);
    	seconds=(int)(ostat/del3);
    
    	if(part==1) return hours;
    	if(part==2) return minets;
    	if(part==3) return seconds;
    }
    DateTime getDate(double ddate)
    {
    	double getYearsInDays(double days, int *ost, int *vis);
    	int getMonthsInDays(int days, int *ost,int vis);
    	int getOnlyDate(double, int);
    	int getOnlyTime(double, int);
    	int days,months,years,hours,minets,seconds;
    	double fraction, integer;
    
    	fraction = modf(ddate, &integer);
    
    	years=getOnlyDate(integer, 1);
    	months=getOnlyDate(integer, 2);
    	days=getOnlyDate(integer, 3);
    	hours=getOnlyTime(fraction, 1);
    	minets=getOnlyTime(fraction, 2);
    	seconds=getOnlyTime(fraction, 3);
    	System::DateTime dateTime =
    		System::DateTime( years,      // Year
    		months,        // Month
    		days,        // Day
    		hours,        // Hour
    		minets,        // Minute
    		seconds         // Second
    		);
    
    	return dateTime;
    
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: WriteFile (Date,TimeSeries) Export dll cpp ( .csv.txt)

    You can change the separator quite easily: It is defined as a local String variable right at the top of DataToFile().

    You can read in the data file to a String array using File::ReadAllLines(), dissect the lines read from the file into the individual fields using String::Split() and use the Parse() methods of DateTime and double to convert the string representation of the fields to the respective types.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Join Date
    Apr 2017
    Posts
    3

    Re: WriteFile (Date,TimeSeries) Export dll cpp ( .csv.txt)

    I am nuts about c ++
    A code written for export as a csv from a program
    How to read code from csv file (Date; TimeSeries) and transfer code to a program???
    ''There may also be a second csv file in the same format.Reading and writing csv'''

    picture upload
    Last edited by mimtek; April 30th, 2017 at 07:38 AM.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: WriteFile (Date,TimeSeries) Export dll cpp ( .csv.txt)

    I just pointed you to the relevant pieces of the .NET framework, including links to their documentation on MSDN, in my previous post. These also contain code samples demonstrating their use. There are only code samples in C# and VB .NET, but for one who is able to read C++/CLI code, the C# samples shouldn't be too hard to understand. (Ok, links to docs on the Parse() methods were missing. Here they are: DateTime::Parse(), double::Parse()) You just have to put these pieces together.

    Sorry, I'm not going to write this simple piece of code for you. Give it a try yourself, and if you'll encounter difficulties, you're welcome to post a (more specific) question here.

    As to reading another CSV file: what's the problem with this? Once you have the code to read the first file, you can use the same code for the other file as well; just change the file name. (Or better, of course, make a method out of your reading code and pass the file name as a parameter.)
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Apr 2017
    Posts
    3

    Re: WriteFile (Date,TimeSeries) Export dll cpp ( .csv.txt)

    There is a test code like you told me What is the reason for giving an error ??

    Code:
    #include "stdafx.h"
    #include <math.h>
    #include <vector>
    #using <System.dll>
    #using <System.Xml.dll>
    
    #define M1 11.574
    
    using namespace std;
    using namespace System;
    using namespace System::IO;
    using namespace System::Text;
    using namespace System::Diagnostics;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Collections::Generic;
    using namespace System::Globalization;
    
    DateTime getDate(double);
    
    void AddText( FileStream^ fs, String^ value )
    {
       array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( value );
       fs->Write( info, 0, info->Length  );
    }
    
    extern"C"__declspec(dllexport) void _cdecl PAExch(double *Date, double *TimeSeries, double *out, int rf, long int size)
    {long int i;
    
    ArrayList^ date_v = gcnew ArrayList;
    
    String^ sep = ";";
    String^ pathdat = "C:\\NeuroShell Trader 5\\Servers\\PAT.dat";
    array<String^>^ sdata = File::ReadAllLines(pathdat);
    String^ output = sdata[0];
    String^ intput = sdata[1];
    
    // ÓäÃ*ëÿåì ôÃ*éë out åñëè îÃ* ñóùåñòâóåò.
    if ( File::Exists( output ) )
    {
    	File::Delete( output );
    }
    //ÑîçäÃ*åì è çÃ*ïèñûâÃ*åì ôÃ*éë ôÃ*éë.
    {
    	FileStream^ fs = File::Create( output );
    	try
    	{
    		for (i =0; i < size; i++ )
    		{
    			DateTime dateTimeValue;
    			dateTimeValue = getDate(Date[i]); // ïîëó÷Ã*åì äÃ*òó èç ÷èñëÃ*
    			String^ strDT=dateTimeValue.ToString(); // äÃ*òó êîÃ*âåðòèì â ñòðîêó
    			StringBuilder^ sb = gcnew StringBuilder; // ñòðîèì ñòðîêó
    			sb = sb->Append(strDT)->Append( sep );  // äÃ*òÃ*
    			sb = sb->Append(TimeSeries[i])->Append( sep ); // òÃ*éìñåðèÿ
    			sb = sb->Append("\r\n");
    			String^ str = sb->ToString();
    			AddText( fs, str ); // ïèøåì ñòðîêó â ôÃ*éë
    		}
    	}
    	finally
    	{
    		if ( fs )
    			delete (IDisposable^)fs;
    	}
    }
    if(rf==1)
    {
    	if ( File::Exists( intput ) )
    	{
    		array<String^>^ sr = File::ReadAllLines(intput);
    		for (int count=0; count<sr->Length; count++)
    		{
    			array<Char>^chars = {';'};
    			array<String^>^split = sr[count]->Split( chars );
    			DateTime  cDateTime = DateTime::Parse( split[0] );
    			date_v->Add(cDateTime);
    		}
    		for (i=0;i<size;i++)
    		{
    			DateTime  iDateTime = getDate(Date[i]);
    			if(date_v->Contains(iDateTime))
    			{
    				int dIndex = date_v->IndexOf( iDateTime );
    				DateTime  nDateTime = (DateTime)date_v[dIndex];
    				if(DateTime::Compare( iDateTime, nDateTime ) == 0 )
    				{
    					array<Char>^chars = {';'};
    					array<String^>^split = sr[dIndex]->Split( chars );
    					double outts=0.0;
    					outts=Double::Parse( split[1] );
    					out[i]=(double)outts;
    				}
    				else{out[i]=3.4e38;}
    			}
    			else
    			{
    				out[i]=3.4e38;
    			}
    		}
    	}
    }
    }
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    double getYearsInDays(double days, int *ost, int *vis)
    {
      int Year=1900;
      double ddays=0;
      int dci;
    
      if(days<0) {Year--; dci=-1;}
      else dci=1;
    
      while(-1)
      {
         *vis=0;
         if (((Year%4) == 0) && ((Year %100)!=0) || ((Year % 400)==0))
         {
           ddays+=dci;
           *vis=1;
         }
         ddays+=(365*dci);
         if (dci>=0){if (ddays<days) Year+=dci;else break;}
         else{if (ddays>days) Year+=dci;else break;}
    
      }
      if (days<0) *ost=(int)(days+*vis - ddays);
      else *ost=(int)(days+*vis+365 - ddays);
    
      return Year;
    }
    
    int getMonthsInDays(int days, int *ost, int vis)
    {
      int Month=1;
      int iDays=0;
      int iDaysMonth;
      while(-1)
      {
         switch(Month)
         {
           case 4: case 6: case 9: case 11:
         iDaysMonth=30;
         break;
           case 2:
         iDaysMonth=28;
         if (vis==1) iDaysMonth++;
         break;
           default:
         iDaysMonth=31;
         }
         if ((iDays+iDaysMonth)>=days) break;
         iDays+=iDaysMonth;
         Month++;
      }
      *ost = days-iDays;
      return Month;
    }
    
    int getOnlyDate(double date, int part)
    {
    	int days,
    		months,
    		years;
    	int ostat,vis=1;
    
    	if (date>0) years=(int)(getYearsInDays(date-1,&ostat,&vis));
    	else years=(int)(getYearsInDays(date-1,&ostat,&vis));
    	months=(int)(getMonthsInDays(ostat,&ostat,vis));
    	days=(int)ostat;
    
    	if(part==1) return years;
    	if(part==2) return months;
    	if(part==3) return days;
    }
    
    int getOnlyTime(double time, int part)
    {
    	int hours=4,
    		minets=35,
    		seconds=17;
    	double ostat;
    	double del1,del2,del3;
    
    	time*=1000000;
    	del1=(double)60*60*M1;
    	del2=(double)60*M1;
    	del3=(double)M1;
    
    	hours=(int)(time/del1);
    	ostat=fmod(time,del1);
    	minets=(int)(ostat/del2);
    	ostat=fmod(ostat,del2);
    	seconds=(int)(ostat/del3);
    
    	if(part==1) return hours;
    	if(part==2) return minets;
    	if(part==3) return seconds;
    }
    DateTime getDate(double ddate)
    {
    	double getYearsInDays(double days, int *ost, int *vis);
    	int getMonthsInDays(int days, int *ost,int vis);
    	int getOnlyDate(double, int);
    	int getOnlyTime(double, int);
    	int days,months,years,hours,minets,seconds;
    	double fraction, integer;
    
    	fraction = modf(ddate, &integer);
    
    	years=getOnlyDate(integer, 1);
    	months=getOnlyDate(integer, 2);
    	days=getOnlyDate(integer, 3);
    	hours=getOnlyTime(fraction, 1);
    	minets=getOnlyTime(fraction, 2);
    	seconds=getOnlyTime(fraction, 3);
    	System::DateTime dateTime =
    		System::DateTime( years,      // Year
    		months,        // Month
    		days,        // Day
    		hours,        // Hour
    		minets,        // Minute
    		seconds         // Second
    		);
    
    	return dateTime;
    
    }
    The cause of the error may be ??


    image hosting
    Last edited by mimtek; April 30th, 2017 at 02:09 PM.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: WriteFile (Date,TimeSeries) Export dll cpp ( .csv.txt)

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: WriteFile (Date,TimeSeries) Export dll cpp ( .csv.txt)

    Unfortunately, the error code in your screenshot is unreadable due to compression loss. It may or may not be a useful hint for discovering the reason of the fault. However, the error message refers to automation, and since your code doesn't contain anything related to automation, the error code may be unrelated too.

    In order to compile your code as part of a C++ CLR console application project under VS2015, I first needed to comment out the line using namespace std;. Otherwise the compiler mistook your managed array as a reference to the C++ standard library array template. Besides this, I could leave your code unmodified; it doesn't use anything from that namespace anyway.

    Using the following driver code, the program produced the expected output file content:

    Code:
    // Main.cpp
    
    #include "stdafx.h"
    
    #pragma unmanaged
    
    #include <iostream>
    
    extern "C" void _cdecl PAExch(double *Date, double *TimeSeries, double *out, int rf, long int size);
    
    void NativeMain()
    {
      double adDates[10];
      double dCurrentDate = 42835.427083333333333;  // 4/10/2017 10:15:00 AM
      for (int i = 0; i < 10; ++i)
      {
        adDates[i] = dCurrentDate;
        dCurrentDate += 0.01041666666666666666666666666667;  // 15 minutes
      }
      double adValues[] =
      {
        1.05807995796204,
        1.05810713768005,
        1.0581442117691,
        1.05816948413849,
        1.0581511259079,
        1.05814778804779,
        1.05817842483521,
        1.0582081079483,
        1.05821871757507,
        1.05818438529968
      };
      double adOut[10];
      PAExch(adDates, adValues, adOut, 1, 10);
      for (int i = 0; i < 10; ++i)
        std::cout << adOut[i] << std::endl;
    }
    
    #pragma managed
    
    int main(array<System::String ^> ^args)
    {
      NativeMain();
      return 0;
    }
    Well, at least it was what I was expecting:

    Code:
    10.04.2017 10:15:00;1,05807995796204;
    10.04.2017 10:30:00;1,05810713768005;
    10.04.2017 10:45:00;1,0581442117691;
    10.04.2017 11:00:00;1,05816948413849;
    10.04.2017 11:15:00;1,0581511259079;
    10.04.2017 11:30:00;1,05814778804779;
    10.04.2017 11:45:00;1,05817842483521;
    10.04.2017 12:00:00;1,0582081079483;
    10.04.2017 12:15:00;1,05821871757507;
    10.04.2017 12:30:00;1,05818438529968;
    It is lacking the header line as shown in your initial post, which is no surprise, since there's no code at all provided to create it. Also the DateTime values are stored in the localized format. This should be unproblematic as long as it's always just your program reading back in the data, as DateTime::Parse() by default accepts the localized format as well. It may irritate other programs trying to read the file, though.

    This is the console output produced by the program:

    Code:
    1.05808
    1.05811
    1.05814
    1.05817
    1.05815
    1.05815
    1.05818
    1.05821
    1.05822
    1.05818
    Don't know what was to be expected here.

    For all that to work, I had to provide a pre-existing PAT.dat file at the expected location, containing the eventual input and output file names. I used the same name twice, so the program reads back in the data that it just wrote to disk. Without that file the program fails with an uncaught FileNotFoundException.

    Finally, first checking for existence of the output file and deleting it if it exists is unnecessary: File::Create() overwrites any pre-existing file anyway. Better, perhaps, check for existence of the intended output directory and create it if missing. Otherwise a missing output directory will lead to failure with an uncaught exception as well.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

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