Solved

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

Posted on 2004-08-29
14
462 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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Different colored text in ComboBox without Subclassing 8 44
substring method in java 1 79
noX challenge 17 76
wordappend challenge 8 84
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now