如何在c++中读取数组中的输入文件

How to read a input file in an array in c++

本文关键字:输入 文件 数组 读取 c++      更新时间:2023-10-16

我试图将十六进制值存储到文件中的某种形式的数组中,然后将这些数字更改为二进制。到目前为止,我编写的程序只收集最后一个值。文件看起来像这样。注意(每个十六进制值前面有一个1或0)

1   408ed4
0   10019d94
0   408ed8
1   10019d88
0   408edc
0   1001322

我的代码是#include

#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] = '';
        for (int x = 0; array[x] != ''; 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;
}

如果您只想获得矢量中的十六进制值,并且您的文件如您所说,您可以按如下方式执行:

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 

不要忘记包含Vector库。

#include <vector>

希望这对你有帮助。

编辑:

如果你也需要假人,你只需要把两个值都放在一个结构中:

struct myStructure{
String dummy;     
String hexValue;
};

而不是创建字符串的向量,而是创建myStructure:的向量

Vector<myStructure> myStructureVector;

要填充你的矢量,你只需要这样做:

myStructure myStructure; 
if(fin.is_open()){
  ...
while (!fin.eof()){
fin >> myStructure.dummy >> myStructure.hexValue;
myStructureVector.push_back(myStructure);  

如果这解决了你的问题,请投票给答案。

关于Vector,它是一个STL容器,如果你想了解更多关于它的详细信息,请检查http://www.cplusplus.com/reference/vector/vector/