1. C++的问题,怎样按字节读取二进制文件
这个功能完全可以用 C 语言实现,你干吗非要用 C++ 呢? 语言实现就足够了。
不要为了 C++ 而写 C++ 程序,这样写出来的源程序,可读性较差。只有当问题本身确实适合用 C ++ 来描述时,再用 C ++ 写程序,这样才自然。而且了,在 C ++ 程序中,写 C 语言的函数也完全没有问题,至少这个功能你可以用 C 代码写。
如果你需要对文本文件进行读、写操作,用:"r" 、"w" 方式,
如果你需要对二进制文件进行读、写操作,用:"rb" 、"wb" 方式。
如果你确保每一条记录的字节数都完全相同,你可以使用:fread()、fwrite() 库函数实现你的功能。
#include <stdio.h>
void main()
{
FILE * fpr ; * fpw;
unsigned char record[60] ; /* 假设每一条记录是 60 个字节 */
if( ( fpr=fopen("source", "rb") ) == NULL )
{
printf("Can't open source file for read\n") ;
exit(1);
}
if( ( fpw = fopen("target", "wb") ) == NULL )
{
printf("Can't open target file for write!\n") ;
exit(1);
}
fread( record, sizeof(record), 1, fpr ) ; /* 读一条记录 */
......
fwrite( record, sizeof(record), 1, fpw ) ; /* 写一条记录 */
fclose(fpr) ; fclose(fpw) ;
}
2. 利用字节文件输入输出流,编写程序完成文件的读,写,复制功能。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
/**
* 多种方式读文件内容。 按字节读取文件内容、按字符读取文件内容、按行读取文件内容、随机读取文件内容
*/
public class ReadFromFile
{
public static void main(String[] args)
{
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*
* @param fileName
* 文件的名
*/
public static void readFileByBytes(String fileName)
{
File file = new File(fileName);
InputStream in = null;
try
{
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1)
{
System.out.write(tempbyte);
}
in.close();
}
catch (IOException e)
{
e.printStackTrace();
return;
}
try
{
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1)
{
System.out.write(tempbytes, 0, byteread);
}
}
catch (Exception e1)
{
e1.printStackTrace();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e1)
{
}
}
}
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*
* @param fileName
* 文件名
*/
public static void readFileByChars(String fileName)
{
File file = new File(fileName);
Reader reader = null;
try
{
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1)
{
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r')
{
System.out.print((char) tempchar);
}
}
reader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1)
{
// 同样屏蔽掉\r不显示
if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != '\r'))
{
System.out.print(tempchars);
}
else
{
for (int i = 0; i < charread; i++)
{
if (tempchars[i] == '\r')
{
continue;
}
else
{
System.out.print(tempchars[i]);
}
}
}
}
}
catch (Exception e1)
{
e1.printStackTrace();
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e1)
{
}
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*
* @param fileName
* 文件名
*/
public static void readFileByLines(String fileName)
{
File file = new File(fileName);
BufferedReader reader = null;
try
{
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null)
{
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e1)
{
}
}
}
}
/**
* 随机读取文件内容
*
* @param fileName
* 文件名
*/
public static void readFileByRandomAccess(String fileName)
{
RandomAccessFile randomFile = null;
try
{
System.out.println("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
// 读文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
// 将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1)
{
System.out.write(bytes, 0, byteread);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (randomFile != null)
{
try
{
randomFile.close();
}
catch (IOException e1)
{
}
}
}
}
/**
* 显示输入流中还剩的字节数
*
* @param in
*/
private static void showAvailableBytes(InputStream in)
{
try
{
System.out.println("当前字节输入流中的字节数为:" + in.available());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/////////////////////////////////////////////////////////////////
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFileUtil
{
/**
* 复制整个目录的内容,如果目标目录存在,则不覆盖
*
* @param srcDirName
* 待复制的目录名
* @param destDirName
* 目标目录名
* @return 如果复制成功返回true,否则返回false
*/
public static boolean Directory(String srcDirName, String destDirName)
{
return CopyFileUtil.Directory(srcDirName, destDirName, false);
}
/**
* 复制整个目录的内容
*
* @param srcDirName
* 待复制的目录名
* @param destDirName
* 目标目录名
* @param overlay
* 如果目标目录存在,是否覆盖
* @return 如果复制成功返回true,否则返回false
*/
public static boolean Directory(String srcDirName, String destDirName, boolean overlay)
{
// 判断原目录是否存在
File srcDir = new File(srcDirName);
if (!srcDir.exists())
{
System.out.println("复制目录失败:原目录" + srcDirName + "不存在!");
return false;
}
else if (!srcDir.isDirectory())
{
System.out.println("复制目录失败:" + srcDirName + "不是一个目录!");
return false;
}
// 如果目标文件夹名不以文件分隔符结尾,自动添加文件分隔符
if (!destDirName.endsWith(File.separator))
{
destDirName = destDirName + File.separator;
}
File destDir = new File(destDirName);
// 如果目标文件夹存在,
if (destDir.exists())
{
if (overlay)
{
// 允许覆盖则删除已存在的目标目录
System.out.println("目标目录已存在,准备删除它!");
if (!DeleteFileUtil.delete(destDirName))
{
System.out.println("复制目录失败:删除目标目录" + destDirName + "失败!");
}
}
else
{
System.out.println("复制目录失败:目标目录" + destDirName + "已存在!");
return false;
}
}
else
{
// 创建目标目录
System.out.println("目标目录不存在,准备创建它!");
if (!destDir.mkdirs())
{
System.out.println("复制目录失败:创建目标目录失败!");
return false;
}
}
boolean flag = true;
// 列出源文件夹下所有文件(包括子目录)的文件名
File[] files = srcDir.listFiles();
for (int i = 0; i < files.length; i++)
{
// 如果是一个单个文件,则进行复制
if (files[i].isFile())
{
flag = CopyFileUtil.File(files[i].getAbsolutePath(), destDirName + files[i].getName());
if (!flag)
{
break;
}
}
// 如果是子目录,继续复制目录
if (files[i].isDirectory())
{
flag = CopyFileUtil.Directory(files[i].getAbsolutePath(), destDirName + files[i].getName());
if (!flag)
{
break;
}
}
}
if (!flag)
{
System.out.println("复制目录" + srcDirName + "至" + destDirName + "失败!");
return false;
}
System.out.println("复制目录" + srcDirName + "至" + destDirName + "成功!");
return true;
}
/**
* 复制单个文件, 如果目标文件存在,则不覆盖。
*
* @param srcFileName
* 待复制的文件名
* @param destFileName
* 目标文件名
* @return 如果复制成功,则返回true,否则返回false
*/
public static boolean File(String srcFileName, String destFileName)
{
return CopyFileUtil.File(srcFileName, destFileName, false);
}
/**
* 复制单个文件
*
* @param srcFileName
* 待复制的文件名
* @param destFileName
* 目标文件名
* @param overlay
* 如果目标文件存在,是否覆盖
* @return 如果复制成功,则返回true,否则返回false
*/
public static boolean File(String srcFileName, String destFileName, boolean overlay)
{
// 判断原文件是否存在
File srcFile = new File(srcFileName);
if (!srcFile.exists())
{
System.out.println("复制文件失败:原文件" + srcFileName + "不存在!");
return false;
}
else if (!srcFile.isFile())
{
System.out.println("复制文件失败:" + srcFileName + "不是一个文件!");
return false;
}
// 判断目标文件是否存在
File destFile = new File(destFileName);
if (destFile.exists())
{
// 如果目标文件存在,而且复制时允许覆盖。
if (overlay)
{
// 删除已存在的目标文件,无论目标文件是目录还是单个文件
System.out.println("目标文件已存在,准备删除它!");
if (!DeleteFileUtil.delete(destFileName))
{
System.out.println("复制文件失败:删除目标文件" + destFileName + "失败!");
return false;
}
}
else
{
System.out.println("复制文件失败:目标文件" + destFileName + "已存在!");
return false;
}
}
else
{
if (!destFile.getParentFile().exists())
{
// 如果目标文件所在的目录不存在,则创建目录
System.out.println("目标文件所在的目录不存在,准备创建它!");
if (!destFile.getParentFile().mkdirs())
{
System.out.println("复制文件失败:创建目标文件所在的目录失败!");
return false;
}
}
}
// 准备复制文件
int byteread = 0;// 读取的位数
InputStream in = null;
OutputStream out = null;
try
{
// 打开原文件
in = new FileInputStream(srcFile);
// 打开连接到目标文件的输出流
out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
// 一次读取1024个字节,当byteread为-1时表示文件已经读完
while ((byteread = in.read(buffer)) != -1)
{
// 将读取的字节写入输出流
out.write(buffer, 0, byteread);
}
System.out.println("复制单个文件" + srcFileName + "至" + destFileName + "成功!");
return true;
}
catch (Exception e)
{
System.out.println("复制文件失败:" + e.getMessage());
return false;
}
finally
{
// 关闭输入输出流,注意先关闭输出流,再关闭输入流
if (out != null)
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
// 复制单个文件,如果目标存在,则覆盖
String srcPath = "C:/temp/tempfile0.txt";
String destPath = "C:/temp_bak/tempfile0_bak.txt";
CopyFileUtil.File(srcPath, destPath, true);
// 如果目标存在,则不覆盖
CopyFileUtil.File(srcPath, destPath);
System.out.println();
// 复制文件夹,如果目标存在,则覆盖
String srcDir = "C:/temp";
String destDir = "D:/temp";
CopyFileUtil.Directory(srcDir, destDir, true);
}
}
3. PLC中字节 字 双字的用法
题目太大了。常规是:
1、字节:比较,判断、循环计数、从字或双字取出自己需要的字节来专门某个目的使用等等;2、字:正数运算、比较判断,数据输出、显示等等;
3、双字:浮点数运算等等。
PLC中字节 字 双字的用法,程序设计手册都有介绍。关键需要编程实践中自己领悟,不能一概而论,有的资料个别有错,需要字节动手编程验证,变成自己的真实理解和应用规则。听一万遍不如自己动手做一遍!!!
4. 简述什么叫做字节对齐,编程时使用什么方式在代码中说
什么是字节对齐:
字节(Byte)是计算机信息技术用于计量存储容量和传输容量的一种计量单位,一个字节等于8位二进制数,在UTF-8编码中,一个英文字符等于一个字节。
字节按照一定规则在空间上排列就是字节对齐。
解释:
现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问,这就需要各种类型数据按照一定的规则在空间上排列,而不是顺序的一个接一个的排放,这就是对齐。
作用和原因:
各个硬件平台对存储空间的处理上有很大的不同。一些平台对某些特定类型的数据只能从某些特定地址开始存取。比如有些架构的CPU在访问 一个没有进行对齐的变量的时候会发生错误,那么在这种架构下编程必须保证字节对齐。其他平台可能没有这种情况,但是最常见的是如果不按照适合其平台要求对 数据存放进行对齐,会在存取效率上带来损失。
比如有些平台每次读都是从偶地址开始,如果一个int型(假设为32位系统)如果存放在偶地址开始的地方,那么一个读周期就可以读出这32bit,而如果存放在奇地址开始的地方,就需要2个读周期,并对两次读出的结果的高低字节进行拼凑才能得到该32bit数据。
准则:
其实字节对齐的细节和具体编译器实现相关,但一般而言,满足三个准则:
1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除;
2) 结构体每个成员相对于结构体首地址的偏移量都是成员大小的整数倍,如有需要编译器会在成员之间加上填充字节;例如上面第二个结构体变量的地址空间。
3) 结构体的总大小为结构体最宽基本类型成员大小的整数倍,如有需要编译器会在最末一个成员之后加上填充字节。
概念与规则:
四个基本概念
1.数据类型自身的对齐值:对于char型数据,其自身对齐值为1,对于short型为2,对于int,float类型,其自身对齐值为4,对于double型,其自身对齐值为8,单位字节。
2.结构体或者类的自身对齐值:其成员中自身对齐值最大的那个值。
3.指定对齐值:#pragma pack (value)时的指定对齐值value。
4.数据成员、结构体和类的有效对齐值:自身对齐值和指定对齐值中小的那个值。对齐规则有效对齐值N是最终用来决定数据存放地址方式的值,最重要。有效对齐N,就是表示“对齐在N上”,也就是说该数据的"存放起始地址%N=0".而数据结构中的数据变量都是按定义的先后顺序来排放的。第一个数据变量的起始地址就是数据结构的起始地址。结构体的成员变量要对齐排放,结构体本身也要根据自身的有效对齐值圆整。
5. 小弟初学PLC编程时,什么时候该用字节,什么时候该用字,什么时候该用双字
字节只能表示0-255的数字。字可以表示0-65535,双字可以表示n多亿。如果你的数值不大或者需要按位寻址那么字节就行。如果数值较大用字。数值很大或浮点数那得用双字了。