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
Solved

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

Posted on 2004-08-29
14
477 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

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

Suggested Solutions

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

828 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