본문 바로가기
WORK/Sotfware

파일 저장 및 불러오기

by KANG Stroy 2013. 8. 6.
728x90
728x90

FILE* fp = fopen("test.csv","w+");


fprintf(fp,"이름,주소,나이,\n");


fprintf(fp,"홍길동,모름,100,\n");


fclose(fp);


CString FileName;

char Filter[] = "Text File(*.txt)";

CFileDialog pDlg(false, "textfile(*.txt)", "*.txt", OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, Filter, NULL);

if(pDlg.DoModal() == IDOK)

{

FileName = pDlg.GetFileName();

FILE* fp = fopen(FileName, "wt");

if(fp == NULL)

{

AfxMessageBox("Do not Create File");

return ;

}

fwrite(m_strView, m_strView.GetLength(), 1, fp);

fclose(fp);

}

delete pDlg;


CFile file;                                                                                                                                        

CString m_csv = _T("날  짜, 내  용, 금  액, 호  차, 비  고\n "),pathname2;                                                         

int csvlen = m_csv.GetLength();

CString m_listcsv;


for(int i = 0; i < 10; i++)

{

m_listcsv = m_csv;

// m_csv = m_csv + m_listcsv;

}


CString file_name = m_csv.Mid(csvlen, 4) + _T("년도 차고부품사용명세.csv");

// 저장하기 위해 저장 다이얼로그를띄운다.

CString szFilter = _T("Save Data File(*.csv)| *.csv;|All Files(*.*)|*.*||");

CFileDialog dlg(FALSE, _T("Save Data File"), file_name, OFN_OVERWRITEPROMPT, szFilter);

pathname2 += _T(".\\") + file_name;


if(file.Open(pathname2, CFile::modeCreate | CFile::modeWrite))

{

char Temp[1000];

// WideCharToMultiByte(CP_ACP, 0, m_listcsv, lstrlenW(m_listcsv), Temp, 1000, 0, 0);

file.Write(Temp , m_csv.GetLength());

file.Close();

CString str = file_name + _T(" 파일이 생성되었습니다.");

AfxMessageBox(str);

 }


CString data1="AAAAAA",data2="AAAA",data3="AA",Title_str,In_str;

FILE *fp = ::fopen(".\\myCsv.csv","w");

In_str = "\n";

Title_str ="Hour, Minute, Second, FlowRate, SensorRaw, SensorDiff, ValveDuty ";

if ( fp )

{

    { //this block probably should be in loop on each line you want to write.

file.SeekToEnd(); 

+fprintf(fp,/*the data foratting you writing*/"%s,%s,%s,%s,%s\n",Title_str,In_str,data1,data2,data3);

    }

    fclose(fp);

  }



int hour, min, sec, year,month,day;


char szFilter[] = "CSV File (*.csv)|*.csv|All files(*.*)|*.*||";

/*

CFileDialog dlg(FALSE, "cpp", "제목없음", OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,

"CSV File(*.csv)|*.csv|");

*/

CFileDialog dlg(TRUE, "cpp", "myCsv.csv", OFN_HIDEREADONLY,

"CSV File(*.csv)|*.csv|");


//if(IDOK == dlg.DoModal()) 

if(1)

{   

CTime tm;

tm = CTime::GetCurrentTime();

year = tm.GetYear();

month = tm.GetMonth();

day = tm.GetDay();

hour = tm.GetHour();

min  = tm.GetMinute();

sec  = tm.GetSecond();

CStdioFile file;

CFileException e;

CString In_str, Title_str, Data_str, Time_str,Data_A,Year_str;

CString data1="AAAAAA",data2="AAAA",data3="AA";

BYTE data4[10]={'A','B','C','D','E','F','G','H','I'};

In_str = "\n";

Year_str.Format("%04d/%02d/%02d",year,month,day);

Time_str.Format("%02d:%02d:%02d:",hour, min, sec);

Data_str.Format("%s,%s,%s,%s,%s,%s,%s,%s",P_Name, Year_str,Time_str, RE_ADDRESS, data2, data3,data4, In_str);

file.Open(dlg.GetPathName(), CFile::modeNoTruncate | CFile::modeCreate | CFile::modeReadWrite, &e);

file.SeekToEnd();

file.WriteString(Data_str);

file.Close();

/*

if(IDOK == dlg.DoModal()) 

m_path_string = dlg.GetPathName(); // 저장할 파일의 경로를 얻는다. 

    else 

m_path_string = ""; 

*/

}

/*

if(m_path_string.GetLength() > 0){ 

    int hour, min, sec; 

    CTime tm; 

    tm = CTime::GetCurrentTime(); 

    hour = tm.GetHour(); 

    min  = tm.GetMinute(); 

    sec  = tm.GetSecond(); 

    CStdioFile file; 

    CFileException e; 

    CString In_str, Title_str, Data_str, Time_str; 

    In_str = "\n"; 

    Time_str.Format("%02d,%02d,%02d", hour, min, sec); 

    Title_str ="Hour, Minute, Second, FlowRate, SensorRaw, SensorDiff, ValveDuty "; 

    Data_str.Format(" , %s, %s, %s, %s", In_str, Title_str, Data_str, Time_str);

    file.Open(dlg.GetPathName(), CFile::modeCreate | CFile::modeReadWrite, &e); 

    file.SeekToEnd(); 

    file.WriteString(Title_str+In_str); 

    file.SeekToEnd(); 

    file.WriteString(Time_str+Data_str+In_str); 

    file.Close(); 

*/




728x90

'WORK > Sotfware' 카테고리의 다른 글

cosmic license 받기  (0) 2014.03.24
#pragma란  (0) 2013.08.14
RGB 색상코드  (0) 2013.07.18
combo box 선택 실행  (0) 2013.07.08
VC++ 에서 cannot find the definition for this function  (0) 2013.07.06

댓글