Solved

Save in CSV file

Posted on 1997-12-09
9
362 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
  • 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 50 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now