Click here to Skip to main content
15,900,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a student on a final project. I'm writing an apps that read from csv in win32 app. But I'm not using CStdioFile. I'm using ifstream. This is my code for reading

C++
time_t long_time;
time(&long_time);
tm* st = localtime(&long_time);

string line;
char get;
int i;

fstream opn;
opn.open(path);
for(i = 1; i <= jumlahdata; i++)
{

    getline(opn, line,'\n');
    std::istringstream iss(line);

    iss >> st->tm_mon >> get >> st->tm_mday >> get >> st->tm_year >>
        st->tm_hour >> get >> st->tm_min >> get >> tinggi[i];
    st->tm_sec=0;
    st->tm_mon--;
    st->tm_year-=1900;
    waktu[i] = mktime(st);
}
opn.close()

This script work well in console app. But when I used it on the win32 app this script went error.

C++
void CMainFrame::OnFileOpen40001()
{
    // TODO: Add your command handler code here
    CString str;
    //CRect rc;
    //string path;
        
    //CFile ifp;
    //fstream opn;
    CFileDialog FileDlg(true, _T("*.csv"), NULL,NULL, _T("Comma Separated Value|*.csv"));
    if (FileDlg.DoModal()==IDOK)
    {
        str=FileDlg.GetFolderPath();
        str+='\\'+FileDlg.GetFileName();
        //string path((LPCTSTR)str);
        // Convert a TCHAR string to a LPCSTR
        CT2CA pszConvertedAnsiString (str);
        // construct a std::string using the LPCSTR input
        //std::string path (pszConvertedAnsiString);
        string path (pszConvertedAnsiString);
        //opn.open(str);
        //ifp=fopen((str),"r");
        /*
        CFileException e;
        if(ifp.Open(str,CFile::modeRead))
            e.ReportError();
            */
        n = data::countdata(path);
        CString a;
        waktu = new int unsigned [n+1];
        tinggi = new double [n+1];
        data::readdata(path, waktu, tinggi, n);
        CClientDC dc(this);
        a.Format(_T(" Data tinggi = %d"),tinggi[1]);
        dc.TextOutW(250,250,str);
        dc.TextOutW(250,350,a);
        
    }
}

Please help me. I can't figure why the value won't get into the matrix called tinggi. But the value is right in the matrik called waktu.

[edit]
I write them.
They are declared in the separated header file. These are the code.
C++
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <sstream>
using namespace std;

class data
{
public :
	int static countdata(string);
	void static readdata(string ,int unsigned *, double *,int);
	void static savedata(string , int unsigned *,double *, int);
	void static selectdt(unsigned int *, double *, int);
};

int data::countdata(string path)
{
	cout << "\n Menghitung data.";
	int n;
	fstream opn;
	opn.open(path);
	string tri;
	n=0;
	while(!opn.eof())
	{
		getline(opn,tri ,'\n');
		n++;
	}
	opn.close();
	n--;
	cout << "\n Data telah berhasil dihitung.";
	return(n);
}

void data::readdata(string path, int unsigned *waktu,double *tinggi, int jumlahdata)
{
	time_t long_time;
	time(&long_time);
	tm* st = localtime(&long_time);

	string line;
	char get;
	int i;
	double a;

	fstream opn;
	opn.open(path);
	for(i = 1; i <= jumlahdata; i++)
	{
		
		getline(opn, line,'\n');
		std::istringstream iss(line);
		
		iss >> st->tm_mon >> get >> st->tm_mday >> get >> st->tm_year >>
			st->tm_hour >> get >> st->tm_min >> get >> a;
		st->tm_sec=0;
		st->tm_mon--;
		st->tm_year-=1900;
		waktu[i] = mktime(st);
		tinggi[i] = a;
	}
	opn.close();
}

void data::savedata(string path, int unsigned *input1, double *input2, int jumlahdata)
{
	time_t long_time;
	time(&long_time);
	tm* st = localtime(&long_time);
	ofstream simp;
	simp.open(path);
	//string str;
	for(int i=1;i<=(jumlahdata);i++)
		{
			//format tanggal 8/18/2013 10:42,2.216
			long_time = input1[i];
			localtime_s(st,&long_time);
			// str = printf("%c,%d",asctime(st),datasel[i]);
			//simp <<datasel[i]<<','<<asctime(st);	
			simp << st->tm_mon+1 <<"/"<<st->tm_mday<<"/"<<st->tm_year+1900<<" " << st->tm_hour
				<<":"<<st->tm_min<<","<<input2[i]<<endl;
	}
	simp.close();
}

void data::selectdt(unsigned int *tanggal, double *tinggi, int jumlahdata)
{
	int i,j,k,l;
	unsigned int *copytanggal;
	double *copytinggi;
	copytanggal = new int unsigned [jumlahdata+1];
	copytinggi = new double [jumlahdata+1];

	for(i=1; i<= jumlahdata; i++)
	{
		copytanggal[i] = tanggal[i];
		copytinggi[i] = tinggi[i];
	}
	delete tanggal,tinggi;
	int n=0;
	for( i = copytanggal[1]; i <= copytanggal[jumlahdata]; i+=3600)
		{
			n++;
		}
	cout << "\n Jumlah baris = " << n;
	tanggal = new int unsigned [n+1];
	tinggi = new double [n+1];
	int temp(1);
	l=1;
	
	for(i = copytanggal[1]; i <= copytanggal[jumlahdata]; i+=3600)
	{
	for(j = l; j <= jumlahdata; j++)
			{
				if(i==copytanggal[j])
				{
					tanggal[temp] = copytanggal[j];
					tinggi[temp] = copytinggi[j];
					temp++;
					l=j;
					break;
				}	
			}
	}
	jumlahdata=n;
	cout << "\n Jumlah data baru = " << jumlahdata;
	/*
	cout << "\n\n Waktu awal dan akhir = " << copytanggal[1] << " dan " << copytanggal[jumlahdata];
	cout << "\n Selisih = " << copytanggal[jumlahdata]-copytanggal[1];
	cout << "\n Jumlah data = " << (copytanggal[jumlahdata]-copytanggal[1])/3600;
	*/
	delete copytanggal, copytinggi;
}


I am define this outside the Doc generated class by MFC Wizard.
[/edit]
Posted
Updated 24-Nov-13 6:39am
v6
Comments
Richard MacCutchan 24-Nov-13 3:54am    
Unless you explain what the problem is, then we can't either.
Richard MacCutchan 24-Nov-13 3:58am    
What does data::countdata() do? Also, do not try to use TextOut functions in a general method like this, you should only write to your output window in the ON_PAINT handler.
Zulfa Syathari 24-Nov-13 4:09am    
I am using data::countdata to count the data line. So I can declare the matrix dynamically. I am using matrix called tinggi to store data with double type. Matriks tanggal get the value from the file. But matriks tinggi do not get the value. That is my problem.
Sorry about my grammar.
Richard MacCutchan 24-Nov-13 4:14am    
Where are the data::countdata and data::readdata functions defined? Are these functions that you have written, or are they part of some third party library?
Zulfa Syathari 24-Nov-13 4:28am    
Code moved to original post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900