How would one go about combining these two programs into one that can...
Sep 2, 2014 at 10:51pm UTC
How would one go about combining these two programs into one that can open the .wav file in windows media player b entering the selected file's name?
Program 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <windows.h>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
string file;
cin >> file;
stringstream str;
str<<"/C://Users//My/Desktop//C++//" <<file<<".wav/" ;
string filepath=str.str();
cout<<filepath;
ifstream ipf("/C://Users/My//Desktop//C++//.wav/" );
if (ipf)
{cout << "hi" ;}
}
Program 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#define WIN32_LEAN_AND_MEAN // prevent windows.h from including "kitchen sink"
#include <windows.h>
#include <iostream>
#include <string>
int main(int argc, char **argv)
{
std::string file;
std::cout << "Please enter your files name\nEX:LOOKING LIKE THIS.wav" ;
std::cin >> file;
argv[0] = "\C:\\Documents and Settings\\Jason\\My Documents\\Jason\My Music\\" << file << ".wav\"" ;
//why are you changing the first argument (path to current file)
LPCSTR Application = "C:\\Program Files\\Windows Media Player\\wmplayer.exe" ;
// Media file extension must be provided
// Paths are quoted
char Command[1024] = "\"C:\\Program Files\\Windows Media Player\\wmplayer.exe\""
"\"C:\\Documents and Settings\\Jason\\My Documents\\Jason\My Music << file << " .wav\"" ;
//Above this is what needs input.
STARTUPINFOA si = {0}; // zero struct
si.cb = sizeof (si);
PROCESS_INFORMATION pi = {0}; // zero struct
if (CreateProcessA(Application, Command, NULL, NULL, FALSE, NULL, 0, NULL, &si, &pi))
{
std::cout << "Song playing\n" ;
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitCode = 0;
GetExitCodeProcess(pi.hProcess, &exitCode);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
DWORD errCode = GetLastError();
std::cerr << "song playing failed\nError code: " << errCode << "\n" ;
}
}
Plz help!
Last edited on Sep 3, 2014 at 9:44pm UTC
Sep 3, 2014 at 12:55am UTC
I'm not a windows guy, but this looks suspicious to me:
1 2
char Command[1024] = "\"C:\\Program Files\\Windows Media Player\\wmplayer.exe\""
"\"C:\Documents and Settings\Jason\My Documents\Jason\My Music\\LOOKING LIKE THIS.wav\"" ;
This actually creates one long string whose contents are
"C:\Program Files\Windows Media Player\wmplayer.exe""C:Documents and SettingsJasonMy DocumentsJasonMy Music\LOOKING LIKE THIS.wav"
- In C++ two back-to-back string literals like
"hello " "world"
are the same as
"hello world"
- You have single backslashes near the end. E.g.
"C:\Documents"
is the same as
"C:Documents"
- You probably don't want the string to contain quotes in the first place. Those are usually present so a command line parser (like cmd.exe) knows that a single argument contains spaces. What actually gets passed to the program is probably the unquoted string.
Sep 3, 2014 at 12:59am UTC
that already opens the file in windows media player but I cannot enter the file's directory I want.
I have turned it into this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#define WIN32_LEAN_AND_MEAN // prevent windows.h from including "kitchen sink"
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
int main( int argc, char **argv )
{
string file;
cout << "Please enter your files name\nEX:LOOKING LIKE THIS.wav" ;
cin >> file;
argv[0]="\C:\\Documents and Settings\\Jason\\My Documents\\Jason\My Music\\" <<file<<".wav\"" ;
LPCSTR Application = "C:\\Program Files\\Windows Media Player\\wmplayer.exe" ;
// Media file extension must be provided
// Paths are quoted
char Command[1024] = "\"C:\\Program Files\\Windows Media Player\\wmplayer.exe\""
"\"C:\\Documents and Settings\\Jason\\My Documents\\Jason\My Music\\" <<file<<".wav\"" ;
//Above this is what needs input.
STARTUPINFOA si = {0}; // zero struct
si.cb = sizeof (si);
PROCESS_INFORMATION pi = {0}; // zero struct
if ( CreateProcessA(
Application,
Command,
NULL,
NULL,
FALSE,
NULL,
0,
NULL,
&si,
&pi) )
{cout << "Song playing\n" ;
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitCode = 0;
GetExitCodeProcess(pi.hProcess, &exitCode);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);}else
{DWORD errCode = GetLastError();
cerr << "song playing failed\nError code: " << errCode << "\n" ;}}
I think this will work but I haven't tested it
Last edited on Sep 3, 2014 at 1:13am UTC
Sep 3, 2014 at 1:32am UTC
I think this will work but I haven't tested it
You should probably test it before asking for help it might or might not work :P
Also, I don't mean to be rude or anything but your indentation makes me want to carve my eyes out. Isn't this easier to read?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#define WIN32_LEAN_AND_MEAN // prevent windows.h from including "kitchen sink"
#include <windows.h>
#include <iostream>
#include <string>
int main(int argc, char **argv)
{
std::string file;
std::cout << "Please enter your files name\nEX:LOOKING LIKE THIS.wav" ;
std::cin >> file;
argv[0] = "\C:\\Documents and Settings\\Jason\\My Documents\\Jason\My Music\\" << file << ".wav\"" ; //why are you changing the first argument (path to current file)
LPCSTR Application = "C:\\Program Files\\Windows Media Player\\wmplayer.exe" ;
// Media file extension must be provided
// Paths are quoted
char Command[1024] = "\"C:\\Program Files\\Windows Media Player\\wmplayer.exe\""
"\"C:\\Documents and Settings\\Jason\\My Documents\\Jason\My Music << file << " .wav\"" ;
//Above this is what needs input.
STARTUPINFOA si = {0}; // zero struct
si.cb = sizeof (si);
PROCESS_INFORMATION pi = {0}; // zero struct
if (CreateProcessA(Application, Command, NULL, NULL, FALSE, NULL, 0, NULL, &si, &pi))
{
std::cout << "Song playing\n" ;
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitCode = 0;
GetExitCodeProcess(pi.hProcess, &exitCode);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
DWORD errCode = GetLastError();
std::cerr << "song playing failed\nError code: " << errCode << "\n" ;
}
}
Sep 3, 2014 at 7:18pm UTC
yea... I got that program off of a friend with a bad habit of not indenting. You see how mine is indented.
Last edited on Sep 3, 2014 at 7:19pm UTC
Sep 3, 2014 at 9:45pm UTC
The string "file" is supposed to be the file name.
Last edited on Sep 5, 2014 at 7:27pm UTC
Topic archived. No new replies allowed.