Listing the Files in a Directory in Visual C++ - visual-c++

I have tried to "simplify" a nice piece of example code, the hyperlink to the code is at the end of this message, to specify the directory string instead of passing it as a command line argument. The simplified code compiles and executes, but the filename and size are not what I expect: the file name appears to be a hex number, and the nFileSize.High is larger than the nFileSize.Low (the actual file sizes range from 0 to 100Mb). I think my type casting may have introduced errors. Any suggestions?
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <bitset>
#include <sstream>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#pragma comment(lib, "User32.lib")
using namespace std;
using namespace System; //set common language runtime support to /clr
int main()
{
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
//TCHAR szDir[MAX_PATH];
//size_t length_of_arg;
HANDLE hFind = INVALID_HANDLE_VALUE;
//DWORD dwError=0;
finstr = "C:\\Users\\MyName\\Documents\\Visual Studio 2010\\Projects\\Data Analysis\\Data Folder\\*";
//Just evaluate the first file before looping over all files
hFind = FindFirstFile((wchar_t*)(finstr.c_str()), &ffd);
wstring wsfname(ffd.cFileName);
string newtemp(wsfname.begin(), wsfname.end());
cout << "1st fname = " << ffd.cFileName << " newtemp = "<< newtemp << " nFSizeLo = "<< ffd.nFileSizeLow << " nFSizeHi = "<< ffd.nFileSizeHigh << "\n";
FindClose(hFind);
return 0;
}
Link to original example from Microsoft
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(d=printer,v=vs.85).aspx

Related

Question about this error: ‘bdget’ was not declared in this scope

trying to figure out why I’m getting this error about ’bdget’ (when on Linux Ubuntu, info below) and how to get rid of this error, maybe you could suggest some specific steps for me to try and/or ask me to provide you some additional info about the error, thank you for any help.
error: ‘bdget’ was not declared in this scope
b_dev = bdget(st.st_rdev);
The code (for a short sample/tester program put together quickly to demonstrate the problem here to you) that produces this error is below, here’s other info: When I comment out that ‘bdget’ line, the program (seen below) compiles, otherwise it doesn’t and produces the error. I understand from other stackoverflow.com questions that using bdget requires #include <linux/fs.h>, which I’ve done here as you can see in the code below.
I think this is my first question here at stackoverflow.com, haven’t yet learned what question-related actions I need to take here at stackoverflow.com about your answers such as accept, and the steps for taking those actions, so bear with me a bit.
Regards,
Yaman Aksu,PhD
The build used this g++ version (full path: /usr/bin/g++) :
g++ (Ubuntu 7.3.0-27ubuntu1-18.04) 7.3.0 <snip>
and was simple as follows:
g++ -Wall -03 tester.cpp -o tester
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <linux/fs.h>
#define FNABS_SIZE 1024
char prog[FNABS_SIZE];
using namespace std;
int main(int argc, char *argv[])
{
int rv=0;
struct dirent *de;
struct stat st;
struct block_device *b_dev;
char direc[FNABS_SIZE], fnabs[FNABS_SIZE], fnabs_bfile[FNABS_SIZE], hd_serial[NAME_MAX], hd_model[NAME_MAX];
int status;
unsigned int maj_rdev, min_rdev;
ios::sync_with_stdio();
strcpy(prog,argv[0]); argc--;
strcpy(direc,"/dev/disk/by-id");
for (DIR *p = opendir(direc); (de=readdir(p)); ) {
if (de->d_type==DT_LNK) {
sprintf(fnabs,"%s/%s",direc,de->d_name);
cout << "[glk] Now running 'stat' on this: " << fnabs << endl;
status = stat(fnabs, &st);
cout << "[glk] 'stat' results:" << endl;
cout << "[glk]\t return value:" << status << endl;
cout << "[glk]\t st.st_rdev:" << st.st_rdev << endl;
cout << "[glk]\t st.st_dev=" << st.st_dev << endl;
cout << "[glk]\t st.st_ino=" << st.st_ino << endl;
maj_rdev = major(st.st_rdev); min_rdev = minor(st.st_rdev);
printf("[glk]\t major:minor = %u:%u\n",maj_rdev, min_rdev);
if ((st.st_mode & S_IFMT) == S_IFBLK) {
printf("[glk]\t This is a 'block device' !!\n");
}
b_dev = bdget(st.st_rdev);
}
}
}

