I am just about to finish my last C++ program and for the life of me cannot figure it out. My problem is everytime I try to run this my command prompt returns with "Write file cannot be opened".

I have looked many times throughout my program and cannot figure this out. I only have a week left to finish this before graduation and it pretty much is an all or nothing. If anyone can see why this is happening please let me know as soon as you can.

#include <windows.h>
#include <iostream>
#include <string>
#include <list>
using namespace std;

//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>1)
	{
		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
			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, 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;
			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;

		//INPUT WORDS FROM FILE WITH FOR STATEMENT
		for(int i=0; i < readFileSize; i++)
		{
			while(isalpha(readptr[i]))
			{
				word = word += tolower(readptr[i]);
				i++;
			};
			words.push_front(word);
			word = "";
		}

		//SORT OUT THE WORDS
		words.sort();

		//WRITE THE WORDS THAT ARE SORTED TO THE OUTPUT FILE AND USE A FOR STATEMENT
		for(int i=0; i<readFileSize; i++)
		{
			word = words.front();
			int j = 0;

			//CREATE WHILE STATEMENT
			while(j < word.size())
			{
				writeptr[i] = word[j];
				j++;
				i++;
			};
			writeptr[i] = ' ';
			words.pop_front();
		}

		//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;
}

Recommended Answers

All 6 Replies

I apologize that did not come out how it should have

I apologize that did not come out how it should have

Is this supposed to mean something?

My guess is your second command line parameter for the write file is bad. And it doesn't already exist since you are using the OPEN_EXISTING flag.

I apologize I meant the code just didnt look organized when I copied it

I tried the following instead of OPEN_EXISTING and did not work...

CREATE_NEW
OPEN_ALWAYS

Anyone know if anything else in this line I can try because this is where the problem apparently is from my instructor and been looking at it for 5 days now

writeFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); :
Are you opening this file for reading? If so, why GENERIC_WRITE?
Are you opening this file for writing? If so, why GENERIC_READ?
Are you sure you want to require the file to exist using OPEN_EXISTING if all you are doing is writing anyway?

Look up all these attributes and understand when to use them, and decide if you need them in this particular case. Don't just blindly change things and hope for the best. That's not how programming works.

Also, write a test program to test the one statement you are having trouble with. See if you can get it to work without all that other overhead.

I also had one additional question...Can someone let me know if my palindrome even works as I also previously used the following code and not sure if either will work even...

#include <string>
#include <algorithm>

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
}

int main()
{
    bool ret = isPalindrome( "eve redivider" );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.