Solved

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

Posted on 2004-08-29
14
466 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 180
substring method in java 1 99
Doc'in system (example?) BA 7 89
changeXy challenge 13 69
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 …
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 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

26 Experts available now in Live!

Get 1:1 Help Now