I am trying to read numerical data from a binary file containing 100 double values and store it in an array called myArray. When I print myArray nothing is being shown, as if myArray has never been filled. Any help is appreciated.
int main()
{
int file_len;
ifstream infile;
infile.open("mybinfile.bin", ios::binary | ios::in);
if (!infile.is_open())
{
cout << "Error in opening the file. " << endl;
}
else
{
const int file_len = 100;
std::vector<char> myVect;
myVect.resize(file_len);
int i = 0;
infile.read((char *) (&temp), sizeof(char));
myVect[i] = temp;
while(!infile.eof())
{
myVect[i] = temp;
i++;
infile.read((char *) (&temp), sizeof(char));
}
for (int i = 0; i < 100 ; i++)
{cout << i << ": " << myVect[i]<< endl;}
}
infile.close();
return 0;
}
Here
infile.read((char*)&myArray, file_len * sizeof(double));
You pass the pointer to the pointer to double. It should be
infile.read((char*)myArray, file_len * sizeof(double));
Makes me wonder why you didn't see a crash, since writing into random memory almost never turns out well.
Here is one example of brute-force serialization and de-serialization. Note that serialization in general is tricky, due to e.g. endianness, different floating-point formats, conversion between decimal text representations and binary and whatnot.
#include <fstream>
using std::string;
using std::ofstream;
using std::ifstream;
using std::cout;
string fname("/tmp/doubles.bin");
void write_doubles()
{
ofstream of;
of.open(fname.c_str(), std::ios::binary | std::ios::out);
double arr[100];
for (int i = 0; i < 100; i++)
{
arr[i] = static_cast<double>(i+100);
}
of.write((char*)arr, 100*sizeof(double));
}
void read_doubles()
{
ifstream ifs;
ifs.open(fname.c_str(), std::ios::binary | std::ios::in);
double arr[100];
ifs.read((char*)arr, 100*sizeof(double));
for (int i = 0; i < 100; i++)
{
cout << "Arr[i]: " << arr[i] << ", ";
}
cout << '\n';
}
int main()
{
write_doubles();
read_doubles();
return 0;
}
Related
I have the following code
if (StartMonitor) //StartMonitor
{
TotalVoltage5s += CalculatePower();
//TotalVoltage5s += 20;
xx123++;
CString sad;
m_power_edit.GetWindowText(sad);
int num1 = _ttoi(sad);
if(xx123 >= num1){
if(TotalTime > 19){
Power_Log_Chart.ClearChart();
TotalTime = 0;
}
CTime tm;
tm=CTime::GetCurrentTime();
CString str=tm.Format("%X");
ostringstream os;
ostringstream os1;
float ds= (float)atof((char *)(LPTSTR)(LPCTSTR)sad);
os << TotalVoltage5s/ds;
os1 << m_frequency;
m_power_list.InsertColumn(Current_columns,"",LVCFMT_CENTER,80,0);
m_power_list.SetItemText(0, Current_columns, str);
m_power_list.SetItemText(1, Current_columns, os.str().c_str());
m_power_list.SetItemText(2, Current_columns, os1.str().c_str());
m_power_list.SetItemState(Current_columns, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
m_power_list.EnsureVisible(Current_columns, FALSE);
Power_Log_Chart.SetXYValue(TotalTime, TotalVoltage5s/ds, TotalTime, 0);
TotalTime++;
TotalVoltage5s = 0;
m_frequency = 0;
xx123 = 0;
}
}
Power_Log_Chart.Invalidate();
}
CDialog::OnTimer(nIDEvent);
}
What it simply does, it takes the output of the function CalculatePower and it plots and displays its results in real time every 5 seconds, and it does the same thing for frequency and time.
I need a method to save the data in a text file and this is what i tried to do but i am not sure if it is going to work or not.
SavePowerData[32780] =TotalVoltage5s;
SaveTimeData[32780] = TotalTime;
ofstream myfile;
myfile.open ("Power Data.txt");
for(int x=0; x<=TotalVoltage5s; x++)
{
myfile << SavePowerData[x];
myfile << SaveTimeData[x];
}
myfile.close();
#include <fstream>
void append_file(std::string param1,std::string param2, std::string param3){
std::ofstream outfile;
outfile.open("out.txt", std::ios_base::app);
outfile << param1 << " " << param2<< " " << param3;
}
You can try to encapsulate your method like this and append the values you want to the file each time before putting them to your plot. You can also use a separator just after the end of each 3 params so it will be easier to read from afterwards.
Thank you Yigit, That is what i did and it worked,
for (int q = 0; q<=TotalTime; q++)
{
Time_[q] = TotalTime;
voltage[q] = TotalVoltage5s;
Frequency[q] = m_frequency;}
std::ofstream outfile;
outfile.open("Power Data.csv", std::ios_base::app);
outfile<<"Total Time"<<" "<<"Total Voltage 5 sec"<<" "<<"Frequency"<<endl;
for(int k = 0; k<TotalTime; k++){outfile<<voltage[k]<<", "<<Time_[k]<<", "<<Frequency[k]<<endl;}
outfile.close();
I am reading a text file which contains integers separated by a new line. Like this.
5006179359870233335
13649319959095080120
17557656355642819359
15239379993672357891
3900144417965865322
12715826487550005702
From this file, I want to access each integer in a loop and compare it with another, in order to match those two. In function File_read() I can print the integers. But what I want is to get it integer by integer outside the function. For example in main method, if there is a integer called x, I want to check whether x equals one of the integers in my text file.
string File_read() {
std::ifstream my_file("H:\\Sanduni_projects\\testing\\test.txt",
std::ifstream::binary);
if (my_file) {
string line;
for (int i = 0; i < 25; i++){
getline(my_file, line);
//cout << line << endl;
return line;
}
if (my_file)
std::cout << "all characters read successfully."<<endl;
my_file.close();
}
return 0;
}
Never return unconditionally inside a loop.
You are returning unconditionally from inside the loop. This causes the caller to exit the loop and return from the function during the first iteration.
for (int i = 0; i < 25; i++){
getline(my_file, line);
return line; // <-- Return from function (rest of iterations unreachable). Bad.
}
No need to reinvent stuff
Use the standard library to read the numbers, e.g., into a container std::vector.
std::vector<unsigned long long> v{std::istream_iterator<unsigned long long>{my_file},
std::istream_iterator<unsigned long long>{}};
Notice the value type of unsigned long long that is needed to fit the large numbers (you're pushing ~64 bits here).
Find a match
Use, e.g., std::find to find a possible match among the parsed numbers.
auto key = 15239379993672357891ull;
if (auto it = std::find(std::begin(v), std::end(v), key); it != std::end(v)) {
std::cout << "Key found at line " << std::distance(std::begin(v), it) + 1 << std::endl;
}
Here, I'm using a C++1z if(init; condition) statement to limit the scope of the iterator it to inside the if statement. It's optional of course.
Live example
You are, currently, just returning the first number (as a std::string and not a number). If you remove the return statement in your loop you can, of course, print each of them. Here is a slightly modified version of your File_read function that will return a std::vector<unsigned long long> that contains all the numbers. Then you can use this vector in, e.g., your main function to do your processing.
std::vector<unsigned long long> File_read()
{
std::vector<unsigned long long> numbers;
std::ifstream my_file("H:\\Sanduni_projects\\testing\\test.txt"); // Text files are not 'binany', i.e., removed std::ifstream::binary
if (my_file)
{
std::string line;
for (int i = 0; i < 25; i++)
{
std::getline(my_file, line);
numbers.push_back(std::stoull(line));
}
if (my_file)
{
std::cout << "all characters read successfully." << std::endl;
}
// my_file.close(); // Do not do this manually
}
return numbers;
}
Usage example:
int main()
{
unsigned long long x = /* some number */;
// Read all the numbers
std::vector<unsigned long long> vl = File_read();
// Run through all the numbers
for (unsigned long long y : vl)
{
// Check if any of the numbers are equal to x
if (x == y)
{
// There is a match...
// Do stuff
}
}
}
Update
The numbers cannot be held by in a long, however unsigned long long is sufficient.
std::vector<long> File_read(){
vector<long> numbers;
ifstream my_file("H:\\Sanduni_projects\\testing\\test.txt",
std::ifstream::binary);
if (my_file) {
string line;
for (int i = 0; i < frames_sec; i++){
getline(my_file, line);
numbers.push_back(std::stol(line));
}
if (my_file)
std::cout << "all characters read successfully." << endl;
else
std::cout << "error: only " << my_file.gcount() << " could be read" << endl;
my_file.close();
}
else{
cout << "File can not be opened" << endl;
}
return numbers;
}
Although the someone gives the answers that works correctly, I want to share my code.
#include <memory>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define MAX_SIZE 4096
class FileRead
{
public:
FileRead(string path) :_file(path)
{
Reset();
}
void Reset()
{
memset(_buff, 0, MAX_SIZE);
}
string ReadLine()
{
if (!_file.is_open())
{
cout << "error open file" << endl;
return "";
}
if (!_file.eof())
{
Reset();
_file.getline(_buff,MAX_SIZE);
return string(_buff);
}
else
{
cout << "read file finished." << endl;
return "";
}
}
private:
ifstream _file;
string _line;
char _buff[MAX_SIZE];
};
int _tmain(int argc, _TCHAR* argv[])
{
FileRead fr("H:\\Sanduni_projects\\testing\\test.txt");
string line;
while (!(line = fr.ReadLine()).empty())
{
//do some compare..
}
return 0;
}
The other answers are correct about how return works, but there is something that acts how you thought return acted.
using string_coro = boost::coroutines::asymmetric_coroutine<std::string>
void File_read(string_coro::push_type & yield) {
std::ifstream my_file("H:\\Sanduni_projects\\testing\\test.txt", std::ifstream::binary);
if (my_file) {
string line;
for (int i = 0; i < 25; i++){
getline(my_file, line);
yield (line);
}
if (my_file)
std::cout << "all characters read successfully." << std::endl;
my_file.close();
}
}
Which is used like this
string_coro::pull_type(File_read) strings;
for (const std::string & s : strings)
std::cout << s << endl;
So, I'm trying bring over some code to a Qt project I'm working on. The Motion class imports some control points from .txt file into the public member variable ctrlPos using fstream. When I use readCtrlPositions and then try to access ctrlPos with writePositions, for example, I get the error "vector subscript out of range".
There is a lot more code, but hopefully this should be sufficient to answer my question. I'm also a bit of a novice, so with any luck it's not something too stupid.
Motion class header:
#ifndef MOTION_H
#define MOTION_H
#include <vector>
#include "DualQuaternion.h"
class Motion
{
public:
virtual ~Motion();
virtual void readCtrlPositions(char*, char*);
virtual void writePositions(char*);
virtual void drawCtrlPositions();
virtual void set(int, vector<DualQuaternion>);
virtual pair<int, vector<DualQuaternion>> get();
public:
vector<DualQuaternion> ctrlPos, c;
int numberOfPositions;
};
#endif
Motion class:
#include <stdlib.h>
#include <GL\glut.h>
#include "motion.h"
#include "Quaternion.h"
#include "hMatrix.h"
#include "hPoint.h"
using namespace std;
void Motion::readCtrlPositions(char *fileNameArg, char *t)
{
ifstream inFile(fileNameArg, ios::in);
if (!inFile)
{
cerr<<"File" << fileNameArg << "could not be opened" << endl;
exit(1);
}
int i;
inFile >> numberOfPositions;
Quaternion *RotationQuaternion = new Quaternion[numberOfPositions];
for (i = 0; i<numberOfPositions; i++)
inFile >> RotationQuaternion[i];
if (t == "v")
{
Vector *TranslationVector = new Vector[numberOfPositions];
for (i = 0; i<numberOfPositions; i++)
inFile >> TranslationVector[i];
ctrlPos.clear();
for (i = 0; i<numberOfPositions; i++)
{
DualQuaternion dQ(RotationQuaternion[i], TranslationVector[i]);
ctrlPos.push_back(dQ);
cout << "first position from input: " << ctrlPos[i] << endl;
}
delete[] TranslationVector;
}
else if (t == "q")
{
Quaternion *TranslationQuaternion = new Quaternion[numberOfPositions];
for (i = 0; i<numberOfPositions; i++)
inFile >> TranslationQuaternion[i];
ctrlPos.clear();
for (i = 0; i<numberOfPositions; i++)
{
DualQuaternion dQ(RotationQuaternion[i], TranslationQuaternion[i]);
ctrlPos.push_back(dQ);
cout << "first position from input: " << ctrlPos[i] << endl;
}
delete[] TranslationQuaternion;
}
delete[] RotationQuaternion;
}
void Motion::writePositions(char *fileNameArg)
{
ofstream outFile(fileNameArg, ios::out);
if (!outFile)
{
cerr<<"File" << fileNameArg << "could not be opened for writing" << endl;
exit(1);
}
int i;
outFile << numberOfPositions << endl << endl;
for (i = 0; i<numberOfPositions; i++)
outFile << ctrlPos[i].GetReal();
outFile << endl;
for (i = 0; i<numberOfPositions; i++)
outFile << ctrlPos[i].GetDual();
}
void Motion::set(int n, vector<DualQuaternion> p)
{
int i;
numberOfPositions = n;
ctrlPos.clear();
for (i = 0; i<numberOfPositions; i++)
ctrlPos.push_back(p[i]);
}
pair<int, vector<DualQuaternion>> Motion::get()
{
return make_pair(numberOfPositions, ctrlPos);
}
void Motion::drawCtrlPositions()
{
vector <hMatrix> homogeneousMatricesForCtrlPositions;
for (int i=0; i<numberOfPositions; i++)
{
homogeneousMatricesForCtrlPositions.push_back(ctrlPos[i].dualQuaternionToHomogeneousMatrix().transpose());
double MatrixforOpenGLStack[16];
for (int i1=0; i1<4; i1++)
for (int i2=0; i2<4; i2++)
MatrixforOpenGLStack[4*i1+i2] = homogeneousMatricesForCtrlPositions.at(i).m[i1][i2];
::glPushMatrix();
::glMultMatrixd(MatrixforOpenGLStack);
glutSolidTeapot(0.15);
::glPopMatrix();
}
}
Motion::~Motion()
{
}
Sample code where error occurs in Qt program:
static Curve m;
m.readCtrlPositions("input.txt", "v");
m.writePositions("output.txt"); //<--vector subscript out of range
m.readCtrlPositions("output.txt", "q");
ctrlPos = m.get().second;
numberOfPositions = m.get().first;
In readCtrlPositions, t is a char*, so nor t=="v", nor t=="q" will be evaluated to true (it would return true if the two pointers were having the same address). So your function will set numberOfPositions to a non zero value but will never fill ctrlPos vector with any value.
Later, you'll try to access ctrlPos elements from 0 to numberOfPositions (not zero), while ctrlPos vector is empty. That's why you are reported to access the vector out of its range!
Replace char* by std::string is an easy way to fix the problem. If you need to keep the parameter as a char*, then use strcmp to compare string values rather than pointers.
I would also strongly recommend that you remove your numberOfPositions attribute and simply use ctrlPos.size() instead. It would have prevented a crash in this case by guaranteeing your class attributes integrity.
I tried to make a program that loads chunks of a large (We're speaking of a few MBs) of file, and searches for a value, and prints its address and value, except my program every few times throws a !myfile , doesn't give the value except a weird symbol (Although I've used 'hex' in cout), the addresses seem to loop sorta, and it doesn't seem to find all the values at all. I've tried for a long time and I gave up, so I'm asking experiences coders out there to find the issue.
I should note that I'm trying to find a 32 bit value in this file, but all I could make was a program that checks bytes, i'd require assistance for that too.
#include <iostream>
#include <fstream>
#include <climits>
#include <sstream>
#include <windows.h>
#include <math.h>
using namespace std;
int get_file_size(std::string filename) // path to file
{
FILE *p_file = NULL;
p_file = fopen(filename.c_str(),"rb");
fseek(p_file,0,SEEK_END);
int size = ftell(p_file);
fclose(p_file);
return size;
}
int main( void )
{
ifstream myfile;
myfile.open( "file.bin", ios::binary | ios::in );
char addr_start = 0,
addr_end = 0,
temp2 = 0x40000;
bool found = false;
cout << "\nEnter address start (Little endian, hex): ";
cin >> hex >> addr_start;
cout << "\nEnter address end (Little endian, hex): ";
cin >> hex >> addr_end;
unsigned long int fsize = get_file_size("file.bin");
char buffer[100];
for(int counter = fsize; counter != 0; counter--)
{
myfile.read(buffer,100);
if(!myfile)
{
cout << "\nAn error has occurred. Bytes read: " << myfile.gcount();
myfile.clear();
}
for(int x = 0; x < 100 - 1 ; x++)
{
if(buffer[x] >= addr_start && buffer[x] <= addr_end)
cout << "Addr: " << (fsize - counter * x) << " Value: " << hex << buffer[x] << endl;
}
}
myfile.close();
system("PAUSE"); //Don't worry about its inefficiency
}
A simple program to search for a 32-bit integer in a binary file:
int main(void)
{
ifstream data_file("my_file.bin", ios::binary);
if (!data_file)
{
cerr << "Error opening my_file.bin.\n";
EXIT_FAILURE;
}
const uint32_t search_key = 0x12345678U;
uint32_t value;
while (data_file.read((char *) &value, sizeof(value))
{
if (value == search_key)
{
cout << "Found value.\n";
break;
}
}
return EXIT_SUCCESS;
}
You could augment the performance by reading into a buffer and searching the buffer.
//...
const unsigned int BUFFER_SIZE = 1024;
static uint32_t buffer[BUFFER_SIZE];
while (data_file.read((char *)&(buffer[0]), sizeof(buffer) / sizeof(uint32_t))
{
int bytes_read = data_file.gcount();
if (bytes_read > 0)
{
values_read = ((unsigned int) bytes_read) / sizeof(uint32_t);
for (unsigned int index = 0U; index < values_read; ++index)
{
if (buffer[index] == search_key)
{
cout << "Value found.\n";
break;
}
}
}
}
With the above code, when the read fails, the number of bytes should be checked, and if any bytes were read, the buffer then searched.
i have a problem with write in file and read it back.when i open file in append mode.
and when i change the cursor of file it just mix up and dont work.
how can i write in file in hash position and read it back with hash with Order(1)?
thx for helping by the way.
struct student{
int id;
char name[20];
}st;
int hashing(char word[20])
{
int hash;
int sum=0;
int z;
sum += (int)word[0];
sum += (int)word[1];
z=(sum)%1000;
hash=z*sizeof(student);
return hash;
}
void main()
{
fstream op("d://ttest.txt",ios::app | ios::binary);
if(!op)
{
cout<<endl<<"cant open file :D in write !!!!!!:"<<endl;
getch();
}
for (int i = 0; i < 3; i++)
{
cin>>st.id>>st.name;
op.seekp(hashing(st.name),ios::beg);
op.write((char *)&st,sizeof(student));
}
op.close();
cout<<endl<<endl<<endl;
fstream op2("d://ttest.txt",ios::in | ios::binary);
if(!op2)
{
cout<<endl<<"cant open file :D in write !!!!!!:"<<endl;
getch();
}
char temp[20];
for (int i = 0; i < 3; i++)
{
st.id=0;
cin>>temp;
op2.seekp(hashing(temp),ios::beg);
op2.read((char *)&st,sizeof(student));
cout<<st.id<<" "<<st.name<<endl;
//op.seekp(hashing(st.name),ios::beg);
}
op2.close();
getch();
}
Here is an example of how you might detect if the database file is initialized and if not, do the initialization:
struct student
{
int id;
char name[20];
};
int main()
{
const std::string filename = "database.txt";
std::fstream fs;
// try to open the file
fs.open(filename, std::ios::in|std::ios::out|std::ios::binary);
if(!fs)
{
// Assume it needs initializing - create new file
std::cout << "Initializing database: " << filename << '\n';
student s;
s.id = -1; // empty
s.name[0] = '\0';
fs.clear();
fs.open(filename, std::ios::app|std::ios::binary);
if(!fs)
{
// can't recover from this
std::cout << "ERROR: initializing database: " << filename << '\n';
return 1;
}
// write 1000 empty student records
for(unsigned i = 0; i < 1000; ++i)
fs.write((char*) &s, sizeof(s));
fs.close();
// try again
fs.open(filename, std::ios::in|std::ios::out|std::ios::binary);
}
if(!fs) // still not working?
{
// not just an initialization issue, must abort
std::cout << "ERROR: opening database: " << filename << '\n';
return 1;
}
// Database open and ready for business ...
}