[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Save in CSV file

Posted on 1997-12-09
9
Medium Priority
?
411 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

650 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