Solved

multi lines in .ini file how to save and load/read ?

Posted on 2004-08-29
14
480 Views
Last Modified: 2013-11-20
I am working on this little clock program and I added notepad to it.
I wanted it to save notepad text to its one ini file only.

Dont tell me to save it to anothet txt file.  
I already know basics of saving and reading to .ini files in MFC pragramming.    

I want to do same thing as the script editor in mIRC.
the script editor in mIRC saves multi lines to .ini like this
n1=text34234
n2=i love you
n3=this is funny  


Here is my .ini  

[BOX1]
Title=test1 remeber me
Hour=10
Minutes=1
AM_PM=PM
[BOX2]
Title=test2
Hour=3
Minutes=4
AM_PM=PM
[BOX3]
Title=test3
Hour=5
Minutes=6
AM_PM=PM
[BOX4]
Title=test4
Hour=0
Minutes=0
AM_PM=AM
[BOX5]
Title=test5
Hour=0
Minutes=0
AM_PM=AM
[BOX6]
Title=test6
Hour=0
Minutes=0
AM_PM=AM
[NOTEPAD]


note=dfsdfasdjf
fksjflasd;f
df;alsdf;ladsf

MY COOL NOTE PAD !!!!!!



when you open it again after you close it

you only see dfsdfasdjf


I know I need to add

note1=
note2=
note3=
note4=


here is my screenshot

http://dasher.psychz.net/~area51/notepadclock.jpg


