File I/O - directory problem on WinCe

I wonder if there are many people who program for winCE with C++, but I'm going to ask ahead anyway, maybe somone knows how to answer.

I am doing an application similar to notepad, and I simply want to read and write to a text file. There are loads of examples on the net on how to do this, but that's not the problem.


On a normal computer, you would ask the user on where you want to save the file, and you would get something like "C:\file.txt". Or you could just say "file.txt" and it would save it in the working directory.

I tried this code:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}


It works perfectly on my computer. Then when I try it on winCe, I couldn't find the file anywhere. Now the problem is the following: I want to get the current working directory, to make sure the file is saved there. I found alot of samples on the net too on how to do this, but they all use "direct.h" which is not available for winCE. So, any ideas on how to go around it, or how to get the current working directory without direct.h?
Any help would be greatly appreciated. Thanks! :)
Can you use the regular Windows API under CE?
DWORD WINAPI GetCurrentDirectory(
__in DWORD nBufferLength,
__out LPTSTR lpBuffer
);
Thanks for your reply. Most of the API functions are available for WinCE,
usually depending on the device and Windows version. However I couldn't get it to work. I tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// DirTestWM6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	LPSTR buff = "";
	
	for (int i = 0; i < 1025; i++) { //Filling the buffer with spaces
		buff += ' ';
	}
	GetCurrentDirectoryA(1024,buff); // <- Exception received here
	cout << buff;             //Attempting to output result
	system("pause");

	return 0;
}


It compiles fine, (the function is available) but I get this runtime error:
"Unhandled exception at 0x.... in DirTestWm6.exe 0x......:
Access violation writing location 0x......"


Did you mount the drive you are trying to save to? I use Windows CE from time to time it is a ROM OS. If you don't have a real storage device selected then your problem might be that you are trying to save it to RAM hence the access violation.
That's not how character arrays work.
1
2
3
4
5
//LPSTR buff = ""; //Initializing output pointer to read-only buffer. Will error if attempted to write to.
TCHAR buff[1024]; //Create a buffer on the stack.
//No need to fill, since GetCurrentDirectory() will overwrite anything at the beginning of the array.
GetCurrentDirectory(1024,buff);
buff[1023]=0; //Ensure buffer is null-terminated. 
@ helios: Wow, who has two thumbs and answers the OPs question without reading his code? This guy...

Also Windows CE is a ROM OS and uses the RAM\Cache for its 'drive', unless you mount a physical drive to the system and save your data there it will not survive a reboot.

http://en.wikipedia.org/wiki/WinCE


@computergeek01:
I was running a WindowsMobile 6 Device, which is WinCE based, but not exactly the same thing, as far as I know :) Altough if I write programs for WinCE they will work for my device. It was a problem with my code!

Anyways, I found out, the default output directory is "\My documents", for a normal pc this would be the application path.

Do you know, by any chance, if there is an API that could open a dialog box that would return the path in which the user wants to save/load??

@Helios
Thanks, it worked :)
Although there is alot that I don't understand. Why was TCHAR used when the function required LPSTR as a parameter? I understood almost everything apart from:
"create a buffer on the stack" =/
When you initialize a pointer, don't you use a *?
Last thing, the buffer is readonly, but did you edit it in the last line? That confused me....
Last edited on
Topic archived. No new replies allowed.