现有如下格式的文件pram1 = ddd;
pram2 = dfe;
.......
如何把等号后面的东西读出到一个变量中啊,其中等号后的位置是不定的,字符串处理高手,请帮忙,谢谢,最好详细点,要不搞不定

解决方案 »

  1.   

    BOOL GetValueStr(char *InputStr, char *OutputStr, WORD OutputLen)
    {
    WORD i;
    for (i=0; i<strlen(InputStr); i++)
    {
    if (*(InputStr +i) == '=')
    break;
    }
    if (i == strlen(InputStr))
    {
    return FALSE;
    }
    i++;
    for (; i<strlen(InputStr); i++)
    {
    if (*(InputStr +i) != ' ')
    break;

    }
    if (i == strlen(InputStr))
    {
    return FALSE;
    }
    i++;
    strcpy(OutputStr, InputStr + i);
    return TRUE;
    }
      

  2.   

    一行一行的读吧
    CStdioFile f("your file name");
    CString str;
    while(f.ReadString(str))
    {
        int index = str.Find('=');
        CString temp = str.Right(str.GetLength()-index-1);
    }
      

  3.   

    楼主可以把文件名改为.ini格式,这样,可以用程序方便的实现你需要的要求。关于读取INI文件有专门得方法,楼主参考以下文章:http://www.gameres.com/Articles/Program/Control/Vcini.htm有源代码下载。
      

  4.   

    例如:
    char a[]="asdd = bbbbss";
    char b[256];
    GetValueStr(a,b,256);
    则b就为"bbbbss";
      

  5.   

    写一个处理一行数据的函数(比如说 function(CString str))
    在 str 中找到 '=' ,把'='后面的东西取出来不就行了。读文件时一行一行的读,每一行数据调用一次 function好像不难啊
      

  6.   

    还有一篇文章供参考:
    http://www.czvc.com/view.asp?id=12
      

  7.   

    用熟CStdioFile,CString这两个类吧
      

  8.   

    将你的文件做成ini文件
    用DWORD GetPrivateProfileString(
      LPCTSTR lpAppName,
      LPCTSTR lpKeyName,
      LPCTSTR lpDefault,
      LPTSTR lpReturnedString,
      DWORD nSize,
      LPCTSTR lpFileName
    )
    指定AppName、KeyName、nSize和FileName,将=号后面的内容读到ReturnString中
      

  9.   

    把每一行读入到一个CString对象,然后用下面的getValue解析这个字符串,最后根据解析后的字符串给变量赋值getValue(CString szSource, // 输入字符串
    CString& szProperty, // 输出属性名
    CString& szValue // 输出值
    )
    {
    szValue.Empty();
    szProperty.Empty();
    szSource.TrimLeft();
    szSource.TrimRight();
    int nEqual = szSource.Find("="); // 查找等号
    if(nEqual ==-1)
    {
    AfxMessageBox("Couldnot find '=' while reading file.." );
    return;
    }
    szProperty = szSource.Left(nEqual); // 属性名
    szProperty.TrimLeft();
    szProperty.TrimRight();
    if(szProperty.IsEmpty())
    {
    AfxMessageBox("Couldnot find propertyname ..");
    return;
    } CString szLocalValue = szSource.Mid(nEqual+1);
    int nSemi= szLocalValue.Find(';'); // 查找行末的分号
    if(nSemi == -1)
    {
    AfxMessageBox("Couldnot find ';' ..");
    szValue = szLocalValue;
    return;
    }
    szValue = szLocalValue.Left(nSemi);
    szValue.TrimLeft();
    szValue.TrimRight();
    if(szValue.IsEmpty())
    AfxMessageBox("Empty Value for the property ..");
    }BOOL loadProperty()
    {
    CString szStrBuff, szProperty, szValue;
    ... // 打开文件操作
    for (...) // 自己判断如何结束循环
    {
    ... // 按行读取文件内容到szStrBuff,并移动文件指针。 getValue(szStrBuff,szProperty,szValue);
    if(!szProperty.Compare("pram1"))
    {
    nparam1 = atoi(szValue); // 这是把ddd当作整数读入。
    continue;
    }
    ... // 其他操作
    }
    }
      

  10.   

    是啊,如果文件行数不多,可以用INI文加,这样就可以方便的 GetPrivateProfileIntGetPrivateProfileString拉
      

  11.   

    我倒是读过INI文件,而且我的这个文件就是如上的格式,不是象上面说的那样,还有什么AppName之类的,只能采取一行一行的读了。楼上说的对,基本的类都没有用熟悉啊!
      

  12.   

    按行:CStdioFile ::ReadString----〉str
    然后可以取得等号得位置,然后可以mid取得前面和后面的字符串,可以用一下TrimLeft,TrimRight去掉空格
      

  13.   

    多蒙各位启发与帮助,最后问题解决如下,文件共有十行,依次显示到程序的编辑框里面
        CStdioFile f("c:\\dt.ini",CFile::modeRead);
        f.SeekToBegin();
        CString strLine;
        int  nID_Index[10]={IDC_EDIT_DEV,
    IDC_EDIT_SHARE,
    IDC_EDITCARD,
    IDC_EDIT_SHARE,
    IDC_EDITMULTIT,
    IDC_EDITSHAREDM,
    IDC_EDIT_SERNUM,
    IDC_EDIT_SS_IPADDRESS,
    IDC_EDIT_SETIMEOUT,
    IDC_EDIT_SOMEOUT};
    for(int i = 0;i<10;i++)
    {
    strLine.Empty();
    f.ReadString(strLine);
    int npos = strLine.ReverseFind(';');  
    int mpos = strLine.ReverseFind(' ');  
    strLine.TrimRight(';');
    CString strValue = strLine.Right(npos-mpos-1); 
    SetDlgItemText(nID_Index[i],strValue);
    }
    f.Close();
    另外:dt.ini的内容如下:
    DCount = 0;
    SNum = 1;
    ddresses = 192.168.0.18;
    WaitI = 300;
    Shar = 1111;
    CardL = 2222;
    Shared = 65537;
    Multi = 4444;
    Sha = 5555;
    Sem = 300;
    Soc = 160;
    .##
    第一次处理字符串,请各位提出宝贵意见以及优化代码的方法
      

  14.   

    CStdioFile cFile;
    CString strTemp;
    CString strTarget;
    cFile.Open("yourfilepath", CFile::modeRead);
    while (cFile.ReadString(strTemp))
    {
    int pos = strTemp.Find('=');
    strTarget = strTemp;
    strTarget.Right(strTemp.GetLength()-pos-1);
    }
    cFile.Close();看看MSDN关于CStdioFile 的说明吧!
    ^_^
      

  15.   

    int npos = strLine.ReverseFind(';');  
    int mpos = strLine.ReverseFind(' ');  
    strLine.TrimRight(';');想干what?没看明白。去掉;??哪就mid的时候少取一位长度就可以了
      

  16.   

    主要用指针定位,循环读=号,两个指针,一个指=号,一个指;号,计算中间长度,取数据,再循环
    ,前不久刚写了一个处理HTML文件找数据的工具,呵