ifstream initialization error- Error Reading Characters - c++

#include "Stdafx.h"
#include "FishTracker.h"
#include <string>
#include "Utils.h"
#include <fstream>
using namespace std;
using namespace cv;
int main()
{
std::string videopath;
videopath = "E:\\TUKLNUST\\fishdata2\\Damo\\AR2-6\\Tetraodontidae_Lagocephalus_sceleratus_AD\\00001\\";
ifstream str;
str.open((videopath + DATA_TXT).c_str());
if (str.is_open())
{
cout << "file is open.";
}
}
file is open but str is this.
+ str {_Filebuffer={_Set_eback=0xcccccccc <Error reading characters of string.> _Set_egptr=0xcccccccc <Error reading characters of string.> ...} } std::basic_ifstream<char,std::char_traits<char> >
Configs:
win32, Debug
Visual Studio 2013

#include "Stdafx.h"
#include "FishTracker.h"
#include <string>
#include "Utils.h"
#include <fstream>
using namespace std;
using namespace cv;
int main()
{
std::string videopath;
videopath = "E:\\TUKLNUST\\fishdata2\\Damo\\AR2-6\\Tetraodontidae_Lagocephalus_sceleratus_AD\\00001\\";
ifstream str;
str.open((videopath + "DATA_TXT").c_str());
if (str.is_open())
{
cout << "file is open.";
}
}

Related

How to connect two modules by systemC tlm_fifo<float>?