How to calculate the intersection of a line segment and circle with CGAL

I've been banging my head against a wall trying to understand how to use CGAL's Circular Kernel to calculate the intersection(s) between a line segment (Line_Arc_2) and a Circle (Circle_2). Unfortunately there isn't much in the way of example code for the Circular Kernel, and I'm not finding the reference manual much help.
Here is code that I thought would work, but right now it won't even compile (Mac OS 10.9 using the latest system compiler):
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2)>::type Res;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<Res> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}
I get an error on the result_of line: "error: no type named 'result_type' in...", and a second error that "no viable overloaded '='" is available for the intersection line.
Also, since this would probably be the follow up question once this is working: how do I actually get at the intersection points that are put in the vector? CGAL's documentation suggests to me "result" should contain pairs of a Circular_arc_point_2 and an unsigned int representing its multiplicity. Is this what I will actually get in this case? More generally, does anyone know a good tutorial for using the Circular Kernel and Spherical Kernel intersection routines?
Thanks!
So it seems that result_of doesn't work here, despite being suggested in the CGAL reference manual for the CircularKernel's intersection function.
Here is a different version that seems to work and can properly handle the output:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/iterator.h>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> IsectOutput;
using namespace std;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1.0,0.0), Pt2(0.0,1.0), Pt2(-1.0, 0.0));
LineArc2 l = LineArc2( Pt2(0.0,-2.0), Pt2(0.0,2.0) );
std::vector<IsectOutput> output;
typedef CGAL::Dispatch_output_iterator< CGAL::cpp11::tuple<IsectOutput>,
CGAL::cpp0x::tuple< std::back_insert_iterator<std::vector<IsectOutput> > > > Dispatcher;
Dispatcher disp = CGAL::dispatch_output<IsectOutput>( std::back_inserter(output) );
CGAL::intersection(l, c, disp);
cout << output.size() << endl;
for( const auto& v : output ){
cout << "Point: (" << CGAL::to_double( v.first.x() ) << ", " << CGAL::to_double( v.first.y() ) << "), Mult: "
<< v.second << std::endl;
}
return 0;
}
result_of is working but the operator you are asking for does not exist, you are missing the output iterator.
However, I agree the doc is misleading. I'll try to fix it.
The following code is working fine:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef boost::variant<std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> > InterRes;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2,std::back_insert_iterator<std::vector<InterRes> >)>::type Res;
int main(){
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<InterRes> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}

show first frame in the video using open cv?

