?
Solved

Save in CSV file

Posted on 1997-12-09
9
Medium Priority
?
396 Views
Last Modified: 2012-05-04
Dear
       I want to save the input which comes form interface in        a CSV file.But when I open CSV file with editor, It is not CSV type.
     And I want to append the information in the last line of file. How to modify?    
       The code is shown below
        Thank You for help
void CDeliverer::OnOkButton()
{
      // TODO: Add your control notification handler code here
            CFile f;                                                                   f.Open("C:\\TRY.CSV",
                                CFile::modeCreate|CFile::modeWrite);
      UpdateData(TRUE);
      CArchive ar(&f,CArchive::store);
      ar<<m_Var1;
      f.Write(m_Var1,m_Var1.GetLength());
      ar.Close();
      f.Close();

}
0
Comment
Question by:logout
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 2

Expert Comment

by:AlFa
ID: 1175597
You open your file with no truncate option an seek to end.

CFile f; f.Open("C:\\TRY.CSV",
 CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate   );
f.Seek( 0L, CFile::end );
0
 

Author Comment

by:logout
ID: 1175598
Dear
        I can insert a comma betw each input data. But I find some porblem. First of all, When I add 2 set data, I find there are some unknown extra repeat data in the file. (please see the result).
And I hope the 2 set data in different line , How to change the code. Thank You

p.s temp=","

void CParcel::OnOkButton()
{
      // TODO: Add your control notification handler code       CFile f;
                     f.Open("C:\\Parcel.CSV",CFile::modeCreate
               |CFile::modeWrite
               |CFile::modeNoTruncate);
      f.Seek( 0L, CFile::end );
      UpdateData(TRUE);
       CArchive               ar(&f,CArchive::store);                                  ar<<m_PID<<m_Weight;
      f.Write(m_PID,m_PID.GetLength());
      f.Write(temp,temp.GetLength());
      f.Write(m_Weight,m_Weight.GetLength());
      f.Write(temp,temp.GetLength());
      ar.Close();
      f.Close();

}

Result:
===================================
1357 , HK  ,  1357HK         AASA , NNN ,  AASANNN    


Expect result:
===================================  
1357 , HK  ,  
 AASA , NNN ,
 
0
 
LVL 2

Expert Comment

by:AlFa
ID: 1175599
Why do you use 2 write method ?
use Carchive or Cfile not both
p.s temp=","

void CParcel::OnOkButton()
{
// TODO: Add your control notification handler code CFile f;
f.Open("C:\\Parcel.CSV",CFile::modeCreate
 |CFile::modeWrite
 |CFile::modeNoTruncate);
f.Seek( 0L, CFile::end );
UpdateData(TRUE);
//CArchive ar(&f,CArchive::store); ar<<m_PID<<m_Weight;
f.Write(m_PID,m_PID.GetLength());
f.Write(temp,temp.GetLength());
f.Write(m_Weight,m_Weight.GetLength());
f.Write(temp,temp.GetLength());
//ar.Close();
f.Close();

}

or
void CParcel::OnOkButton()
{
// TODO: Add your control notification handler code CFile f;
f.Open("C:\\Parcel.CSV",CFile::modeCreate
 |CFile::modeWrite
 |CFile::modeNoTruncate);
f.Seek( 0L, CFile::end );
UpdateData(TRUE);
CArchive ar(&f,CArchive::store);
ar<<m_PID;
ar<<temp;
ar<<m_Weight;
ar<<temp;
ar.Close();
f.Close();
}

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:logout
ID: 1175600
Dear
        Thank for your help, If I want to get the value from the file,
       What is the code?

      f.Open("C:\\Parcel.CSV",CFile::modeRead)
       f.??(m_PID,10,  ','     );
10 means that read 10 char , comma is the separation betw the data
0
 
LVL 2

Expert Comment

by:AlFa
ID: 1175601
How to read?
 you have to read char by char up to ','

void ReadOne(CFile &f, CString &cStr)
{
    char c;
    cStr.Empty();
    do
    {
        if(f.Read(&c, 1)<1) return; // error?
        if(c==',') return;
        cStr += c;
    } while(1);
}

call this for each field

f.Open("C:\\Parcel.CSV",CFile::modeRead);
CString cStrTmp;
ReadOne(f, cStrTmp);
m_PID = atoi(cStrTmp);
etc ...

you could have a ft witch return CString or overloaded ft for each type you have to manage.


0
 

Author Comment

by:logout
ID: 1175602
Dear
I integrate the  following coding into my program. Is the wrong declaration

void CDeliverer::OnModifyButton()
{
      // TODO: Add your control notification handler code here
f.Open("C:\\Parcel.CSV",CFile::modeRead);
CString cStrTmp;
ReadOne(f, cStrTmp);
m_PID = atoi(cStrTmp);
f.Close();
}

void CDellverer::ReadOne(CFile &f, CString &cStr)
{
char c;
cStr.Empty();
do
{
if(f.Read(&c, 1)<1) return; // error?
if(c==',') return;
cStr += c;
} while(1);
}

In header filer
================
protected:

      // Generated message map functions
      //{{AFX_MSG(CDeliverer)
      afx_msg void OnExitButton();
      afx_msg void OnClearButton();
      afx_msg void OnOkButton();
      afx_msg void OnAddButton();
      afx_msg void OnModifyButton();
      afx_msg void ReadOne(CFile ,CString );  <- Wrong Declaration?

Error message:
error C2558: 'CFile' : no copy constructor available
error C2558: 'CFile' : no copy constructor available
error C2579: 'ReadOne(class CFile &,class CString &)' : parameter list not sufficiently different from 'ReadOne(class CFile,class CString)'

0
 
LVL 2

Accepted Solution

by:
AlFa earned 100 total points
ID: 1175603
afx_msg void ReadOne(CFile ,CString ); <- Wrong Declaration?
 you are right it's wrong.
declare it as
void ReadOne(CFile &, CString&); that's all.
Do not put instruction(declaration) in class wizard work
//{{AFX.....
 here
//}}AFX ...
b'cause next time you call it it will not reconize it's job an go to error.


0
 

Author Comment

by:logout
ID: 1175604
Dear
      Help again!
                How to disable the button (ADD button) before I enter all Fields and enable it when user enter all information in all fields. What is the coding?

0
 
LVL 2

Expert Comment

by:AlFa
ID: 1175605
Where is this add button?
To disable a button you've to get a variable on it with GetDlgItem
after you disable  it with EnableWindow.


CWnd * xx = GetDlgItem(#ID_ADD);
xx->EnableWindow(FALSE);

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question