Opening a pdf and opening a specific page - c++

I am trying to open a pdf at a specific page when the user enters the correct number, I've been looking around but am lost with the windows.h not sure what to do next, here's my code:
// DungeonsAndDragons.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <iostream>
#include "windows.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
int featsSpells;
int select1;
int select2;
int select3;
string exit;
cout << "******Brad's DnD Feat/Spell Glossary*********" << endl;
cout << "Are you looking for Feats[#1] or Spells[#2]" << endl;
cin >> featsSpells;
if (featsSpells == 1){
ShellExecute(GetDesktopWindow(), "open", Argv[1],NULL,NULL,SHOWNORMAL);
}
system("pause");
return 0;
}
The code does not compile because I get a complaint about "const char *" is incompatible with parameter of type "LPCWSTR".

There are several things you should change.
The compiler / editor error is because you are mixing data types. You need to convert the ARGV into a LPCWSTR, because this is what ShellExecute expect.
To make sure that ShellExecute finds the .pdf you can specify the full path to the file, for example C:\temp\foo.pdf (if hardcoded into the source code you need to specify the backslash twice).
In order to tell the Acrobat reader (I think not all other alternative pdf viewer support this) on which page you want to start, you can append #page=123. Adobe documents the list of possible parameters in a PDF in a - you guessed it ;-) - pdf file

Related

How to disable input method in C++ console?

I'm making a text-based console game.
The Chinese input method is annoying as it blocks normal key input.
I tried to send NULL to ImmAssociateContext but it doesn't work.
#include<Windows.h>
#include <imm.h>
#include <atlstr.h>
#include<handleapi.h>
#include<iostream>
#pragma comment ( lib,"imm32.lib" )
using namespace std;
int main(int argc, char* argv[])
{
SetConsoleTitle(TEXT("test"));
char ch;
ImmAssociateContext(FindWindow(NULL, TEXT("test")), NULL);
cin >> ch;
cout << ch;
return 0;
}
Edit:
I don't need to input Chinese in this game.
About Chinese input:
When you input a letter in console, Chinese input methods would receive the input as pinyin, and turn it into Chinese characters.
(In the screenshot, aabbcc with dashline below is pinyin)
The same thing happened to Japanese input method.
This is not what I need. All I want is, when I press A, the console receives A.
About the code:
I'm using PDCurses in my project to draw a text-based gui and get key inputs.
Everything looks fine when the input method is turned off.
The code above shows ImmAssociateContext (google says it can turn off input method) doesn't work to me.

Providing a file path to an input-dependent program

First off, sorry if the title makes no sense. The nature of my question makes it very hard for me to phrase.
I am working on an assignment for my datastructures class and I am completely and totally brand new to c++ due to only having learned Java at my old school. The project is a weather logger that reads in data from a text file climatedata.txt. My teacher has provided us with a main function in the file (that we are NOT allowed to modify in any way) weatherlog.cpp which is below.
#include <iostream>
#include <fstream>
#include "datalogger.h"
using namespace std;
int main(int argc, char** argv) {
datalogger dl;
if (argc != 2) {
cout << "Usage: " << argv[0] << " <datafile>" << endl;
exit(0);
}
// Read the data
char* datafile = argv[1];
ifstream infile(datafile);
int timestamp;
double temperature;
double windspeed;
while (!infile.eof()) {
infile >> timestamp;
infile >> temperature;
infile >> windspeed;
if (!infile.eof()) {
dl.addData(timestamp, temperature, windspeed);
}
}
// Output the report
dl.printReport();
return(0);
}
Initially I was confused as to why the program would never fully execute until I figured out what argc is in the scope of a main function. It seems that he wants me to provide the text file name while compiling so that argc will be 2 instead of 1 (the value I saw when debugging) so that it can actually execute the rest of the program instead of immediately exiting.
My problem is I'm not sure how to provide the program with the text file location. I've looked all over the internet but since I'm not even sure at which stage to provide the file path I haven't had any success. Is that information supposed to be passed when compiling with g++? After successfully compiling when I'm trying to run the executable? What does the terminal command to do so look like?
So I understand that you need to provide a file name in argv (Comment below if I'm incorrect). argv is an array of arguments passed by the commandline, and argc is the amount of arguments passed (automatically set). To do that simply call the program in terminal like this: ./<progam> <file_name>
Example:
compile just as you would a hello world progam.
Call the program weatherlog climatedata.txt.
If your file has spaces in its name either remove them or do this enclose its name in quotes.
argc stores number of passed in parameters, while argv points to parameters.
if (argc != 2) means checking number of input parameters passed in via Console mode. The first parameter is always the program name. From the second parameter you can pass anything you want. char* datafile = argv[1]; means taking the second parameter as data filename.
In short, open Console mode (CMD on Windows, Terminal on Linux) and type something like: yourprogram C:\path\to\climatedata.txt.

windows, c++: send file to exe (c++ solution) and read data from sent file

My goal is to send an arbitrary text file to a exe which is a build of a c++ project. In the c++ project I would like to read the file which was sent to the exe. Hence, I think I need to pass the path of the sent file to the application (exe).
My c++ code [is working!]:
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::string readLineFromInput;
ifstream readFile("input.txt"); // This is explizit.
// What I need is a dependency of passed path.
getline(readFile, readLineFromInput);
ofstream newFile;
newFile.open("output.txt");
newFile << readLineFromInput << "\n";
newFile.close();
readFile.close();
}
My Windows configuration:
In the following path I created a shortcut to the exe (build of c++ project):
C:\Users{User}\AppData\Roaming\Microsoft\Windows\SendTo
Question:
I want to right click to an arbitrary text file and pass it (SendTo) to the exe. How can I pass the path of the sent file as an argument to the application such that the application can read the sent file?
When the path is passed as an argument the line of code should be like this, I guess:
ifstream readFile(argv[1]);
Much thanks!
David
Whether you use SendTo or OpenWith, the clicked filename will be passed to your executable as a command-line parameter. As such, the argv[] array will contain the filename (at argv[1], unless your SendTo shortcut specifies additional command-line parameters, in which case you would have to adjust the argv[] index accordingly).
I just did a test with SendTo and argv[1] works fine. Just make sure you check argc before attempting to open the filename, eg:
int _tmain(int argc, _TCHAR* argv[])
{
if (argc > 1)
{
std::string readLineFromInput;
std::ifstream readFile(argv[1]);
if (readFile)
std::getline(readFile, readLineFromInput);
std::ofstream newFile(_T("output.txt"));
if (newFile)
newFile << readLineFromInput << "\n";
}
}

