string - How to read a input file in an array in c++ -
string - How to read a input file in an array in c++ -
i'm trying store hexadecimal values form of array file alter numbers binary. far programme wrote collects lastly value. file looks this. note(a 1 or 0 before each hex value)
1 408ed4 0 10019d94 0 408ed8 1 10019d88 0 408edc 0 1001322 my code #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 type of cache //string filen = argv[7]; int info, = 1, j = 1; //info read in, & j testing variables string *rd_add = new string[max_addresses]; //set max string length what's read string *wrt_add = new string[max_addresses]; //set max string length 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'; (int x = 0; array[x] != '\0'; i++){ cout << array[i]; } } else{ cout << "file not opened" << endl; } //check powerfulness of 2 szl1 while (1) { if (i == szl1) break; //error message else if (i > szl1) { cout << "error. sizel1 must powerfulness of 2. please seek again." << endl << endl; homecoming 0; } *= 2; } //cout << "size1 " << szl1 << endl; //check powerfulness of 2 szl2 while (1) { if (j == szl2) break; //error else if (j > szl2) { cout << "error. sizel2 must powerfulness of 2. please seek again." << endl << endl; homecoming 0; } j *= 2; } //cout << "size2 " << szl2 << endl; //check see if szl2 larger szl1 if (szl2 <= szl1) { cout << "error. sizel2 must larger sizel1. please seek again." << endl << endl; homecoming 0; } //read file contents***** while (cin >> info) //check part { //if 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 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 file" << endl; cout << "total: " << total << endl; cout << rds << " reads" << endl; cout << wrt << " writes" << endl; homecoming 0; }
if want hexadecimal values in vector , file said can below:
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 do not forget include vector library.
#include <vector> hope help you.
edited:
if need dummy have set both values in structure:
struct mystructure{ string dummy; string hexvalue; }; and instead of creating vector of string create vector of mystructure:
vector<mystructure> mystructurevector; to fill vector have this:
mystructure mystructure; if(fin.is_open()){ ... while (!fin.eof()){ fin >> mystructure.dummy >> mystructure.hexvalue; mystructurevector.push_back(mystructure); if solves problem, please vote answer.
about vector, stl container, if want more details check http://www.cplusplus.com/reference/vector/vector/
c++ string file-io
Comments
Post a Comment