I am still a novice for writing systemC-TLM. Now, I want to connect two modules by tlm_fifoFF. I am searching examples for a long time on net. But no use. Please help to give some ideas or examples how to achieve this.
This is my architecture:
and,this is my error information:
this is "main.cpp"
#include <systemc.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "merlin2.h"
#include "tlm.h"
#include <tlm_utils/simple_target_socket.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/multi_passthrough_initiator_socket.h>
#include <tlm_utils/multi_passthrough_target_socket.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_channels/tlm_fifo/tlm_fifo.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_1_interfaces/tlm_fifo_ifs.h>
using namespace sc_core;
using namespace tlm;
using namespace tlm_utils;
using namespace std;
int sc_main(int argc, char** argv){
Merlin2 merlin_test("merlin2_JW");
sc_start();
cout << "end" << endl;
return 0;
}
this is "merlin2.cpp"
#include "merlin2.h"
Merlin2::Merlin2(const sc_module_name& name)
{
ProcessEngine PE_TEST("test_PE");
test_PE TP("PE_test");
tlm::tlm_fifo <float> FF ("fifo");
TP.out(FF);
PE_TEST.in(FF);
}
this is "merlin2.h"
#ifndef __MERLIN2_H__
#define __MERLIN2_H__
#include <fstream>
#include <string>
#include <systemc.h>
#include "pe.h"
#include "test_PE.h"
class Merlin2: public sc_module
{
public:
SC_HAS_PROCESS(Merlin2);
Merlin2(const sc_module_name& name);
};
#endif
this is "pe.cpp"
#include <systemc.h>
#include <iostream>
#include "pe.h"
using namespace std;
ProcessEngine::ProcessEngine(const sc_module_name& name):in("in")
{
cout << "before SC_THREAD(run)" << endl;
SC_THREAD(run);
cout << "after SC_THREAD(run)" << endl;
}
void ProcessEngine::run()
{
int N = in.nb_get();
cout << " N=" << N << endl;
wait(1,SC_NS);
}
this is "pe.h"
#ifndef __PE_H__
#define __PE_H__
#include <iostream>
#include <bitset>
#include "tlm.h"
#include <tlm_utils/simple_target_socket.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/multi_passthrough_initiator_socket.h>
#include <tlm_utils/multi_passthrough_target_socket.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_channels/tlm_fifo/tlm_fifo.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_1_interfaces/tlm_fifo_ifs.h>
using namespace sc_core;
using namespace tlm;
using namespace tlm_utils;
using namespace std;
class ProcessEngine: public sc_module
{
public:
tlm::tlm_fifo<float> in;
void run();
ProcessEngine(const sc_module_name& name);
SC_HAS_PROCESS(ProcessEngine);
};
#endif
this is "test_PE.cpp"
#include <systemc.h>
#include <iostream>
#include "test_PE.h"
using namespace std;
test_PE::test_PE(const sc_module_name& name):out("out")
{
cout << "before start_test " << endl;
SC_THREAD(start_test);
cout << "After start_test " << endl;
}
void test_PE::start_test()
{
float A=5;
in.nb_put(A);
cout << "A=" << A << endl;
wait(1,SC_NS);
}
this is "test_PE.h"
#ifndef __TESTPE_H__
#define __TESTPE_H__
#include <fstream>
#include <string>
#include <systemc.h>
#include "tlm.h"
#include <tlm_utils/simple_target_socket.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/multi_passthrough_initiator_socket.h>
#include <tlm_utils/multi_passthrough_target_socket.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_channels/tlm_fifo/tlm_fifo.h>
#include <tlm_core/tlm_1/tlm_req_rsp/tlm_1_interfaces/tlm_fifo_ifs.h>
using namespace sc_core;
using namespace tlm;
using namespace tlm_utils;
using namespace std;
class test_PE:public sc_module
{
public:
tlm::tlm_fifo<float> out;
test_PE(const sc_module_name& name);
SC_HAS_PROCESS(test_PE);
void start_test();
private:
float A;
};
#endif
You're mixing SystemC/TLM notions and especially ports and channels.
tlm_fifo is a channel. It can be used to connect two ports. It's the yellow part of your image.
However, you can't directly connect a channel to a module. Instead, you need to use ports in each module you want to connect the channel. A port is associated to an interface defining methods you can use. In your case, you use nb_get(...) and nb_put(...).
For tlm_fifo, you can use these ports:
sc_port< tlm_nonblocking_put_if<float> >
sc_port< tlm_nonblocking_get_if<float> >
Finally, you create instances of your modules in your Merlin2 constructor. Objects are not persisted and deleted at the end of the constructor. They should be member of the class. Here is a complete working example of TLM-1.0 using tlm_fifo with non blocking interface.
Note: You're mixing TLM-2.0 and TLM-1.0 headers. I removed useless headers.
main.cpp
#include <systemc>
#include "merlin2.h"
using namespace sc_core;
using namespace std;
int sc_main(int argc, char** argv) {
Merlin2 merlin_test("merlin2_JW");
sc_start();
cout << "end" << endl;
return 0;
}
merlin2.h
#ifndef __MERLIN2_H__
#define __MERLIN2_H__
#include <fstream>
#include <string>
#include <systemc>
#include "pe.h"
#include "test_PE.h"
class Merlin2: public sc_module
{
public:
Merlin2(const sc_module_name& name);
tlm::tlm_fifo <float> FF;
ProcessEngine PE_TEST;
test_PE TP;
};
#endif
merlin2.cpp
#include "merlin2.h"
Merlin2::Merlin2(const sc_module_name& name):
PE_TEST("test_PE"),
TP("PE_test"),
FF("fifo")
{
TP.out(FF);
PE_TEST.in(FF);
}
pe.h
#ifndef __PE_H__
#define __PE_H__
#include <fstream>
#include <string>
#include <systemc>
#include <tlm>
using namespace sc_core;
using namespace tlm;
using namespace std;
class ProcessEngine: public sc_module
{
public:
sc_port< tlm_nonblocking_get_if<float> > in;
void run();
ProcessEngine(const sc_module_name& name);
SC_HAS_PROCESS(ProcessEngine);
};
#endif
pe.cpp
#include <systemc.h>
#include "pe.h"
using namespace std;
ProcessEngine::ProcessEngine(const sc_module_name& name):
in("in")
{
SC_THREAD(run);
}
void ProcessEngine::run()
{
sc_core::wait(1, SC_NS);
float N = 0;
bool r = in->nb_get(N);
cout << " N=" << N << endl;
}
test_PE.cpp
#include <systemc>
#include "test_PE.h"
using namespace std;
test_PE::test_PE(const sc_module_name& name):
out("out")
{
SC_THREAD(start_test);
}
void test_PE::start_test()
{
float A=5;
out->nb_put(A);
cout << "A=" << A << endl;
sc_core::wait(1, SC_NS);
}
test_PE.h
#ifndef __TESTPE_H__
#define __TESTPE_H__
#include <fstream>
#include <string>
#include <systemc>
#include <tlm>
using namespace sc_core;
using namespace tlm;
using namespace std;
class test_PE:public sc_module
{
public:
sc_port< tlm_nonblocking_put_if<float> > out;
test_PE(const sc_module_name& name);
SC_HAS_PROCESS(test_PE);
void start_test();
private:
float A;
};
#endif

Error in file reading with ifstream