Cannot open file with relative path? (C++ ifstream)

I know this seems like a simple question, but I tried everything I can think of to no avail to something that shouldn't have been a problem in the first place.
This is a small C++ program that opens a file. When I open it with its absolute filepath, it works fine. With a relative path, however, it stops working.
Here's the file path of the program and the files I'm trying to read:
C++ program: "/Users/Baggio/C++/Lab0/Lab0/Lab0/main.cpp"
Files: /Users/Baggio/C++/Lab0/Lab0/Lab0/result.txt, /Users/Baggio/C++/Lab0/Lab0/Lab0/dict.txt
Here's the code snippet:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[]) {
// string dict_filename = "/Users/Baggio/C++/Lab0/Lab0/Lab0/dict.txt";
// string result_filename = "/Users/Baggio/C++/Lab0/Lab0/Lab0/result.txt";
string dict_filename_string = "dict.txt";
string result_filename_string = "result.txt";
const char* dict_filename = dict_filename_string.c_str();
const char* result_filename = result_filename_string.c_str();
// open files
ifstream dict_file(dict_filename, ifstream::in);
ifstream result_file(result_filename, ifstream::in);
if (!dict_file || !result_file) {
cerr << "File could not be opened." << endl;
exit(1);
}
}
Result of execution
File could not be opened.
I'm sure I've done all the includes right, and the data types right for the ifstream constructor arguments. The only thing I can think of worth mentioning is the system I'm on: I'm on a Mac and I'm using XCode6 as my IDE.
Also, I've tried to move the files' location (results.txt and dict.txt) to these locations to no avail:
/Users/Baggio/C++/Lab0/Lab0/Lab0/
/Users/Baggio/C++/Lab0/Lab0/
/Users/Baggio/C++/Lab0/
/Users/Baggio/C++/
Thanks for your help guys!! Any suggestions or thoughts appreciated.
Print out your current working directory when you run the program:
char buffer[256];
char *val = getcwd(buffer, sizeof(buffer));
if (val) {
std::cout << buffer << std::endl;
}
This will tell you where you are running your program from and thus why the path doesn't match for relative paths. A relative path is relative to the current working directory, not to where your binary is located.
If you want to make the path relative to the location of the binary then you will have to do that yourself. Many programming languages offer this as an option, but it is not built-in to C++. You can do this by finding the executable using the argv[0] from main. Then you need to drop the file component of the executable path and replace it with the file name that you are interested in.
Since C++17, you can use std::filesystem::current_path() instead of getcwd.
std::cout << std::filesystem::current_path() << std::endl;

Unicode Windows console application (WxDev-C++/minGW 4.6.1)

I'm trying to make simple multilingual Windows console app just for educational purposes. I'm using c++ lahguage with WxDev-C++/minGW 4.6.1 and I know this kind of question was asked like million times. I'v searched possibly entire internet and seen probably all forums, but nothing really helps.
Here's the sample working code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/* English version of Hello world */
wchar_t EN_helloWorld[] = L"Hello world!";
wcout << EN_helloWorld << endl;
cout << "\nPress the enter key to continue...";
cin.get();
return 0;
}
It works perfectly until I try put in some really wide character like "Ahoj světe!". The roblem is in "ě" which is '011B' in hexadecimal unicode. Compiler gives me this error: "Illegal byte sequence."
Not working code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/* Czech version of Hello world */
wchar_t CS_helloWorld[] = L"Ahoj světe!"; /* error: Illegal byte sequence */
wcout << CS_helloWorld << endl;
cout << "\nPress the enter key to continue...";
cin.get();
return 0;
}
I heard about things like #define UNICODE/_UNICODE, -municode or downloading wrappers for older minGW. I tried them but it doesn't work. May be I don't know how to use them properly. Anyway I need some help. In Visual studio it's simple task.
Big thanks for any response.
Apparently, using the standard output streams for UTF-16 does not work in MinGW.
I found that I could either use Windows API, or use UTF-8. See this other answer for code samples.
Here is an answer, not sure this will work for minGW.
Also there are some details specific to minGW here