0
Comment
Question by:Alien3
[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
  • 7
  • 7
14 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926399
I am not sure I have understood your question, I think you want to save a multiline string in many .ini entries. Could be something like this:

CString text = GetDlgItemText(YOUR_CONTROL_ID);   // Some method to retrieve your text
CString line, label;
int count=0;

while (!text.IsEmpty()) {
    line = text.SpanExcluding("\n\r");
    if (!line.IsEmpty()) {
        label.Format("note%i", ++count);
        // Put here your write to .ini function (may WriteProfileString?) using label and line
    }
    text.Delete(0, line.GetLenght());
    line = text.SpanIncluding("\n\r");
    text.Delete(0, line.GetLenght());
}
0
 
LVL 1

Author Comment

by:Alien3
ID: 11926521
your code bring errors  

here is my function.  

void CMyDigClockDlg::OnSavenote()
{

char sIniFile[MAX_PATH];
GetModuleFileName( NULL, sIniFile, MAX_PATH );
strcpy( strrchr( sIniFile, '\\' ) + 1, "reminderclock.ini" );


WritePrivateProfileString("NOTEPAD", NULL, NULL, sIniFile);
CString KeyName;


CString text = GetDlgItemText(IDC_NOTEPAD);   // Some method to retrieve your text
CString line, label;
int count=0;

while (!text.IsEmpty()) {
    line = text.SpanExcluding("\n\r");
    if (!line.IsEmpty()) {
        label.Format("note%i", ++count);
        // Put here your write to .ini function (may WriteProfileString?) using label and line

WritePrivateProfileString("NOTEPAD", label, m_notepad, sIniFile);

    }
    text.Delete(0, line.GetLenght());
    line = text.SpanIncluding("\n\r");
    text.Delete(0, line.GetLenght());
}



/*

char notepad[MAX_PATH];
int loop;
CString temp_key;


///WritePrivateProfileString ("NOTEPAD", "note", m_notepad, sIniFile);
WritePrivateProfileStruct("NOTEPAD", "note", (LPVOID)&m_notepad, sizeof(m_notepad), sIniFile);

      for (loop = 0; loop < m_notepad.Get; ++loop)
      {
            temp_key.Format ("note%2.2d", loop);




            WritePrivateProfileStruct ("NOTEPAD", temp_key, (void *)&m_notepad, sizeof(notes), sIniFile);
            
      }
*/
      
}


ERRORS:


Compiling...
MyDigClockDlg.cpp
C:\-= My Stuff =-\-= programs =-\-= MY CLOCKS =-\MyDigClockTest\MyDigClockDlg.cpp(854) : error C2661: 'GetDlgItemTextA' : no overloaded function takes 1 parameters
C:\-= My Stuff =-\-= programs =-\-= MY CLOCKS =-\MyDigClockTest\MyDigClockDlg.cpp(867) : error C2039: 'GetLenght' : is not a member of 'CString'
        c:\-= my software =-\microsoft visual studio\vc98\mfc\include\afx.h(368) : see declaration of 'CString'
C:\-= My Stuff =-\-= programs =-\-= MY CLOCKS =-\MyDigClockTest\MyDigClockDlg.cpp(869) : error C2039: 'GetLenght' : is not a member of 'CString'
        c:\-= my software =-\microsoft visual studio\vc98\mfc\include\afx.h(368) : see declaration of 'CString'
Error executing cl.exe.

MyDigClock.exe - 3 error(s), 0 warning(s)






0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926540
It's a typo replace GetLenght with GetLength
0
Technology Partners: 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!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926617
GetDlgItemText line must be:

CString text;
GetDlgItemText(IDC_NOTEPAD, text);

0
 
LVL 1

Author Comment

by:Alien3
ID: 11926627
it does work but

I get this in .ini

[NOTEPAD]
note1=fgdfgsdfg
note2=fgdfgsdfg
note3=fgdfgsdfg
note4=fgdfgsdfg
dfsgsdfgsfdg
333333333

eeeeeeeeeeee
dfsgsdfgsfdg
333333333

eeeeeeeeeeee
dfsgsdfgsfdg
333333333

eeeeeeeeeeee
dfsgsdfgsfdg
333333333

eeeeeeeeeeee




not right !!!!


0
 
LVL 1

Author Comment

by:Alien3
ID: 11926657
anyway  I fixed this by replacing m_notepad with line.  

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926676
Yes, there was the bug
0
 
LVL 1

Author Comment

by:Alien3
ID: 11926681
ok I have another question for you.

can you help with how can i read it ?  

      char sIniFile[MAX_PATH];
      GetModuleFileName( NULL, sIniFile, MAX_PATH );
      strcpy( strrchr( sIniFile, '\\' ) + 1, "reminderclock.ini" );

        char   getnote[MAX_PATH];

        GetPrivateProfileString("NOTEPAD", "note", "", getnote, sizeof(getnote), sIniFile);

        m_notepad = _T(getnote);

this is old code.  

this is in OnInitDialog()  


it need to read from [NOTEPAD] section with
note1=
note2=  

it should not include notexx= into textbox.    


0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926698
CString label;
CString text;
char   getnote[MAX_PATH];

for (int i=1; ;i++) {
    label.Format("note%i",i);
    GetPrivateProfileString("NOTEPAD", label, "", getnote, sizeof(getnote), sIniFile);
    if (*getnote) {
         text += getnote;
         text += "\n\r";
    } else
         break;
}
0
 
LVL 1

Author Comment

by:Alien3
ID: 11926725
the code compiles fine but

I dont get anything in the textbox :/  


0
 
LVL 1

Author Comment

by:Alien3
ID: 11926742
nevermind

here is my code


CString label;
CString text;
char   getnote[MAX_PATH];

for (int i=1; ;i++) {
    label.Format("note%i",i);
    GetPrivateProfileString("NOTEPAD", label, "", getnote, sizeof(getnote), sIniFile);
    if (*getnote) {
         text += getnote;
         text += "\n\r";
      m_notepad = _T(text);
    } else

      break;
}




I get some blocks between the returns now.  

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 11926750
The problem is here, try with:
         text += "\r\n";
0
 
LVL 1

Author Comment

by:Alien3
ID: 11926783
the code helps :)  

Thanks very much  
elite MFC programmer :D  



0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926804
Thanks to you too, see you in next question.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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