Please, read the post#6
Printable View
Please, read the post#6
By doing so this is the line I am getting the error in but cannot figure out why...
readFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
I believe it has something to do with argv[1]
When you run from the command line, the argument that you give
the executable will be argv[1]. What are you entering ?
argv[1] = filenameCode:your_exe filename
As I mentioned in my previous post, remove tghe argc/argv coding
and hardwire a filename. Does your code work ?
I am not sure what I even am supposed to run from the command prompt
jimJohnson123, open up your command prompt and try actually using the program the way it was designed to be used. You type:
name_of_program.exe name_of_file_to_process
When you execute the programs from the command line, and supply the name of your file, then argc will be > 1.
The reason you've been failing is because you are trying to run the thing from the MSVC IDE!
OK I guess I am just not doing command prompt right as this is what I did...
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\Palindrome
C:\Users\Bryan\Documents\CSI 345\Palindrome\Palindrome>palindrome.exe
'palindrome.exe' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Bryan\Documents\CSI 345\Palindrome\Palindrome>
My program is located here...
C:\Users\Bryan\Documents\CSI 345\Palindrome\Palindrome
The following files are located here...
debug(folder), Palindrome (C++ source), Input(text document), Palindrome (VC project), palindrome.vcxproj (VC++ Project Filter File), Palindrome.vcxproj(Visual Studio Project User Options File)
I just need someone to show me how to run this through command prompt and please only positive feedback I do not want anyone responding with rude comments(not saying anyone has just want to be upfront) My reasoning is this is probably extremely simple for most of u guys thats all
You're right, it's very simple. Judging from post #15,
your executable is in "'C:\Users\Bryan\Documents\CSI 345\Palindrome\Debug\"Quote:
'Palindrome.exe': Loaded 'C:\Users\Bryan\Documents\CSI 345\Palindrome\Debug\Palindrome.exe', Symbols loaded.
So, open a command prompt:
C:
CD \Users\Bryan\Documents\CSI 345\Palindrome\Debug\
Palindrome.exe <NAMEOFTHEFILE>
I did that with input.txt and get the error read file map could not be opened so it does not it past the first section
I think I read on how to fix this....
If I wanted to how would I cast argv[1] and argv[2] to a LPCTSTR data type?
just like the how your code below doesQuote:
Originally Posted by jimJohnson123
CreateFileCode:readFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE WINAPI CreateFile( __in LPCTSTR lpFileName, ..... );
LPCSTR stands for long pointer const string... basically. The same as const char* which char* is being implicitly casted to.
does that even seem like what needs to be done to fix this or am i reading something that is not the problem
If your output didn't read "No file given", it will try to open the file. You will have to use GetLastError() after the last call that failed to return the error code. Look up the number at http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx to find why it didn't workQuote:
Originally Posted by jimJohnson123
I did read
which seems to be misplaces and should be deleted, but go ahead and run this modified versionCode:if(argc>1)
{
// stuff I don't care about
}
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;
//VERIFYING ARGUMENTS
if(argc == 3 )
{
cout << "opening for read: " << argv[1] << endl;
readFile = CreateFile(argv[1], 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);
writeFile = CreateFile(argv[2], 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 << "Way 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;
}
You still haven't proven you understand the very simple basics.
Where does argc get its value? If you don't understand or can't answer that question, then there is no way you are going to understand the program that you supposedly wrote.
Regards,
Paul McKenzie