怀疑从INI文件中读取数值 [英] Doubt in reading numeric values from INI file

查看:77
本文介绍了怀疑从INI文件中读取数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在我的应用程序中,我想读取ini文件中的两个整数值。该值应该在-1到1023的范围内。我有使用GetPrivateProfileString()读取值并使用_wtoi()将字符串转换为整数。

但是如果我输入一些无效值,如字符串值或空值,_wtoi会将值返回为零。在我的情况下这是一个有效的数字,但它不应该发生。



所以请告诉我是否可以在读取ini文件或任何文件中的值时指定任何范围_wtoi()的其他替代方案。



提前谢谢...

Hi,

In my application, I want read the two integer values from ini file.That value should be in the range -1 to 1023. I have used GetPrivateProfileString() to read the values and converted the string to integer using _wtoi().
But if I enter some invalid values like string values or empty values, the _wtoi returns the value as zero. That is a valid number in my case, but it should not happen.

So please tell me whether I can specify any range while reading the value from ini file or any other alternative for _wtoi().

Thanks in advance...

推荐答案

最后我决定了编写我自己的函数,它将从INI读取整数值并根据输入进行验证,如下所示。



Finally I have decided to write my own function which will read the integer value from INI and does the validation according to the input as shown below.

long ReadIntegerValueFromIni( const CString& csAppName_i,
                              const CString& csKeyName_i,
                              const CString& csDefaultVal_i,
                              const CString& csFileName_i,
                              const int nMinVal_i,
                              const int nMaxVal_i )
{
    long lKeyValue = 0;
    int nDefaultVal = _ttoi( csDefaultVal_i );
    try
    {
        const int SUCCESS_RET = 1;
        TCHAR tcszBuf[ MAX_PATH ];
        TCHAR tcszBufTemp[ MAX_PATH ];
        lKeyValue = nDefaultVal;

        // Read the value from INI file.
        GetPrivateProfileString( csAppName_i, csKeyName_i, csDefaultVal_i, tcszBuf, sizeof( tcszBuf ),
                                 csFileName_i );
        // Check whether the value read from ini is a valid numeric number or not.
        if( SUCCESS_RET != swscanf( tcszBuf, _T( "%d%s" ), &lKeyValue, &tcszBufTemp ))
        {
            lKeyValue = nDefaultVal;
            // Invalid value found so adjusted to Default Value.
        }
        // Check whether the value read from ini is within the allowed range or not.
        else if( nMinVal_i > lKeyValue || nMaxVal_i < lKeyValue )
        {
            lKeyValue = nDefaultVal;
            // Input value is not within the allowed range, so adjusted to Default Value
        }
        else
        {
            // Do nothing.
        }
    }
    catch( ... )
    {
        // Unhandled exception occured in ReadIntegerValueFromIni()
        lKeyValue = nDefaultVal;
    }
    return lKeyValue;
}





欢迎提出有效意见。



Valid comments are welcome.


您不能指定任何GetPrivbateProfileString的限制 - 它返回它找到的内容,并且只能给出一个默认值(可能是有效范围之外的数字)。但是如果找不到INI文件中定义的键,它只会应用默认值。



没有相应的_wtoi可以满足您的要求 - 它返回0如果函数失败。



我建议您预先处理返回的字符串,并自行检查它的有效性。
You can't specify any limits on GetPrivbateProfileString - it returns what it finds, and can only be given a default value (which could be a number outside your valid range). But it only applied the default if it does not find the key defined in the INI file.

There is no equivalent of _wtoi that does what you ask - it returns 0 if the function fails.

I would suggest that you pre-process the returned string, and check it for validity yourself.


您可以使用 GetPrivateProfileInt [ ^ ]功能,提供默认值值超出允许范围(例如 16384 ),然后检查返回值。
You may use instead the GetPrivateProfileInt[^] function, providing a default value 'out of the allowed range' (e.g. 16384) and then check the return value.


这篇关于怀疑从INI文件中读取数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