贴出代码啊。保证给分。

解决方案 »

  1.   

    读:GetPrivateProfileString()
    写:WritePrivateProfileString()
    如:读d:\data\com.ini里的
    [SMSC]
    smsc=1388888888nCount=GetPrivateProfileString(_T("SMSC"),_T("smsc"),_T("138777777"),buf,50,"d://data//com.ini");同理
    WritePrivateProfileString为写入。
    看MSDN
      

  2.   

    bool WriteLogFile(LPCTSTR Section,LPCTSTR Value)
    {
    HANDLE handle;
    char szFileName[1024];
    char WindowsDiretory[MAX_PATH];
    GetWindowsDirectory(WindowsDiretory,MAX_PATH);
    sprintf(szFileName,"%s\\Log.ini",WindowsDiretory);
    handle = CreateFile(szFileName,GENERIC_ALL,FILE_SHARE_READ,NULL,OPEN_ALWAYS|CREATE_NEW,FILE_ATTRIBUTE_HIDDEN,NULL);
    WritePrivateProfileString(Section,Value,"1",szFileName);
    CloseHandle(handle);
    return true;
    }
    bool WritePrivateFile(LPCTSTR szInIFile,LPINFORMATION inf)
    {
    TCHAR section[] = "SYSTEM REQUIREMENTS";
    TCHAR key1[] = "HARD DISK SPACE";
    TCHAR key2[] = "CdNo";
    TCHAR key3[] = "FileCount";
    GetPrivateProfileString(section,"VERSION",NULL,inf->Version,strlen(inf->Version),szInIFile);
    inf->RequireSpace = GetPrivateProfileInt(section,key1,NULL,szInIFile); 
    inf->CdNo = GetPrivateProfileInt(section,key2,NULL,szInIFile);
    inf->FileCount = GetPrivateProfileInt(section,key3,NULL,szInIFile);
    return true;
    }
      

  3.   

    哦。了解了一些,我没有MSDN啊,惨
    谁有下载地址啊
      

  4.   

    我改写的一个读INI文件的类,供参考。CIniFile.h///////////////////////////////////////////////////////////////////////////////
    //
    // IniFile.h: interface for the CIniFile class.
    // Written by: Adam Clauss
    // Email: [email protected]
    // You may use this class/code as you wish in your programs.  Feel free to distribute it, and
    // email suggested changes to me.
    //
    // ============================================================================
    //
    // Modify by zming, 2003.04.21
    //
    // Now, the class CIniFile implement whit C style & STL
    //
    ///////////////////////////////////////////////////////////////////////////////
    #ifndef _CINIFILE_H
    #define _CINIFILE_H
    #ifdef WIN32
    #pragma warning(disable : 4786)
    #pragma warning(disable : 4251)
    #endif
    #include <string>
    #include <vector>
    using namespace std;//all keys are of this time
    class CKey
    {
    public: //list of values in key
    vector<string> values; //corresponding list of value names
    vector<string> names;};class CIniFile  
    {
    //all private variables
    private: //stores pathname of ini file to read/write
    string path;

    //list of keys in ini
    vector<CKey> keys;  //corresponding list of keynames
    vector<string> names; 


    //all private functions
    private:
    //overloaded to take string
    istream & getline( istream & is, string & str ); //returns index of specified value, in the specified key, or -1 if not found
    int FindValue(int keynum, const char * valuename);
    //public variables
    public: //will contain error info if one occurs
    //ended up not using much, just in ReadFile and GetValue
    string error;
    //public functions
    public:
    //default constructor
    CIniFile(); //constructor, can specify pathname here instead of using SetPath later
    CIniFile(const char * inipath); //default destructor
    virtual ~CIniFile(); //returns index of specified key, or -1 if not found
    int FindKey(const char * keyname); //sets path of ini file to read and write from
    void SetPath(const char * newpath); //reads ini file specified using CIniFile::SetPath()
    //returns true if successful, false otherwise
    bool ReadFile(); //writes data stored in class to ini file
    bool WriteFile();  //deletes all stored ini data
    void Reset(); //returns number of keys currently in the ini
    int GetNumKeys(); //returns number of values stored for specified key
    int GetNumValues(const char * keyname); //gets value of [keyname] valuename = 
    //overloaded to return string, int, and double,
    //returns "", or 0 if key/value not found.  Sets error member to show problem
    const char * GetValue(const char * keyname, const char * valuename); 
    int GetValueI(const char * keyname, const char * valuename); 
    double GetValueF(const char * keyname, const char * valuename); //sets value of [keyname] valuename =.
    //specify the optional paramter as false (0) if you do not want it to create
    //the key if it doesn't exist. Returns true if data entered, false otherwise
    //overloaded to accept string, int, and double
    bool SetValue(const char * key, const char * valuename, const char * value, bool create = 1);
    bool SetValueI(const char * key, const char * valuename, int value, bool create = 1);
    bool SetValueF(const char * key, const char * valuename, double value, bool create = 1); //deletes specified value
    //returns true if value existed and deleted, false otherwise
    bool DeleteValue(const char * keyname, const char * valuename); //deletes specified key and all values contained within
    //returns true if key existed and deleted, false otherwise
    bool DeleteKey(const char * keyname);
    };#endif // _CINIFILE_H
      

  5.   


    CIniFile.cpp
    ///////////////////////////////////////////////////////////////////////////////
    //
    // IniFile.cpp: implementation of the CIniFile class.
    // Written by: Adam Clauss
    //
    //
    // Modify by zming, 2003.04.21
    //
    ///////////////////////////////////////////////////////////////////////////////#include "CIniFile.h"
    #include <fstream>
    const int MaxLineLength = 80;const int MaxIntegerLength = 32;
    const int MaxDoubleLength  = 32;/////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    ///////////////////////////////////////////////////////////////////////
    // default constructor
    //
    CIniFile::CIniFile()
    {
    }//
    // constructor, can specify pathname here instead of using SetPath later
    //
    CIniFile::CIniFile(const char * inipath)
    {
    path.append( inipath );
    }//
    // default destructor
    //
    CIniFile::~CIniFile()
    {}/////////////////////////////////////////////////////////////////////
    // Public Functions
    /////////////////////////////////////////////////////////////////////
    //
    // sets path of ini file to read and write from
    //
    void CIniFile::SetPath(const char * newpath)
    {
    path.erase();
    path.append( newpath );
    }//
    // reads ini file specified using CIniFile::SetPath()
    // returns true if successful, false otherwise
    //
    bool CIniFile::ReadFile()
    {
    ifstream inifile;
    string readinfo; //
    // 打开指定的文件
    //
    inifile.open(path.c_str());
    int curkey = -1, curval = -1;
    if (inifile.fail())
    {
    error = "Unable to open ini file.";
    return false;
    } string keyname, valuename, value;
    string temp; //
    // 从文件中读文本行数据
    //
    int posLeft, posRight;
    while ( getline(inifile, readinfo) )
    {
    if (strlen(readinfo.c_str()) > 0)
    {
    if (readinfo[0] == '[' && readinfo[readinfo.length()-1] == ']') //if a section heading
    {
    keyname = readinfo; posLeft = readinfo.find_first_not_of("[");
    posRight= readinfo.find_last_not_of("]"); keyname = readinfo.substr(posLeft, posRight);

    }
    else //if a value
    {
    valuename = readinfo.substr(0, readinfo.find("="));
    value = readinfo.substr(valuename.length()+1); SetValue(keyname.c_str(), valuename.c_str(), value.c_str());
    }
    }
    }
    inifile.close();
    return true;
    }
      

  6.   

    用Visual C++操作INI文件 
    发布者: soarlove ——>查看soarlove在VCCode发布的所有文章   
    发布日期:2002.08.21    
    升级次数:0  
    今日浏览:1  
    总浏览:334  
    评价等级:       
      
    0位用户为此文章评分,平均分为0.0 
    在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下:  一.将信息写入.INI文件中.  1.所用的WINAPI函数原型为: BOOL WritePrivateProfileString(
    LPCTSTR lpAppName,
    LPCTSTR lpKeyName,
    LPCTSTR lpString,
    LPCTSTR lpFileName
    );   其中各参数的意义:   LPCTSTR lpAppName 是INI文件中的一个字段名.   LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名.   LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.   LPCTSTR lpFileName 是完整的INI文件名.  2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 c:\stud\student.ini 文件中. CString strName,strTemp;
    int nAge;
    strName="张三";
    nAge=12;
    ::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");   此时c:\stud\student.ini文件中的内容如下:   [StudentInfo]
       Name=张三  3.要将学生的年龄保存下来,只需将整型的值变为字符型即可:strTemp.Format("%d",nAge);
    ::WritePrivateProfileString("StudentInfo","Age",strTemp,"c:\\stud\\student.ini"); 
     二.将信息从INI文件中读入程序中的变量.  1.所用的WINAPI函数原型为:DWORD GetPrivateProfileString(
    LPCTSTR lpAppName, 
    LPCTSTR lpKeyName, 
    LPCTSTR lpDefault, 
    LPTSTR lpReturnedString, 
    DWORD nSize, 
    LPCTSTR lpFileName 
    );   其中各参数的意义:    前二个参数与 WritePrivateProfileString中的意义一样.   lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.    lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.   nSize : 目的缓存器的大小.   lpFileName : 是完整的INI文件名.  2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.CString strStudName;
    int nStudAge; 
    GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");   执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".  3.读入整型值要用另一个WINAPI函数: UINT GetPrivateProfileInt(
    LPCTSTR lpAppName, 
    LPCTSTR lpKeyName, 
    INT nDefault, 
    LPCTSTR lpFileName 
    );   这里的参数意义与上相同.使用方法如下:nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini"); 
    三.循环写入多个值,设现有一程序,要将最近使用的几个文件名保存下来,具体程序如下:  1.写入:CString strTemp,strTempA;
    int i;
    int nCount=6;
    file://共有6个文件名需要保存
    for(i=0;i {strTemp.Format("%d",i);
    strTempA=文件名;
    file://文件名可以从数组,列表框等处取得.
    ::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,
    "c:\\usefile\\usefile.ini");
    }
    strTemp.Format("%d",nCount);
    ::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");
    file://将文件总数写入,以便读出.   2.读出:nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");
    for(i=0;i {strTemp.Format("%d",i);
    strTemp="FileName"+strTemp;
    ::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\\usefile\\usefile.ini");file://使用strTempA中的内容.}   补充:   1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 FALSE 值.   2.文件名的路径中必须为 \\ ,因为在VC++中, \\ 才表示一个 \ .   3.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: ".\\student.ini".
     
      

  7.   

    关注中!那么如何读、写.txt文件,如何插入,删除.txt中的特定内容呢?
      

  8.   


    //
    // writes data stored in class to ini file
    //
    bool CIniFile::WriteFile()
    {
    ofstream inifile;
    inifile.open(path.c_str());
    if (inifile.fail())
    {
    error = "Unable to open ini file.";
    return false;
    }

    for (int keynum = 0; keynum < names.size(); keynum++)
    {
    if (keys[keynum].names.size() != 0)
    {
    inifile << '[' << names[keynum] << ']' << endl;
    for (int valuenum = 0; valuenum < keys[keynum].names.size(); valuenum++)
    {
    inifile << keys[keynum].names[valuenum] << "=" << keys[keynum].values[valuenum];
    if (valuenum != keys[keynum].names.size())
    inifile << endl;
    else
    if (keynum < names.size())
    inifile << endl;
    }
    if (keynum < names.size())
    inifile << endl;
    }
    }
    inifile.close(); return true;
    }//
    // deletes all stored ini data
    //
    void CIniFile::Reset()
    {
    keys.clear();
    names.clear();
    }//
    // returns number of keys currently in the ini
    //
    int CIniFile::GetNumKeys()
    {
    return keys.size();
    }//
    // returns number of values stored for specified key, or -1 if key found
    //
    int CIniFile::GetNumValues(const char * keyname)
    {
    int keynum = FindKey(keyname); if (keynum == -1)
    return -1; return keys[keynum].names.size();
    }
      

  9.   

    //
    // gets value of [keyname] valuename = 
    // overloaded to return string, int, and double
    //
    const char * CIniFile::GetValue(const char * keyname, const char * valuename)
    {
    int keynum = FindKey(keyname);
    int valuenum = FindValue(keynum,valuename); if (keynum == -1)
    {
    error = "Unable to locate specified key.";
    return "";
    } if (valuenum == -1)
    {
    error = "Unable to locate specified value.";
    return "";
    }
    return keys[keynum].values[valuenum].c_str();
    }//
    // gets value of [keyname] valuename = 
    // overloaded to return string, int, and double
    //
    int CIniFile::GetValueI(const char * keyname, const char * valuename)
    {
    return atoi( GetValue(keyname,valuename) );
    }//
    // gets value of [keyname] valuename = 
    // overloaded to return string, int, and double
    //
    double CIniFile::GetValueF(const char * keyname, const char * valuename)
    {
    return atof( GetValue(keyname, valuename) );
    }//
    // sets value of [keyname] valuename =.
    // specify the optional paramter as false (0) if you do not want it to create
    // the key if it doesn't exist. Returns true if data entered, false otherwise
    // overloaded to accept string, int, and double
    //
    bool CIniFile::SetValue(const char * keyname, const char * valuename, const char * value, bool create)
    {
    int keynum = FindKey(keyname);
    int valuenum = 0; //
    // find key
    //
    if (keynum == -1) //if key doesn't exist
    {
    if (!create) //and user does not want to create it,
    return false; //stop entering this key names.resize(names.size()+1);
    keys.resize(keys.size()+1);
    keynum = names.size()-1;

    names[keynum].append(keyname);
    } //
    // find value
    //
    valuenum = FindValue(keynum,valuename);
    if (valuenum == -1)
    {
    if (!create)
    return false; keys[keynum].names.resize(keys[keynum].names.size()+1);
    keys[keynum].values.resize(keys[keynum].names.size()+1);
    valuenum = keys[keynum].names.size()-1;
    keys[keynum].names[valuenum].append(valuename);
    }
    keys[keynum].values[valuenum].erase();
    keys[keynum].values[valuenum].append(value); return true;
    }//
    // sets value of [keyname] valuename =.
    // specify the optional paramter as false (0) if you do not want it to create
    // the key if it doesn't exist. Returns true if data entered, false otherwise
    // overloaded to accept string, int, and double
    //
    bool CIniFile::SetValueI(const char * keyname, const char * valuename, int value, bool create)
    {
    char temp[MaxIntegerLength];
    sprintf(temp, "%d", value);
    return SetValue(keyname, valuename, temp, create);
    }
      

  10.   

    //
    // sets value of [keyname] valuename =.
    // specify the optional paramter as false (0) if you do not want it to create
    // the key if it doesn't exist. Returns true if data entered, false otherwise
    // overloaded to accept string, int, and double
    //
    bool CIniFile::SetValueF(const char * keyname, const char * valuename, double value, bool create)
    {
    char temp[MaxDoubleLength];
    sprintf(temp, "%e", value); return SetValue(keyname, valuename, temp, create);
    }//
    // deletes specified value
    // returns true if value existed and deleted, false otherwise
    //
    bool CIniFile::DeleteValue(const char * keyname, const char * valuename)
    {
    int keynum = FindKey(keyname);
    int valuenum = FindValue(keynum,valuename); if (keynum == -1 || valuenum == -1)
    return false; keys[keynum].names.erase(keys[keynum].names.begin() + valuenum);
    keys[keynum].values.erase(keys[keynum].values.begin() + valuenum); return true;
    }//
    // deletes specified key and all values contained within
    // returns true if key existed and deleted, false otherwise
    //
    bool CIniFile::DeleteKey(const char * keyname)
    {
    int keynum = FindKey(keyname);
    if (keynum == -1)
    return false; keys.erase(keys.begin() + keynum);
    names.erase(names.begin() + keynum); return true;
    }/////////////////////////////////////////////////////////////////////
    // Private Functions
    ///////////////////////////////////////////////////////////////////////
    // returns index of specified key, or -1 if not found
    //
    int CIniFile::FindKey(const char * keyname)
    {
    int keynum = 0;
    while ( (keynum < keys.size()) && (names[keynum] != keyname) )
    keynum++; if (keynum == keys.size())
    return -1; return keynum;
    }//
    // returns index of specified value, in the specified key, or -1 if not found
    //
    int CIniFile::FindValue(int keynum, const char * valuename)
    {
    if (keynum == -1)
    return -1; int valuenum = 0;
    while ( (valuenum < keys[keynum].names.size()) && (keys[keynum].names[valuenum] != valuename) )
    valuenum++; if (valuenum == keys[keynum].names.size())
    return -1; return valuenum;
    }//
    // overloaded from original getline to take string
    //
    istream & CIniFile:: getline(istream & is, string & str)
    {
        char buf[2048];    is.getline(buf, 2048);
        str = buf;    return is;
    }// CIniFile.cpp eof
      

  11.   

    感谢各位的鼎立支持.
    还有 个小问题就是如何使用GETLINE这个函数,并把得到的字符流转换为CSTRING类型啊.
      

  12.   


       extern CRichEditCtrl* pmyRichEditCtrl;   int i, nLineLength, nLineCount = pmyRichEditCtrl->GetLineCount();
       CString strText, strLine;   // Dump every line of text of the rich edit control.
       for (i=0;i < nLineCount;i++)
       {
          nLineLength = pmyRichEditCtrl->LineLength(i);
          pmyRichEditCtrl->GetLine(i, strText.GetBuffer(nLineLength));
          strText.ReleaseBuffer(nLineLength);      strLine.Format(TEXT("line %d: '%s'\r\n"), i, strText.GetBuffer(0));
          afxDump << strLine;
       }