C++ 如何在c++;
我试图将十六进制值从文件存储到某种形式的数组中,然后将这些数字更改为二进制。到目前为止,我编写的程序只收集最后一个值。文件看起来像这样。注(每个十六进制值前为1或0) 我的代码是#includeC++ 如何在c++;,c++,string,file-io,C++,String,File Io,我试图将十六进制值从文件存储到某种形式的数组中,然后将这些数字更改为二进制。到目前为止,我编写的程序只收集最后一个值。文件看起来像这样。注(每个十六进制值前为1或0) 我的代码是#include #include <fstream> #include <string> #include <cstdlib> #include <bitset> #include <sstream> #define MAX_ADDRESSES 1000
#include <fstream>
#include <string>
#include <cstdlib>
#include <bitset>
#include <sstream>
#define MAX_ADDRESSES 1000000
using namespace std;
int main(int argc, char **argv) {
//Declare variables
int szL1 = atoi(argv[2]);
int szL2 = atoi(argv[4]);
string type = argv[6]; //Determines the type of cache
//string fileN = argv[7];
int info, i = 1, j = 1; //info is what is read in, i & j are testing variables
string *rd_add = new string[MAX_ADDRESSES]; //set the max string length for what's read
string *wrt_add = new string[MAX_ADDRESSES]; //set the max string length for what's written
int total = 0; //total # of reads/writes
int rds = 0; //base # of reads
int wrt = 0; //base # of writes
int array_size = 1001024;
char *array = new char[array_size];
int position = 0;
ifstream fin("big_trace.txt");//open big trace file********
if (fin.is_open()){
cout << "File opened successfully" << endl;
while (!fin.eof() && position < array_size){
fin.get(array[position]);
position++;
}
array[position - 1] = '\0';
for (int x = 0; array[x] != '\0'; i++){
cout << array[i];
}
}
else{
cout << "File could not be opened" << endl;
}
//check for a power of 2 for szL1
while (1)
{
if (i == szL1)
break;
//Error Message
else if (i > szL1)
{
cout << "Error. sizeL1 must be a power of 2. Please try again." << endl << endl;
return 0;
}
i *= 2;
}
//cout << "size1 " << szL1 << endl;
//check for a power of 2 for szL2
while (1)
{
if (j == szL2)
break;
//Error
else if (j > szL2)
{
cout << "Error. sizeL2 must be a power of 2. Please try again." << endl << endl;
return 0;
}
j *= 2;
}
//cout << "size2 " << szL2 << endl;
//Check to see if szL2 is larger than szL1
if (szL2 <= szL1)
{
cout << "Error. sizeL2 must be larger than sizeL1. Please try again." << endl << endl;
return 0;
}
//Read file contents*****
while (cin >> info) //check this part
{
//If it is a 1, increment read files
if (info == 1)
{
cin >> rd_add[i++];
rds++;
}
else if (info == 0)
{
cin >> wrt_add[j++];
wrt++;
}
else
{
continue;
}
}
total = rds + wrt;
//Print the arguments read
cout << endl << "Input Parameters read:" << endl;
cout << "SizeL1: " << szL1 << endl;
cout << "SizeL2: " << szL2 << endl;
cout << "Type: " << type << endl;
//Print file stats
cout << endl << "Memory References Read from File" << endl;
cout << "Total: " << total << endl;
cout << rds << " Reads" << endl;
cout << wrt << " Writes" << endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义最大地址1000000
使用名称空间std;
int main(int argc,字符**argv){
//声明变量
int szL1=atoi(argv[2]);
int szL2=atoi(argv[4]);
string type=argv[6];//确定缓存的类型
//字符串fileN=argv[7];
int info,i=1,j=1;//info是读入的内容,i&j是测试变量
string*rd_add=new string[MAX_ADDRESSES];//设置读取内容的最大字符串长度
string*wrt\u add=new string[MAX\u ADDRESSES];//设置写入内容的最大字符串长度
int total=0;//读取/写入的总次数
int rds=0;//读取的基数
int wrt=0;//写操作的基数
int数组_size=1001024;
字符*数组=新字符[数组大小];
int位置=0;
ifstream fin(“big_trace.txt”);//打开大跟踪文件********
如果(fin.is_open()){
cout如果您只想获取向量中的十六进制值,并且您的文件如您所说,您可以按如下方式执行:
String hexValue, dummy;
Vector<String> hexValueVector;
ifstream fin("big_trace.txt");//open big trace file********
if (fin.is_open()){
cout << "File opened successfully" << endl;
while (!fin.eof()){
fin >> dummy >> hexValue;
hexValueVector.push_back(hexValue);
....//your remaining code
而不是创建字符串向量,而是创建myStructure向量:
Vector<myStructure> myStructureVector;
如果这解决了你的问题,请投票支持答案
关于Vector,它是一个STL容器,如果你想了解更多关于它的详细信息,请检查这很好,但我还需要1或0,知道如何在它们之间获得一些空间。你能解释一下发生了什么吗?我以前从未使用过Vector。
#include <vector>
struct myStructure{
String dummy;
String hexValue;
};
Vector<myStructure> myStructureVector;
myStructure myStructure;
if(fin.is_open()){
...
while (!fin.eof()){
fin >> myStructure.dummy >> myStructure.hexValue;
myStructureVector.push_back(myStructure);