-
Re: Can someone please run this in XP
Sorry to bother again but is this what u mean with hardwire the file names.....
Code:
#include <windows.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;
bool isPalindrome(const std::string& s)
{
std::string sReverse = s;
std::reverse(sReverse.begin(), sReverse.end());
return s == sReverse; // return true if the reverse is the same as non-reverse
}
//MAIN FUNCTION
int main(int argc, char *argv[]){
HANDLE readFile, writeFile;
HANDLE readFileMap, writeFileMap;
PVOID pvreadFile, pvwriteFile;
DWORD readFileSize;
string word = "";
list<string> words;
//Test Files -------------------------------------------------------------
char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
argv[1] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
//--------------------------------------------------------------------------
//VERIFYING ARGUMENTS
//LPCTSTR *myStr1 = (LPCTSTR *)argv[1];
if(argc>1)
{
readFile = CreateFile(inFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO CHECK IF THE READ FILE IS NOT VALID
if(readFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSAGE
std::cout << "Read file could not be opened." << std::endl;
return(FALSE);
}
readFileMap = CreateFileMapping(readFile, NULL, PAGE_READONLY, 0, 0, NULL);
//IF STATEMENT TO SEE IF THE READFILEMAP IS NULL
if(readFileMap == NULL)
{
//DISPLAY ERROR MESSAGE
std::cout << "Read file map could not be opened." << std::endl;
CloseHandle(readFile);
return(FALSE);
}
pvreadFile = MapViewOfFile(readFileMap, FILE_MAP_READ, 0, 0, 0);
//IF STATEMENT TO DETERMINE IF PVREADFILE IS NULL
if(pvreadFile == NULL)
{
//DISPLAY ERROR MESSAGE
std::cout << "Could not map view of read file." << std::endl;
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
//DETERMINE SIZE LIMIT OF INPUT FILE
readFileSize = GetFileSize(readFile, NULL);
//writeFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
//LPCTSTR *myStr2 = (LPCTSTR *)argv[2];
//Test Files -------------------------------------------------------------
char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
argv[2] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
//--------------------------------------------------------------------------
writeFile = CreateFile(outFilename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE IS VALID OR NOT
if(writeFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSSAGE IF FILE CAN'T BE OPENED
std::cout << "Write file could not be opened." << std::endl;
//std::cout << "argv[1] = " << argv[1] << " argv[2] = " << argv[2] << std::endl;
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
writeFileMap = CreateFileMapping(writeFile, NULL, PAGE_READWRITE, 0, readFileSize, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE MAP IS NULL
if(writeFileMap == NULL)
{
//DISPLAY ERROR MESSAGE THAT THE WRITE FILE CANNOT BE MAPPED
std::cout << "Write File could not be mapped." << std::endl;
CloseHandle(writeFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
pvwriteFile = MapViewOfFile(writeFileMap, FILE_MAP_WRITE, 0,0,0);
//IF STATEMENT IF THE PVWRITEFILE IS NULL
if(pvwriteFile == NULL)
{
//DISPLAY ERROR MESSAGE THAT I COULD NOT OPEN MAP VIEW OF WRITE FILE
std::cout << "Could not open map view of write file." << std::endl;
}
//POINTERS NEED TO BE CREATED
PSTR readptr = (PSTR) pvreadFile;
PSTR writeptr = (PSTR) pvwriteFile;
{
bool ret = isPalindrome( "eve redivider" );
}
//CLEANUP THE FILE
UnmapViewOfFile(pvwriteFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(writeFileMap);
CloseHandle(readFileMap);
CloseHandle(writeFile);
CloseHandle(readFile);
}
//ELSE STATEMENT IF CANNOT FIND FILE
else
//DISPLAY ERROR MESSAGE THAT NO FILE IS GIVEN
cout << "No file given" << endl << endl;
//RETURN A VALUE
return 0;
}
-
Re: Can someone please run this in XP
Yes, but you need to cooment out:
Code:
argv[1] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
Since if you do not supply enough arguments when running from
the command line (or do not run from the command line), that
will be past the bounds of the array.
Also remove the
logic (or set argc equal to 2).
But why don't you just run from the command line ?
-
Re: Can someone please run this in XP
Ok when you mean running from command line I am doing to change directory (cd0 and going to C:\Users\Bryan\Documents\CSI 345\Palindrome\Debug so I am typing the folliwng in command prompt...
cd C:\Users\Bryan\Documents\CSI 345\Palindrome\Debug and hitting enter...
the name of my input file is input.txt and output file is output txt so I am doing the following...
C:\Users\Bryan\Documents\CSI 345\Palindrome\Debug> palindrome.exe input.txt and get the "read file map could not be opened" I just want to make sure I am doing this part right
-
Re: Can someone please run this in XP
that is correct.
Is input.txt in the Debug folder ? If not, copy it there.
-
Re: Can someone please run this in XP
-
Re: Can someone please run this in XP
I don't use the non-standard I/O, but according to the documentation,
you can call GetLastError() to get extended error info.
So ...
Code:
if(readFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSAGE
std::cout << "Read file could not be opened." << std::endl;
std::cout << "value of GetLastError() is " << GetLastError() << "\n";
return(FALSE);
}
-
Re: Can someone please run this in XP
Quote:
Originally Posted by
jimJohnson123
yes sir it is in there
On the command line, give the complete path of the input file. Then it doesn't matter where the file is located, as you're giving the entire path to the command line:
Quote:
palindrome C:\Whatever\Whatever2\input.txt
Similar to that.
Regards,
Paul McKenzie
-
Re: Can someone please run this in XP
Just want to verify but did so and get the same response...
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Bryan>cd C:\Users\Bryan\Documents\CSI 345\Palindrome\Debug
C:\Users\Bryan\Documents\CSI 345\Palindrome\Debug>palindrome.exe C:\Users\Bryan\
Documents\CSI 345\Palindrome\Debug\input.txt
Read file map could not be opened.
C:\Users\Bryan\Documents\CSI 345\Palindrome\Debug>
-
Re: Can someone please run this in XP
Please, do what I tell you in the post#6. :cool:
-
Re: Can someone please run this in XP
looks like after debugging it shows that it cannot be opened at
int main(int argc, char *argv[]){
I am guessing this is what needs to be changed? if someone can just say yes or no to this I am goin to see what I can do with it
-
Re: Can someone please run this in XP
Quote:
Originally Posted by
jimJohnson123
looks like after debugging it shows that it cannot be opened at
int main(int argc, char *argv[]){
What? :confused:
Could you be more specific?
What exactly did you do? Where did you set a break point? Did you debug it step-by-step with F10? :confused:
-
Re: Can someone please run this in XP
Ok sorry about that....this is what I am doing and i showed where i put my breakpoint in caps so it stands out ....
Code:
//MAIN FUNCTION
//THIS IS WHERE MY BREAK POINT IS...I HIT F5 AND THEN KEEP HITTING F10 UNTIL I AM AT THE END OF MY PROGRAM
int main(int argc, char *argv[]){
LPCTSTR *myStr1 = (LPCTSTR *)argv[1];
HANDLE readFile, writeFile;
HANDLE readFileMap, writeFileMap;
PVOID pvreadFile, pvwriteFile;
DWORD readFileSize;
string word = "";
list<string> words;
//Test Files -------------------------------------------------------------
char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
//argv[1] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
//--------------------------------------------------------------------------
//VERIFYING ARGUMENTS
if(argc=2)
{
readFile = CreateFile(inFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO CHECK IF THE READ FILE IS NOT VALID
if(readFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSAGE
std::cout << "Read file could not be opened." << std::endl;
std::cout << "value of GetLastError() is " << GetLastError() << "\n";
return(FALSE);
}
readFileMap = CreateFileMapping(readFile, NULL, PAGE_READONLY, 0, 0, NULL);
//IF STATEMENT TO SEE IF THE READFILEMAP IS NULL
if(readFileMap == NULL)
{
//DISPLAY ERROR MESSAGE
std::cout << "Read file map could not be opened." << std::endl;
CloseHandle(readFile);
return(FALSE);
}
pvreadFile = MapViewOfFile(readFileMap, FILE_MAP_READ, 0, 0, 0);
//IF STATEMENT TO DETERMINE IF PVREADFILE IS NULL
if(pvreadFile == NULL)
{
//DISPLAY ERROR MESSAGE
std::cout << "Could not map view of read file." << std::endl;
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
//DETERMINE SIZE LIMIT OF INPUT FILE
readFileSize = GetFileSize(readFile, NULL);
//writeFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
//LPCTSTR *myStr2 = (LPCTSTR *)argv[2];
//Test Files -------------------------------------------------------------
char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
//argv[2] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
//--------------------------------------------------------------------------
writeFile = CreateFile(outFilename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE IS VALID OR NOT
if(writeFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSSAGE IF FILE CAN'T BE OPENED
std::cout << "Write file could not be opened." << std::endl;
//std::cout << "argv[1] = " << argv[1] << " argv[2] = " << argv[2] << std::endl;
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
writeFileMap = CreateFileMapping(writeFile, NULL, PAGE_READWRITE, 0, readFileSize, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE MAP IS NULL
if(writeFileMap == NULL)
{
//DISPLAY ERROR MESSAGE THAT THE WRITE FILE CANNOT BE MAPPED
std::cout << "Write File could not be mapped." << std::endl;
CloseHandle(writeFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
pvwriteFile = MapViewOfFile(writeFileMap, FILE_MAP_WRITE, 0,0,0);
//IF STATEMENT IF THE PVWRITEFILE IS NULL
if(pvwriteFile == NULL)
{
//DISPLAY ERROR MESSAGE THAT I COULD NOT OPEN MAP VIEW OF WRITE FILE
std::cout << "Could not open map view of write file." << std::endl;
}
//POINTERS NEED TO BE CREATED
PSTR readptr = (PSTR) pvreadFile;
PSTR writeptr = (PSTR) pvwriteFile;
{
bool ret = isPalindrome( "eve redivider" );
}
//CLEANUP THE FILE
UnmapViewOfFile(pvwriteFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(writeFileMap);
CloseHandle(readFileMap);
CloseHandle(writeFile);
CloseHandle(readFile);
}
//ELSE STATEMENT IF CANNOT FIND FILE
else
//DISPLAY ERROR MESSAGE THAT NO FILE IS GIVEN
cout << "No file given" << endl << endl;
//RETURN A VALUE
return 0;
}
-
Re: Can someone please run this in XP
Well...
Now try to comment out this lineand "replay" with debugging.
-
Re: Can someone please run this in XP
Ok I think I am making progress on this now. I try to debug and it goes through and tries to open but crashes which I expect as I have set this up for memory mapping but when I open with command prompt I get the message "too many arguments given" Just want to see if I am getting close...
Code:
#include <windows.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;
bool isPalindrome(const std::string& s)
{
return s == string(s.rbegin(),s.rend());
}
//MAIN FUNCTION
int main(int argc, char *argv[]){
HANDLE readFile, writeFile;
HANDLE readFileMap, writeFileMap;
PVOID pvreadFile, pvwriteFile;
DWORD readFileSize;
string word = "";
list<string> words;
//Test file
char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
//VERIFYING ARGUMENTS
if(argc == 1 )
{
cout << "opening for read: " << argv[1] << endl;
readFile = CreateFile(inFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO CHECK IF THE READ FILE IS NOT VALID
if(readFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSAGE
cout << "Read file could not be opened. Error code: " << GetLastError() << endl;
return(FALSE);
}
readFileMap = CreateFileMapping(readFile, NULL, PAGE_READONLY, 0, 0, NULL);
//IF STATEMENT TO SEE IF THE READFILEMAP IS NULL
if(readFileMap == NULL)
{
//DISPLAY ERROR MESSAGE
cout << "Read file map could not be opened. Error code: " << GetLastError() << endl;
CloseHandle(readFile);
return(FALSE);
}
pvreadFile = MapViewOfFile(readFileMap, FILE_MAP_READ, 0, 0, 0);
//IF STATEMENT TO DETERMINE IF PVREADFILE IS NULL
if(pvreadFile == NULL)
{
//DISPLAY ERROR MESSAGE
cout << "Could not map view of read file. Error code: " << GetLastError() << endl;
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
//DETERMINE SIZE LIMIT OF INPUT FILE
readFileSize = GetFileSize(readFile, NULL);
cout << "Opening for write: " << argv[2] << endl;
//writeFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
//test file
char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
writeFile = CreateFile(outFilename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE IS VALID OR NOT
if(writeFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSSAGE IF FILE CAN'T BE OPENED
cout << "Write file could not be opened. Error code: " << GetLastError() << endl;
//cout << "argv[1] = " << argv[1] << " argv[2] = " << argv[2] << endl;
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
writeFileMap = CreateFileMapping(writeFile, NULL, PAGE_READWRITE, 0, readFileSize, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE MAP IS NULL
if(writeFileMap == NULL)
{
//DISPLAY ERROR MESSAGE THAT THE WRITE FILE CANNOT BE MAPPED
cout << "Write File could not be mapped. Error code: " << GetLastError() << endl;
CloseHandle(writeFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
pvwriteFile = MapViewOfFile(writeFileMap, FILE_MAP_WRITE, 0,0,0);
//IF STATEMENT IF THE PVWRITEFILE IS NULL
if(pvwriteFile == NULL)
{
//DISPLAY ERROR MESSAGE THAT I COULD NOT OPEN MAP VIEW OF WRITE FILE
cout << "Could not open map view of write file. Error code: " << GetLastError() << endl;
}
//CLEANUP THE FILE
UnmapViewOfFile(pvwriteFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(writeFileMap);
CloseHandle(readFileMap);
CloseHandle(writeFile);
CloseHandle(readFile);
}
//ELSE STATEMENT IF CANNOT FIND FILE
else
{
//DISPLAY ERROR MESSAGE THAT NO FILE IS Given
switch( argc )
{
case 0:
cout << "No arguments given" << endl;
break;
case 1:
cout << "No read file or write file given" << endl;
break;
case 2:
cout << "No write file given" << endl;
break;
default:
cout << "too many arguments given." << endl;
}
}
//cout << "Arg 0 = file to read" << endl;
//cout << "Arg 1 = file to write to" << endl;
//RETURN A VALUE
return 0;
}
-
Re: Can someone please run this in XP
Why didn't you do what I asked you? (about commenting out a line in your previous code snippet)
Why did you show us a new code snippet until you have solved the problem with a previous one? :confused:
Why in the world do you ask for some help but then ignore the answers? :confused:
-
Re: Can someone please run this in XP
Sorry I commented out the line but did not let me run it
Its the same program but wanted to try something else
I did however get it to try opening but after doing so it crashes...Here is what I changed everything to...
Code:
#include <windows.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;
bool isPalindrome(const std::string& s)
{
return s == string(s.rbegin(),s.rend());
}
//MAIN FUNCTION
int main(int argc, char *argv[])
{
HANDLE readFile, writeFile;
HANDLE readFileMap, writeFileMap;
PVOID pvreadFile, pvwriteFile;
DWORD readFileSize;
string word = "";
list<string> words;
//Test file
//char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
char inFilename[] = "C:\\Palindrome\\Debug\\Input.txt";
//VERIFYING ARGUMENTS
if(argc == 1 )
{
cout << "opening for read: " << argv[1] << endl;
readFile = CreateFile(inFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO CHECK IF THE READ FILE IS NOT VALID
if(readFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSAGE
cout << "Read file could not be opened. Error code: " << GetLastError() << endl;
return(FALSE);
}
readFileMap = CreateFileMapping(readFile, NULL, PAGE_READONLY, 0, 0, NULL);
//IF STATEMENT TO SEE IF THE READFILEMAP IS NULL
if(readFileMap == NULL)
{
//DISPLAY ERROR MESSAGE
cout << "Read file map could not be opened. Error code: " << GetLastError() << endl;
CloseHandle(readFile);
return(FALSE);
}
pvreadFile = MapViewOfFile(readFileMap, FILE_MAP_READ, 0, 0, 0);
//IF STATEMENT TO DETERMINE IF PVREADFILE IS NULL
if(pvreadFile == NULL)
{
//DISPLAY ERROR MESSAGE
cout << "Could not map view of read file. Error code: " << GetLastError() << endl;
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
//DETERMINE SIZE LIMIT OF INPUT FILE
readFileSize = GetFileSize(readFile, NULL);
cout << "Opening for write: " << argv[2] << endl;
//writeFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
//test file
char outFilename[] = "C:\\Palindrome\\Debug\\Output.txt";
//char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
writeFile = CreateFile(outFilename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE IS VALID OR NOT
if(writeFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSSAGE IF FILE CAN'T BE OPENED
cout << "Write file could not be opened. Error code: " << GetLastError() << endl;
//cout << "argv[1] = " << argv[1] << " argv[2] = " << argv[2] << endl;
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
writeFileMap = CreateFileMapping(writeFile, NULL, PAGE_READWRITE, 0, readFileSize, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE MAP IS NULL
if(writeFileMap == NULL)
{
//DISPLAY ERROR MESSAGE THAT THE WRITE FILE CANNOT BE MAPPED
cout << "Write File could not be mapped. Error code: " << GetLastError() << endl;
CloseHandle(writeFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
pvwriteFile = MapViewOfFile(writeFileMap, FILE_MAP_WRITE, 0,0,0);
//IF STATEMENT IF THE PVWRITEFILE IS NULL
if(pvwriteFile == NULL)
{
//DISPLAY ERROR MESSAGE THAT I COULD NOT OPEN MAP VIEW OF WRITE FILE
cout << "Could not open map view of write file. Error code: " << GetLastError() << endl;
}
//CLEANUP THE FILE
UnmapViewOfFile(pvwriteFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(writeFileMap);
CloseHandle(readFileMap);
CloseHandle(writeFile);
CloseHandle(readFile);
}
//ELSE STATEMENT IF CANNOT FIND FILE
else
{
//DISPLAY ERROR MESSAGE THAT NO FILE IS Given
switch( argc )
{
case 0:
cout << "No arguments given" << endl;
break;
case 1:
cout << "No read file or write file given" << endl;
break;
case 2:
cout << "No write file given" << endl;
break;
default:
cout << "too many arguments given." << endl;
}
}
//cout << "Arg 0 = file to read" << endl;
//cout << "Arg 1 = file to write to" << endl;
//RETURN A VALUE
return 0;
}
-
Re: Can someone please run this in XP
Where did it crash? on what line of your code snippet? And how exactly did it crash?
-
Re: Can someone please run this in XP
this is what I did...
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Bryan>cd C:\Palindrome\Debug
C:\Palindrome\Debug>palindrome.exe
opening for read:
C:\Palindrome\Debug>
When I do this it I get a message right away saying palindrome.exe has stopped working...is there a way I can do this by getting rid of the argc and argv as I went over this but just dont understand it that great I am sorry to keep asking pretty much same ???
-
Re: Can someone please run this in XP
I asked you to debug
Can't you debug? :confused:
-
Re: Can someone please run this in XP
Code:
//Test file
char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
//VERIFYING ARGUMENTS
if(argc == 1 )
{
cout << "opening for read: " << argv[1] << endl;
...
}
argc stands for argument count. if you have only one argument, that would be the path to your exe.
if you write this in your console
Palindorme.exe "Testing.txt"
How many arguments will be passed to your exe? 1 or 2??
I will tell you it is not 1 ;)
argc will = 2
arg0 will be the path to your exe. this is automatic. you will always receive this before any of your arguments.
arg1 will be "Testing.txt"
so if( argc == 1 ) would mean you have ZERO arguments that you passed...
so if I typed this in the console
Palindorme.exe "Testing.txt"
arg[ 0 ] will equal the path to this exe file of yours
arg[ 1 ] will equal the very first argument that you passed.
arg[ 2 ] will be anything else you sent to your exe after "Testing.txt"
here seems to be a good tutorial on this http://www.learncpp.com/cpp-tutorial...ine-arguments/
Quote:
Originally Posted by jimJohnson123
is there a way I can do this by getting rid of the argc and argv as I went over this but just dont understand it that great I am sorry to keep asking pretty much same ???
If you want to start without the command line args try this,
Code:
#include <windows.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;
bool isPalindrome( const std::string& s )
{
std::string sReverse = s;
std::reverse( sReverse.begin(), sReverse.end() );
return s == sReverse; // return true if the reverse is the same as non-reverse
}
//MAIN FUNCTION
int main()
{
HANDLE readFile, writeFile;
HANDLE readFileMap, writeFileMap;
PVOID pvreadFile, pvwriteFile;
DWORD readFileSize;
string word = "";
list<string> words;
char inFilename[] = "C:\\Palindrome\\Debug\\Input.txt";
char outFilename[] = "C:\\Palindrome\\Debug\\Input.out.txt";
cout << "opening for read: " << inFilename << endl;
readFile = CreateFile( inFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
//IF STATEMENT TO CHECK IF THE READ FILE IS NOT VALID
if( readFile == INVALID_HANDLE_VALUE )
{
//DISPLAY ERROR MESSAGE
cout << "Read file could not be opened. Error code: " << GetLastError() << endl;
return( FALSE );
}
readFileMap = CreateFileMapping( readFile, NULL, PAGE_READONLY, 0, 0, NULL );
//IF STATEMENT TO SEE IF THE READFILEMAP IS NULL
if( readFileMap == NULL )
{
//DISPLAY ERROR MESSAGE
cout << "Read file map could not be opened. Error code: " << GetLastError() << endl;
CloseHandle( readFile );
return( FALSE );
}
pvreadFile = MapViewOfFile( readFileMap, FILE_MAP_READ, 0, 0, 0 );
//IF STATEMENT TO DETERMINE IF PVREADFILE IS NULL
if( pvreadFile == NULL )
{
//DISPLAY ERROR MESSAGE
cout << "Could not map view of read file. Error code: " << GetLastError() << endl;
CloseHandle( readFileMap );
CloseHandle( readFile );
return( FALSE );
}
//DETERMINE SIZE LIMIT OF INPUT FILE
readFileSize = GetFileSize( readFile, NULL );
cout << "Opening for write: " << outFilename << endl;
//writeFile = CreateFile(outFilename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
writeFile = CreateFile( outFilename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
//IF STATEMENT TO DETERMINE IF WRITE FILE IS VALID OR NOT
if( writeFile == INVALID_HANDLE_VALUE )
{
//DISPLAY ERROR MESSSAGE IF FILE CAN'T BE OPENED
cout << "Write file could not be opened. Error code: " << GetLastError() << endl;
//cout << "inFilename = " << inFilename << " outFilename = " << outFilename << endl;
UnmapViewOfFile( pvreadFile );
CloseHandle( readFileMap );
CloseHandle( readFile );
return( FALSE );
}
writeFileMap = CreateFileMapping( writeFile, NULL, PAGE_READWRITE, 0, readFileSize, NULL );
//IF STATEMENT TO DETERMINE IF WRITE FILE MAP IS NULL
if( writeFileMap == NULL )
{
//DISPLAY ERROR MESSAGE THAT THE WRITE FILE CANNOT BE MAPPED
cout << "Write File could not be mapped. Error code: " << GetLastError() << endl;
CloseHandle( writeFile );
UnmapViewOfFile( pvreadFile );
CloseHandle( readFileMap );
CloseHandle( readFile );
return( FALSE );
}
pvwriteFile = MapViewOfFile( writeFileMap, FILE_MAP_WRITE, 0,0,0 );
//IF STATEMENT IF THE PVWRITEFILE IS NULL
if( pvwriteFile == NULL )
{
//DISPLAY ERROR MESSAGE THAT I COULD NOT OPEN MAP VIEW OF WRITE FILE
cout << "Could not open map view of write file. Error code: " << GetLastError() << endl;
}
//CLEANUP THE FILE
UnmapViewOfFile( pvwriteFile );
UnmapViewOfFile( pvreadFile );
CloseHandle( writeFileMap );
CloseHandle( readFileMap );
CloseHandle( writeFile );
CloseHandle( readFile );
return 0;
}
But you must learn on how to accept args still :D They can be important later on.., but I would get this running right first and then try to add args... No reason to try too much at once
-
Re: Can someone please run this in XP
OK I got my input file to work and it now I do a copy con input.txt and it loads into my input file successfully....
Now when I open my output file it is not doing as it should...I also checked the requirements and I do need argc and argv...can anyone see why my output file is blank as it should be storing palindromes?
Code:
#include <windows.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;
bool isPalindrome(const std::string& s)
{
return s == string(s.rbegin(),s.rend());
}
//MAIN FUNCTION
int main(int argc, char *argv[])
{
HANDLE readFile, writeFile;
HANDLE readFileMap, writeFileMap;
PVOID pvreadFile, pvwriteFile;
DWORD readFileSize;
string word = "";
list<string> words;
//Test file
//char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
//VERIFYING ARGUMENTS
if(argc = 2 )
{
cout << "opening for read: " << argv[1] << endl;
readFile = CreateFile(inFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO CHECK IF THE READ FILE IS NOT VALID
if(readFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSAGE
cout << "Read file could not be opened. Error code: " << GetLastError() << endl;
return(FALSE);
}
readFileMap = CreateFileMapping(readFile, NULL, PAGE_READONLY, 0, 0, NULL);
//IF STATEMENT TO SEE IF THE READFILEMAP IS NULL
if(readFileMap == NULL)
{
//DISPLAY ERROR MESSAGE
cout << "Read file map could not be opened. Error code: " << GetLastError() << endl;
CloseHandle(readFile);
return(FALSE);
}
pvreadFile = MapViewOfFile(readFileMap, FILE_MAP_READ, 0, 0, 0);
//IF STATEMENT TO DETERMINE IF PVREADFILE IS NULL
if(pvreadFile == NULL)
{
//DISPLAY ERROR MESSAGE
cout << "Could not map view of read file. Error code: " << GetLastError() << endl;
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
//DETERMINE SIZE LIMIT OF INPUT FILE
readFileSize = GetFileSize(readFile, NULL);
cout << "Opening for write: " << argv[2] << endl;
//writeFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
//test file
char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome";
//char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
writeFile = CreateFile(outFilename, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE IS VALID OR NOT
if(writeFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSSAGE IF FILE CAN'T BE OPENED
cout << "Write file could not be opened. Error code: " << GetLastError() << endl;
//cout << "argv[1] = " << argv[1] << " argv[2] = " << argv[2] << endl;
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
writeFileMap = CreateFileMapping(writeFile, NULL, PAGE_READWRITE, 0, readFileSize, NULL);
//IF STATEMENT TO DETERMINE IF WRITE FILE MAP IS NULL
if(writeFileMap == NULL)
{
//DISPLAY ERROR MESSAGE THAT THE WRITE FILE CANNOT BE MAPPED
cout << "Write File could not be mapped. Error code: " << GetLastError() << endl;
CloseHandle(writeFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}
pvwriteFile = MapViewOfFile(writeFileMap, FILE_MAP_WRITE, 0,0,0);
//IF STATEMENT IF THE PVWRITEFILE IS NULL
if(pvwriteFile == NULL)
{
//DISPLAY ERROR MESSAGE THAT I COULD NOT OPEN MAP VIEW OF WRITE FILE
cout << "Could not open map view of write file. Error code: " << GetLastError() << endl;
}
//CLEANUP THE FILE
UnmapViewOfFile(pvwriteFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(writeFileMap);
CloseHandle(readFileMap);
CloseHandle(writeFile);
CloseHandle(readFile);
}
//ELSE STATEMENT IF CANNOT FIND FILE
else
{
//DISPLAY ERROR MESSAGE THAT NO FILE IS Given
switch( argc )
{
case 0:
cout << "No arguments given" << endl;
break;
case 1:
cout << "No read file or write file given" << endl;
break;
case 2:
cout << "No write file given" << endl;
break;
default:
cout << "too many arguments given." << endl;
}
}
//cout << "Arg 0 = file to read" << endl;
//cout << "Arg 1 = file to write to" << endl;
//RETURN A VALUE
return 0;
}
-
Re: Can someone please run this in XP
if(argc = 2 ) is wrong. you don't assign a value to argc :) use the other operator meant for comparison
-
Re: Can someone please run this in XP
thanks....changed to argc==2 and input still works fine but output has a blank file....could my palindrome not be correct?
-
Re: Can someone please run this in XP
if(argc == 2 )
cout << "Opening for write: " << argv[2] << endl;
Consider that a buffer overrun
Again... debugging will tell you this... or it should. You are trying to modify code that was good when you simply need to learn how to pass args right....
-
Re: Can someone please run this in XP
No I dont see it anywhere when I put it == 2 or if I put argc > 1
-
Re: Can someone please run this in XP
arg[2] == arg + 2
consider
arg[ 0 ] = arg + 0
arg[ 1 ] = arg + 1
arg[ 2 ] = arg + 2
how far down is 2? is it 3 spots or simply 2 and if argc == 2, what does this mean?
-
Re: Can someone please run this in XP
I believe it means it is passing two arguments into the program
-
Re: Can someone please run this in XP
Now I ask if c arrays are based on a zero base or a 1 base?
-
Re: Can someone please run this in XP
Well, dear jimJohnson123,
after eight days this discussion seems to become infinite...
So I only have to repeat:
Quote:
Originally Posted by
VictorN
It is just because you think you are "not needing to hear your comments anymore". It is very bad but for you only! :thumbd:
I would suggest you to use neither any type of programming nor any work concerning logic and mathematics. :cool:
and give up... :sick:
-
Re: Can someone please run this in XP
-
Re: Can someone please run this in XP
think again :D It is zero base. arg[ 0 ] is the first argument. arg[ 0 ] is filled in by NOT YOU, but the os. this is how it works. arg[ 1 ] is your second argument passed to your exe AND is the first one you sent.
so
int arr[ 1 ];
arr[ 0 ] = 0; good
arr[ 1 ] = 0; Bad!
-
Re: Can someone please run this in XP
-
Re: Can someone please run this in XP
Quote:
Originally Posted by
jimJohnson123
Ok I think I am making progress on this now. I try to debug and it goes through and tries to open but crashes which I expect as I have set this up for memory mapping but when I open with command prompt I get the message "too many arguments given" \
Your problem when you entered the full path on the command-line is that your file name contained spaces.
When entering a command-line parameter, and that parameter has spaces, you use double quotes around that parameter.
Quote:
palindrome.exe "C:\A File Name With Spaces\input.txt"
Regards,
Paul McKenzie
-
Re: Can someone please run this in XP
Quote:
Originally Posted by jimJohnson123
k ill stop posting
Do what you like, but I suggest you try the code from #28( unmodified ) and pass both the input file and the output file. If there is any error with the code, we can help, but if you can't run your exe with the appropriate command lines or with the debugger, we really can't help you :(.
Follow this http://stackoverflow.com/questions/2...-visual-studio