读写数据。 [英] reading and writing data.
本文介绍了读写数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个用于读取和写入数据的程序。它使用指针。
读取文件mm.jpg并将其写入jj.jpg(此处未指定)。但是jj.jpg是0字节,而mm.jpg是54字节。
我修改了整个程序。 void main()仅用于检查这些函数是否正常工作。
实际上,它包含有标题作为其他程序的一部分。
I am trying to write a program for reading and writing data. It uses pointers.
It reads a file mm.jpg to and writes it to jj.jpg (Not specified here). But jj.jpg is 0 bytes while mm.jpg 54 bytes.
I have modified the whole program. void main() is here just to check whether these function work correctly.
Actually , it is to be included has header as part of an other program.
long sizeoffile(char filename[])
{
fstream fil(filename,ios::in);
fil.seekg(ios::beg,ios::end);
long ans1 = fil.tellg();
return ans1;
}
bool readfile(char filename[],char * inputdat)
{
ifstream getit(filename,ios::in|ios::binary);
long filsize = sizeoffile(filename);
if(!getit)
{
return false;
}
inputdat = new char[filsize];
if(!inputdat)
{
return false;
}
getit.read((char*)inputdat,(filsize));
getit.close();
return true;
}
bool writefile(char filename[],char * outputdat,long buffsize)
{
ofstream putit(filename,ios::out|ios::binary);
if(!outputdat)
{
return false;
}
if(!putit)
{
; return false;
}
putit.write((char*)outputdat,(buffsize));
putit.close();
return true;
}
void main()
{
char * strdat = NULL;
if(!(readfile("mm.jpg",strdat)))
cout<<"Unable to read file.";
if(!writefile("jj.jpg",strdat,sizeoffile("mm.jpg")))
cout<<"Unable to write file.";
delete []strdat;
}
推荐答案
- 您正在混合文件大小变量的数据类型。不要使用
double
因为它会导致精度损失。- 您正在尝试从输入文件中读取比存在更多的数据:
- You are mixing datatypes for your file size variables. Do not use
double
as it can lead to loss of precision.- You are trying to read more data from the input file than exists:
getit.read((char*)inputdat,(filsize+sizeof(inputdat)));
// buffer is only filsize characters long
sizeof(inputdat)
和 sizeof(outputdat)
总是恒定的 - 字符指针的大小(32位为4位,64位为8位)。 writefile
函数中的输出文件名,可能为零;因此你不会写任何数据。sizeof(inputdat)
and sizeof(outputdat)
will alway be constant - the size of a character pointer (4 on 32-bit, 8 on 64-bit).writefile
function, which is likely to be zero; hence you don''t write any data.
我发现了我之前错过的问题。您需要根据以下注释修改代码:
I have found the problem, which I missed before. You need to modify your code as per the comments below:
// inputdat, needs to be the address of the pointer, rather than the buffer.
long readfile(char filename[],char ** inputdat)
{
ifstream getit(filename,ios::in|ios::binary);
if(!getit)
{
return 0;
}
long filsize = sizeoffile(filename,B);
// save the buffer address in the original pointer not
// the temporary parameter variable
*inputdat = new char[filsize];
if(!*inputdat)
{
return 0;
}
// read only the number of bytes in the file
getit.read((char*)*inputdat,(filsize));
getit.close();
// return the size of the input file (and buffer)
return filsize;
}
// use the size of the input file for the amount to write
bool writefile(char filename[],char * outputdat, long filesize)
{
ofstream putit(filename,ios::out|ios::binary);
// no need to call filesize, since the output file does not exist
if(!putit)
{
return false;
}
// write the correct number of bytes
putit.write((char*)outputdat,(filesize));
putit.close();
return true;
}
int main()
{
char* buffer = NULL;
long filesize;
// send the address of the buffer pointer to the read function
filesize = readfile("TestIn.bmp", &buffer);
// if some data was read in
if (filesize > 0)
{
// write the number of bytes read in to the buffer
writefile("TestOut.bmp", buffer, filesize);
}
return 0;
}
这篇关于读写数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文