Click here to Skip to main content
15,921,837 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: help with graph syntax Pin
Cedric Moonen3-May-10 20:30
Cedric Moonen3-May-10 20:30 
Questionatof conversion issue Pin
kasi143-May-10 12:14
kasi143-May-10 12:14 
AnswerRe: atof conversion issue Pin
Tim Craig3-May-10 12:26
Tim Craig3-May-10 12:26 
AnswerRe: atof conversion issue Pin
Stephen Hewitt3-May-10 14:00
Stephen Hewitt3-May-10 14:00 
Questionmaking ini file in Documents and Settings WritePrivateProfileString to Pin
malaugh3-May-10 12:01
malaugh3-May-10 12:01 
AnswerRe: making ini file in Documents and Settings WritePrivateProfileString to Pin
PJ Arends3-May-10 13:18
professionalPJ Arends3-May-10 13:18 
AnswerRe: making ini file in Documents and Settings WritePrivateProfileString to Pin
«_Superman_»3-May-10 13:18
professional«_Superman_»3-May-10 13:18 
AnswerRe: making ini file in Documents and Settings WritePrivateProfileString to [modified] Pin
wangningyu4-May-10 0:06
wangningyu4-May-10 0:06 
the window sdk's function to operation "*.ini", like function WritePrivateProfileString and more,it's very inconvenient.

so you can use this class in your program (if the section isn't exiting,it will create himself.)
// ***************************************************************
//  OPini.h: interface for the COPini class.
//  -------------------------------------------------------------
//  this class will read the "*.ini" file from the current path.
//  -------------------------------------------------------------
// ***************************************************************

#if !defined(AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_)
#define AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxwin.h>
class COPini 
{
public:
    static DWORD ReadString (char *section, char * key,  char stringtoread[],  char * filename);
    static BOOL  WriteString(LPCTSTR section, LPCTSTR key,char* stringtoadd, char *filename);
    COPini();
    virtual ~COPini();

};

#endif // !defined(AFX_OPINI_H__CE3F8B7B_1ACA_46CC_A91C_F8E23FA9B063__INCLUDED_)



// OPini.cpp: implementation of the COPini class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "OPini.h"

/********************************************************************
    filename:   // OPini.cpp
    file path:   
    file base:  // OPini
    file ext:   // cpp
    author:     // alantop
    purpose:    // 读取INI文件。
*********************************************************************/
//////////////////////////////////////////////////////////////////////
// Construction / Destruction
//////////////////////////////////////////////////////////////////////

COPini::COPini()
{
}
COPini::~COPini()
{
}
void error(LPSTR lpszFunction)
{
    CHAR szBuf[80];
    DWORD dw = GetLastError();
    sprintf(szBuf, "%s failed: GetLastError returned %u\n",
        lpszFunction, dw);
    MessageBox(NULL, szBuf, "Error", MB_OK);
    ExitProcess(dw);
}

/*****************************************************************************
Function:       //
Description:    // write the  config fto the "*.ini" files.
Calls:          //
Called By:      //
Table Accessed: //
Table Updated:  //
Input:          //
Output:         //
Return:         // If success it will return TRUE ,else return FALSE.
Others:         //
author:         //
******************************************************************************/
BOOL COPini::WriteString(LPCTSTR section, LPCTSTR key, char *stringtoadd, char *filename)
{
    CHAR FilePath[255];
    GetModuleFileName(NULL,FilePath,255);
    //Scan a string for the last occurrence of a character.
    (strrchr(FilePath,'\\'))[1] = 0;
    strcat(FilePath,filename);
    return ::WritePrivateProfileString(section,key,stringtoadd,FilePath);
}
/*****************************************************************************
Function:       //
Description:    // read config from the "*.ini" files.
Calls:          //
Called By:      //
Table Accessed: //
Table Updated:  //
Input:          //
Output:         //
Return:         // How many bytes of the the characters
Others:         //
author:         // 
******************************************************************************/
DWORD COPini::ReadString(char *section, char * key,  char stringtoread[],  char * filename)
{
    CHAR FilePath[255];
    GetModuleFileName(NULL,FilePath,255);
    (strrchr(FilePath,'\\'))[1] = 0;
    strcat(FilePath,filename);
    return ::GetPrivateProfileString(section, key,NULL,stringtoread,255,FilePath);
}



you can it like this:
#include <<OPini.h>>

int main()
{
 char *p1 = "Hello";
 char *p2 ;
 
 COPini::WriteString("Section1", "KeyName1", p1, "SetInfo.ini");
 COPini::ReadString("Section2" , "KeyName2", p2, "SetInfo.ini");
 .....
}


Good Luck !

modified on Tuesday, May 4, 2010 6:29 AM

QuestionRe: making ini file in Documents and Settings WritePrivateProfileString to Pin
David Crow4-May-10 3:08
David Crow4-May-10 3:08 
Questionbreak point not hit in release mode Pin
rayjoslyn3-May-10 10:57
rayjoslyn3-May-10 10:57 
AnswerRe: break point not hit in release mode Pin
«_Superman_»3-May-10 11:16
professional«_Superman_»3-May-10 11:16 
GeneralRe: break point not hit in release mode Pin
rayjoslyn3-May-10 11:20
rayjoslyn3-May-10 11:20 
GeneralRe: break point not hit in release mode Pin
rayjoslyn4-May-10 10:59
rayjoslyn4-May-10 10:59 
AnswerRe: break point not hit in release mode Pin
Stephen Hewitt3-May-10 14:01
Stephen Hewitt3-May-10 14:01 
AnswerRe: break point not hit in release mode Pin
loyal ginger3-May-10 14:21
loyal ginger3-May-10 14:21 
GeneralRe: break point not hit in release mode Pin
rayjoslyn4-May-10 4:28
rayjoslyn4-May-10 4:28 
GeneralRe: break point not hit in release mode Pin
rayjoslyn4-May-10 7:47
rayjoslyn4-May-10 7:47 
QuestionProgrammatically find the FTP root directory? Pin
Ed K3-May-10 9:08
Ed K3-May-10 9:08 
AnswerRe: Programmatically find the FTP root directory? Pin
Code-o-mat3-May-10 9:23
Code-o-mat3-May-10 9:23 
GeneralRe: Programmatically find the FTP root directory? Pin
Ed K3-May-10 9:42
Ed K3-May-10 9:42 
GeneralRe: Programmatically find the FTP root directory? Pin
Code-o-mat3-May-10 9:51
Code-o-mat3-May-10 9:51 
GeneralRe: Programmatically find the FTP root directory? Pin
Michel Godfroid3-May-10 10:09
Michel Godfroid3-May-10 10:09 
QuestionRe: Programmatically find the FTP root directory? Pin
David Crow4-May-10 3:11
David Crow4-May-10 3:11 
AnswerRe: Programmatically find the FTP root directory? Pin
Ed K4-May-10 4:08
Ed K4-May-10 4:08 
AnswerRe: Programmatically find the FTP root directory? Pin
Maximilien3-May-10 9:50
Maximilien3-May-10 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.