Solved

Save in CSV file

Posted on 1997-12-09
9
373 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

809 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