I'm having issue in the ifstream function, I have tried using the argv[1] as parameter but wont load the map, the map is located in the same folder of main code.
I'm stucked here and can not debug.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
int main (int argc, char *argv[]){
int h;
int w;
int var;
string inputLine;
ifstream f;
f.open("map.pgm",ios::in);
if (!f){
cout << "error" << endl;
exit(1);
}
I'm using Visual Studio 2017
Change this line:
if (!f){
by this:
if (!f.is_open()){
BTW you can check current directory path with GetModuleFileName

C++ | Problems with files

I'm pretty new to C++ and I wanted to start working with files so I ended up doing this :
#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <stdexcept>
#include <limits>
#include <Lmcons.h>
#include <fstream>
using namespace std;
void out(string x){x+="\n";cout<<x;}
void outn(){out("");}
void delay(int x){Sleep(x);}
void delayS(int x){Sleep(x*1000);}
void cs(){std::system("cls");}
void UserName(string *x){char username[UNLEN + 1];DWORD size = UNLEN + 1;GetUserName(username, &size);string transition(username);*x=transition;}
//use this synthax in main : char user[20];string username(user);UserName(&username);
using namespace std;
int main()
{
char user[20];string username(user);UserName(&username);
out(username);
delayS(2);
cs();
string beginning="C:\\Users\\" ;
string path;
string ending="\\Desktop\\";
string filename;
out("file name = ");
cin>>filename;
path+=beginning;
out(path);
delayS(2);
path+=username;
out(path);
delayS(2);
path+=ending;
out(path);
delayS(2);
path+=filename;
out(path);
delayS(2);
ofstream file;
try{
file.open(path ,ios::in);
if(!file.is_open()){throw 404;}
else{
file.open(path,ios::out|ios::app);
file<<"\n";
file<<"lol";}
}catch(int x){cout<<"Error "<<x<<" : file not found";}
return 0;
}
Which result in this error (line 59) :
" no matching function for call to 'std::basic_ofstream::open(std::string&, std::_Ios_Openmode)' "
image of error : https://imgur.com/YVBHDzq
May I have some help ?
EDIT: I'm using Codeblocks 16.01
In pre-C++11, you need to pass a const char* to ofstream::open() as the first parameter:
file.open( path.c_str(), ios::in );

VideoWriter Error in OpenCv

#include<sstream>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cassert>
#include <cmath>
#include <fstream>
#include <time.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/types_c.h"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/video/tracking.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/calib3d/calib3d.hpp"
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
VideoCapture cap("test2.mp4");
assert(cap.isOpened());
cap>>prev;
VideoWriter outputVideo;
if (!outputVideo.isOpened())outputVideo.open("compare.mp4" , CV_FOURCC('X','V','I','D'), 24,cvSize(cur.rows, cur.cols*2+10), true);
outputVideo<< prev;
while(true) {
stringstream ss;
string name = "cropped_";
string type = ".jpg";
ss<<name<<(chk)<<type;
string filename = ss.str();
ss.str("");
imwrite(filename, canvas);
outputVideo<< canvas;
prev = cur.clone();//cur.copyTo(prev);
prev_ref = cur_ref.clone();//cur.copyTo(prev);
cur_grey.copyTo(prev_grey);
cout << "Frame: " << k << "/" << max_frames << " - good optical flow: " << prev_corner2.size() << endl;
k++;
}
Question:It is a small description of my code.Now when I try to write my canvas(variable of type Mat in OpenCV) for creating a video.It's giving an error.Even if I try for .avi file or .mp4 file it doesn't write/create a video.Please help me out.It shows an error "[mp4 # 0xa0aac0] Tag XVID/0x44495658 incompatible with output codec id '13' error ".I am using ubuntu 14.04 with intel core processor-i5

c++ combine path of file as a string with file name

I want to read some input files in my c++ code and I want to define the path of input files as a string and then combine it with file names. How can I do this? (Input_path + filename.dat)
#include <filesystem> //C++ 17
#include <iostream>
namespace fs = std::filesystem;
using namespace std;
void main()
{
string dir("c:\\temp");
string fileName("my_file.txt");
fs::path fullPath = dir;
fullPath /= fileName;
cout << fullPath.c_str() << endl;
}
You would use something like:
string path ("yourFilePath");
string filename ("filename");
You could then open the file like this:
ifstream inputFileStream;
inputFileStream.open(path + fileName);
Depending on your requirements, you will have to decide whether to use formatted or unformatted input when reading. I would read this for more information regarding that.
Cocatenation referenced from: C++ string concatenation
Reading referenced from: C++ read and write with files
Try any of these codes:
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string filepath = "D:/location/";
filepath+= "filename.dat";
std::ifstream fp;
fp.open(filepath.c_str(),std::ios_base::binary);
....PROCESS THE FILE HERE
fp.close();
return 0;
}
or
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string filepath = "D:/location/";
std::ifstream fp;
fp.open((filepath+"filename.dat").c_str(),std::ios_base::binary);
...............
fp.close();
return 0;
}
or use std::string::append
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string filepath = "D:/location/";
std::ifstream fp;
fp.open((filepath.append("filename.dat")).c_str(),std::ios_base::binary);
fp.close();
return 0;
}