文件IO,malloc分配的字节数不正确 [英] File IO, malloc allocating incorrect number of bytes

查看:100
本文介绍了文件IO,malloc分配的字节数不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,但是对Java编程有基本的了解.现在,我在尝试从磁盘读取数据时遇到问题.从文件中读取字节数后,我通过malloc分配内存并创建一个新字符串来存储文件中的数据字符串.然后我删除空格并尝试创建字节数组,但是当字符无效时崩溃

这是我的readfile.dat
中的数据字符串

Hi, I rather new in C++ but have a basic understanding of programming in terms of Java. Right now I am having problems trying to read data from disk. After reading the number of bytes from the file, I allocate memory via malloc and create a new string to store the string of data from file. Then i remove the white spaces and try to create the byte array, however it crashes when the character is invalid

this is the string of data in my readingfile.dat

10 02 01 02 03 04 0a 00 03 10



将方法转换为字节数组



converting method to byte array

void string_to_bytearray(std::string str, unsigned char* &array, int& size)
{
	int length = str.length();
	// make sure the input string has an even digit numbers
	if(length%2 == 1)
	{
		str = "0" + str;
		length++;
	}

	// allocate memory for the output array
	array = new unsigned char[length/2];
	size = length/2;

	std::stringstream sstr(str);
	for(int i=0; i < size; i++)
	{
		char ch1, ch2;
		sstr >> ch1 >> ch2;
		int dig1, dig2;
// crashes here when an invalid char was passed in
		if(isdigit(ch1)) dig1 = ch1 - '0';
		else if(ch1>='A' && ch1<='F') dig1 = ch1 - 'A' + 10;
		else if(ch1>='a' && ch1<='f') dig1 = ch1 - 'a' + 10;
		if(isdigit(ch2)) dig2 = ch2 - '0';
		else if(ch2>='A' && ch2<='F') dig2 = ch2 - 'A' + 10;
		else if(ch2>='a' && ch2<='f') dig2 = ch2 - 'a' + 10;
		array[i] = dig1*16 + dig2;
	}
}



我的读取文件方法



my read file method

unsigned long ReadFromDisk()
{

	FILE	*pFile;
	long	lSize;
	char	*pCharbuff;
	
	byte	abBuffer[128];
	
	pFile = fopen ( "W:\\SW\\readingfile.dat" , "rb" );

	fseek(pFile , 0 , SEEK_END);
	lSize = ftell (pFile);
	rewind(pFile);

	// allocate the buffer
	pCharbuff = (char*) malloc (sizeof(char)*lSize);

// problem is here, the file has only 29 bytes of data and yet it allocates extra 4 bytes, and when checking bytes if it is a digit or not, it gives an error
	fread (pCharbuff,1,lSize,pFile);

// sorry forgot this line
std::string strString(pCharbuff);

	// remove white spaces
	bool spacesLeft = true;
	std::string::size_type pos = 0;
	while(spacesLeft)
	{
		pos = strString.find(" ");
		if(pos != std::string::npos)
			strString.erase( pos, 1 );
		else
			spacesLeft = false;
	}

	unsigned char* array = NULL;
	int size;
	string_to_bytearray(strString, array, size);	

	return 0;
}



此外,我希望将其实现为线程,这样我就可以从每次退出的同一位置读取内容.我怎么做?我可以在fseek下设置一些偏移吗?



furthermore i wish to implement this as a thread, so i can read from the same postion i left off everytime. How do i do that? can i set some offset under fseek?

推荐答案

fread (pCharbuff,1,lSize,pFile);

// remove white spaces
bool spacesLeft = true;
std::string::size_type pos = 0;
while(spacesLeft)
{
    pos = strString.find(" ");


您的字符串变量strString尚未用从文件读取的缓冲区的内容初始化.

[更新]
问题还在于您没有正确终止输入数据,因此strString(pCharbuff);构造函数正在拾取垃圾.更改代码,如下所示:


Your string variable strString has not been initialised with the contents of the buffer read from the file.

[UPDATE]
The problem also is that you have not terminated your input data properly so the strString(pCharbuff); constructor is picking up garbage. Change the code as follows:

pCharbuff = (char*) malloc (sizeof(char)* (lSize + 1));
fread (pCharbuff,1,lSize,pFile);
pCharbuff[lSize] = '\0';  \\ add null terminator to end of data
std::string strString(pCharbuff);


[/UPDATE]


[/UPDATE]


您写道:
// problem is here, the file has only 29 bytes of data and yet it allocates extra 4 bytes


参见 http://www.cplusplus.com/reference/clibrary/cstdio/ftell/ [ ^ ]:

对于文本流,不能保证该值是从文件开头算起的确切字节数

我不确定这对我自己意味着什么,但这可能是由于文本文件中看不见的控制字符所致,例如行尾,文件尾等.如果是这种情况,那么消除空格是不够的-您可能还需要消除其他空格字符.

要删除所有空格,您可以尝试:


See http://www.cplusplus.com/reference/clibrary/cstdio/ftell/[^] :

For text streams, the value is not guaranteed to be the exact number of bytes from the beginning of the file

I''m not sure what this means myself, but this could be due to control characters that are not visible in a text file, such as end of line, end of file, and the like. If that is the case, then eliminating spaces wouldn''t be enough - you might also need to eliminate other whitespace characters.

To remove all whitespace you could try:

str.erase(remove_if(str.begin(), str.end(), ::isspace), str.end());


(请参见 http://stackoverflow.com/questions/83439/remove-spaces-from- stdstring-in-c [ ^ ])


(see http://stackoverflow.com/questions/83439/remove-spaces-from-stdstring-in-c[^])


这篇关于文件IO,malloc分配的字节数不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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