Link to home
Start Free TrialLog in
Avatar of jakkku ma
jakkku ma

asked on

reading and writing a files in mfc

Reading and Writing CSV Files in MFC code

Files has to open in OpenOffice for the first Data. without closing the OpenOffice  ,In background  ,program s running and
datas has to saved in the CSV files. close the file and reopened the file .new data s has to updated.But only one data thats is also first data only in CSV file.(only in OpenOffice ,datas are not saved  while processing file should in open then  close the file  and reopened the same file,no datas updated )

code was working in CSV FIle Viewer and Notepad++ while opened while processing ,closed and reopened the same file, data are updated )
Plz help
Avatar of sarabande
sarabande
Flag of Luxembourg image

csv files are text files. in c++ (also in mfc) you can use std::ifstream constructor to open a file and std::getline to read line by line.  to write a csv you would use std::ofstream to create (or open and truncate) a text file. after that you write row by row by using std::ofstream::write.

in mfc you could use alternatively CStdioFile class and member functions ReadString and WriteString. it could be an issue that  CStdioFile reads and writes wide strings (wchar_t characters coded in UTF16) when the mfc project property for characterset is UNICODE. if that is not wanted you would need to set the characterset to multi-byte such that the strings are char strings (1-byte per character).

note, i don't know what you mean by openoffice and background and what those terms have to do with mfc programming. i don't think that you can use mfc within openoffice.

can you explain better and with samples (screenshots, csv table, existing mfc code, mfc project MDI, SDI, or dialog-based) what your task is?

Sara
Avatar of jakkku ma
jakkku ma

ASKER

Dialogbased Application.
i had used CFile for open a file(CFile::modeCreate|CFile::modeReadWrite,) and Write to enter the datas,it was not updating datas  in openOfficeCalc(application to open a document) while file is opened.
Dialogbased Application.
i had used CFile for open a file(CFile::modeCreate|CFile::modeReadWrite,) and Write to enter the datas from VC++coding , not updating datas  in openOfficeCalc(application to open a document)  and open a csv file in microosft excel also updated while file is opened. plz help

      HANDLE hFIND = FindFirstFile(output_filename.c_str(), &findData );

      if( hFIND == INVALID_HANDLE_VALUE )
      {
            
            if (!Open(output_filename.c_str(), CFile::modeCreate|CFile::modeReadWrite |CFile::shareDenyWrite))
            {
                  LOG_INFO << filename << " File unable to open" << ENDLOG;
                  return;
            }
      
            bHeader = true;
      }
      else
      {
            CFileStatus status,status2;
            status.m_attribute =(BYTE) 0x00;
            CFile::SetStatus(output_filename.c_str(),status);

            CFile::GetStatus(output_filename.c_str(),status2);
            if(status2.m_attribute != 0x00)
            {
                  LOG_INFO << output_filename.c_str() << " File unable to set normal status" << ENDLOG;
                  return;
            }
            if (Open(output_filename.c_str(), CFile::modeWrite |CFile::shareDenyWrite))
            {
                  LOG_INFO<< output_filename.c_str() << " File unable to open" << ENDLOG;
                  FindClose( hFIND );
                  return;
            }
            SeekToEnd();
      }
was not updating datas  in openOfficeCalc(application to open a document) while file is opened.

actually that is a problem which you can't solve. if openoffice (or any other program) has opened a file for write, another program cannot open for write again. it even could be that you can't open it for read if the openoffice has opened the file exclusively.

Sara
hi
When the microsoft Excel(tried to open CSV file ) is opened,In that file can able to write or not?
you can try to open it read-only, if that worked you could write a new file with your mfc app and use automation to open the new file with open office. you also could issue a command that starts openoffice and pass the new file as commandline argument, if the openoffice is already open, the new command should open a new window with the changed document.

another idea is if you recognize that openoffice has opened the file exclusively, that you use automation which forces the openoffice to close the file.

Sara
it doesn't make a difference whether it is excel or openoffice. but you could use automation if you find out which program was locking the file.

why is the file opened in excel or openoffice? does it happen intentionally? is it ok if excel or openoffice was terminated by the mfc program?

Sara
While putting Logs and see the datas ,datas are in logs if it is locking somewhere means,but only problem is while writing in the CSV file,the file is opened,so datas are not updated in the opened file even i closed and open again also.

when file is not opened  while processing ,after completion if i opened the CSv file all the dats are there.

i want to see after write the first data ,i opend the file and see
user may opened ,closed or reopened ,but new updated datas should be updated

give me example code
datas are not updated in the opened file even i closed and open again
that is because open office first saves the current contents and so overwrites the changes you have made.

try the following. open the csv with visual studio. then use your program and change the csv. if you return to visual studio it should tell you, that the file has changed and ask whether you want to reload.

Sara
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.