Solved

Save in CSV file

Posted on 1997-12-09
9
382 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
Industry Leaders: 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 100
Dynamically allocate memory 9 75
No module found pypyodbc, 3 54
Bitwise and to sum elements 2 52
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

732 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