用C ++编写文件 [英] Writing a file with C++

查看:90
本文介绍了用C ++编写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试复制 - 使用C ++ fstream函数编写图像(不使用任何标准命令来使用C ++复制文件)。



以下是我写的代码:



 void main()
{
char * imgs = NULL;
imgs = new char [filsz()];
ifstream inm(aaa.jpg,ios :: in | ios :: binary);
for(int i = 0; i< filsz(); i ++)
{
inm>> imgs [i];
}
ofstream outnm(linm.jpg,ios :: out | ios :: binary);
for(int i = 0; i< filsz(); i ++)
{
outnm.put(imgs [i]);
}



}

long filsz()
{
fstream inm(aaa.jpg ,IOS ::中);
inm.seekg(ios :: beg,ios :: end);
long sz = inm.tellg();
返回sz;

}
< / pre>







输入和输出文件的文件大小是相同的,我可以用Windows图像查看器打开原始文件但是当它复制文件时它不会打开并显示错误。



可能是什么原因?

解决方案

我要做的第一件事就是改变 char unsigned char 以防它丢失最高位,并尝试缓存文件大小以节省浪费:你正在调用 filsz 函数用于文件中的每个字节,两次,因为每次 for 循环时都需要查看它是否应该再次循环:

  int  bytes = filsz(); 
imgs = new unsigned char [字节];
ifstream inm( aaa.jpg,ios :: in | ios :: binary );
for int i = 0 ; i< bytes; i ++)





如果不排序,那么你需要看一下实际情况文件中的数据,用于计算输出的输出内容。 PsPad [ ^ ]有一个HEX模式,这将使相对简单(并且它不是一个糟糕的编辑器,如果你需要比VS可以提供​​更复杂的编辑 - 我不会使用它很多,但它可以节省很多时间)



愚蠢的HTML ... - OriginalGriff [/ edit]


我看到你没有关闭这两个文件中的任何一个。虽然在阅读文件时没问题,但在将所有数据写入文件后,请用一个简单的关闭它。


outnm.close();
写循环后






这将刷新系统通常缓存的文件中的所有内容

I am trying to copy - write an image using C++ fstream functions (without using any standard commands for copying files using C++).

Below is the code i wrote :

void main()
{
	char *imgs=NULL;
	imgs = new char[filsz()];
	ifstream inm("aaa.jpg",ios::in|ios::binary);
	for(int i=0;i<filsz();i++)
	{
		inm>>imgs[i];
	}
	ofstream outnm("linm.jpg",ios::out|ios::binary);
	for(int i=0;i<filsz();i++)
	{
		outnm.put(imgs[i]);
	}
	
	
	
}

long filsz()
{
	fstream inm("aaa.jpg",ios::in);
	inm.seekg(ios::beg,ios::end);
	long sz = inm.tellg();
	return sz;
	
}
</pre>




The file size of the input and output file was the same , i could open the original file with windows image viewer however when it comes to copied file it wont open and shows an error.

What could be the reason ?

解决方案

The first thing I would do, is to change from char to unsigned char in case it is "losing" the top bit, and try caching the file size to save waste: you are calling the filsz function for each and every byte in the file, twice, as it is called each time the for loop needs to see if it should go round again:

int bytes = filsz();
imgs = new unsigned char[bytes];
ifstream inm("aaa.jpg",ios::in|ios::binary);
for(int i=0; i < bytes; i++)



If that doesn''t sort it, then you need to look at the actual data in the file(s) to work out what is being output for what input. PsPad[^] has a HEX mode which would make that relatively simple (and it''s not a bad editor, either if you need more complex editing than VS can provide - I don''t use it much, but it can save a lot of time)

[edit]Stupid HTML... - OriginalGriff[/edit]


I see that you haven''t closed any of the two files. While its okay when you are reading the file, after writing all the data to the file, close it with a simple

outnm.close();

after the write loop.

This will flush all the contents in the file that the system usually caches


这篇关于用C ++编写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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