I am working on stereo vision.Here i have two stereo videos.I wanted to do following things
1]extract the first frame from the video .
2] convert the frame to gray channel(so that i can apply SURF features)
3]display the first frame (getting error in the code given below)
4]store the video with small size(currently it is 1600*1200)
can anyone give suggestion how to do?
my code is given below(written in c++)
#include <stdio.h>
#include <iostream>
#include <vector>
#include <time.h>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include "opencv2\features2d\features2d.hpp"
#include "opencv2\nonfree\features2d.hpp"
#include "opencv2\nonfree\nonfree.hpp"
#include "opencv2\flann\flann.hpp"
#include "opencv2\contrib\contrib.hpp"
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\gpu\gpumat.hpp>
#pragma comment (lib, "opencv_core243d")
#pragma comment (lib, "opencv_highgui243d")
#pragma comment (lib, "opencv_imgproc243d")
#pragma comment (lib, "opencv_features2d243d")
#pragma comment (lib, "opencv_nonfree243d")
#pragma comment (lib, "opencv_flann243d")
#pragma comment (lib, "opencv_contrib243d")
#pragma comment (lib, "opencv_calib3d243d")
#pragma comment (lib, "opencv_gpu243d")
using namespace std;
using namespace cv;
int main(int argc, char**argv)
{
clock_t start_time=clock();
long lframecount=0;
//load the videos
VideoCapture capture1(argv[1]);
VideoCapture capture2(argv[2]);
cout << argv[1] << endl;
cout << argv[2] << endl;
if(!capture1.isOpened()||!capture2.isOpened())
{
cout<<"cant load stereo video";
return -1;
}
Mat frame1,frame2;
capture1>>frame1;
capture2>>frame2;
int num_rows=frame1.rows;
int num_cols=frame1.cols;
std::cout<<"number of rows and colums"<<num_rows<<":"<<num_cols<<std::endl;
int output_rows=num_rows/4;
int output_cols=num_cols/4;
long framecount1= capture1.get(CV_CAP_PROP_FRAME_COUNT);
long framecount2= capture2.get(CV_CAP_PROP_FRAME_COUNT);
VideoWriter write;
write.open("E:\\Vipil\\open_cv_learning\\video\\new22.avi",CV_FOURCC('D','I','V','X'), 30,cv::Size(output_rows,output_cols) ,true);
if (!write.isOpened())
{
std::cout << "cant open output video";
return -1;
}
namedWindow("depthmap",cv::WINDOW_NORMAL);
cv::Point tex(570, 50);
cv::Mat image1;
cv::Mat image2;
for(long i=1;i<6;i++)
{
lframecount++;
int number=lframecount;
string frame;
std::ostringstream convert;
convert<<number;
frame = convert.str();
capture1>> frame1;
capture2>> frame2;
cv::cvtColor(frame1,image1,CV_RGB2GRAY);
cv::cvtColor(frame2,image2,CV_RGB2GRAY);
imshow("image1",frame1);
//cout<<lframecount<<"\n";
}
You will need to use waitKey(30) or some delay to display image correctly.
Also to resize your frame you can simply use OpenCV resize() function.
Here is the reference
http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html

combining strings

I'm trying to combine strings to input text files. My code looks like this:
`#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int year;
string line;
string fileName;
for (int i=1880; i<2012; i++){
stringstream ss;
ss << year;
fileName = string("yob") + string(year) + string(".txt");
ifstream ifile(fileName.c_str());
getline(ifile,line);
cout << line << endl;
ifile.close();
}
}`
The text files look like "yob1880.txt" <-- that's the first text file and it goes all the way to "yob2011.txt". I want to input the text files one by one, but combining these three string types doesn't work it gives me an error saying invalid conversion from int to const char*.
Any thoughts on the problem? Thanks!
You should get it from the stringstream. You're almost there but this is what you should do instead:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int year;
string line;
string fileName;
for (int i=1880; i<2012; i++){
year = i; //I'm assuming this is what you meant to use "year" for
stringstream ss;
ss << year; //add int to stringstream
string yearString = ss.str(); //get string from stringstream
fileName = string("yob") + yearString + string(".txt");
ifstream ifile(fileName.c_str());
getline(ifile,line);
cout << line << endl;
ifile.close();
}
}

cout in Visual Studio 2010

When I attempt to compile the following
#include <iostream>
using namespace std;
#include "stdafx.h" // This was included by Visual Studio
int _tmain(int argc, _TCHAR* argv[]) // The name _tmain was generated by Visual Studio
{
int a = 1;
cout << a << "\n";
return 0;
}
I get a compiler message:
warning C4627: '#include <iostream>': skipped when looking for precompiled header use
Add directive to 'StdAfx.h' or rebuild precompiled header
Then I'm told that cout is undefined. (It doesn't help to write std::cout.)
I'm using a default Visual Studio projects. This is the first time I've used this. Suggestions appreciated.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a=10;
cout << a << "\n";
cin>>a;
return 0;
}
Slam dear! I have written the same code, but only changing the order of first three line. It gives result at console without any error or warning. Please check it.
Put your iostream include and the std namespace declaration after the stdafx.h include. The program will then compile and run.
As to why, my guess is that precompiled headers (enabled by default) rely on the exact sequence of #include directives. Putting iostream first means that the PCH for stdafx no longer matches the actual sequence of declarations known to the compiler at that point.

Resources