嗨,我宁愿在C,但在Java的编程基本的了解。现在我试图从磁盘读取数据的问题。从文件中读取的字节数后,我通过malloc分配内存,并创建一个新的字符串来存储数据从文件中的字符串。然后,我删除空格和尝试创建的字节数组,但是它崩溃时是无效的字符
这是数据我readingfile.dat的字符串10 02 01 02 03 04 0a 00 03 10
转换方法的字节数组
{C}
我读文件的方法
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抵消一些呢?