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
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.
void CMainFrame::OnFileOpen40001()
{
CString str;
CFileDialog FileDlg(true, _T("*.csv"), NULL,NULL, _T("Comma Separated Value|*.csv"));
if (FileDlg.DoModal()==IDOK)
{
str=FileDlg.GetFolderPath();
str+='\\'+FileDlg.GetFileName();
CT2CA pszConvertedAnsiString (str);
string path (pszConvertedAnsiString);
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.
#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);
for(int i=1;i<=(jumlahdata);i++)
{
long_time = input1[i];
localtime_s(st,&long_time);
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;
delete copytanggal, copytinggi;
}
I am define this outside the Doc generated class by MFC Wizard.
[/edit]