I am trying to write a simple program to READ a text file named "1.txt" (which contains just "abc") in Windows CE OS using Windows Mobile 5.0 SDK, WIN32 and C in Visual Studio 2008. I have stored this text file in My Documents folder.
My program is giving me the error "Cannot open text file" which means I can't open the file to be read . I suspect I am not setting the correct path to my file according to WinCE file structure, but (as you can see from my commented code) I've tried all kinds of path expressions for Windows CE to no avail. I tried the GetModuleFileName() function and it IS returning the filepath "My Documents\1.txt". Here is my code:
#include <winbase.h>
#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "resource.h"
#include "ScanCAPI.h"
#include <time.h>
#include <tchar.h>
#pragma comment(lib, "Kernel32.lib")
wchar_t text10[256];
FILE * fPtr;
.
.
switch(uMsg)
{
case WM_INITDIALOG:
fPtr = _wfopen ("My Documents\\1.txt" , "rt");
//fPtr = _wfopen ("\\My Documents\\1.txt" , "rt");
//fPtr = _wfopen ("\My Documents\1.txt" , "rt");
//fPtr = _wfopen ("My Documents\1.txt" , "rt");
//fPtr = _wfopen ("My Device\1.txt" , "rt");
if (fPtr != NULL)
{
if ( fgetws (text10 , 100 , fPtr) != NULL )
wprintf("%s",fgetws(text10,255,fPtr));
//fwscanf(fPtr,"%s", &text10);
//fgetws (text10 , 255 , fPtr);
//fputws ( text10, stdout );
//fwscanf(fPtr,"%s", &text10);
MessageBox(0, text10, TEXT("text10"), MB_OK); //returning blank message box
} else {
MessageBox(0, TEXT("Cannot read file."), TEXT("File Read Error"), MB_OK);
}
if(fPtr == NULL)
{
//Open File failure
fclose(fPtr);
MessageBox(0, TEXT("Cannot open text file."), TEXT("File Open Error"), MB_OK);
PostQuitMessage(0);
}
.
.
Here is the contents of my Resource.h header file :
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
//
#define IDD_DIALOG_SSCAN 101
#define IDI_ICON1 102
#define IDC_STATIC1 995
#define IDC_EDIT1 1010
#define IDC_BUTTON1 1012
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 105
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1014
#define _APS_NEXT_SYMED_VALUE 106
#endif
#endif
How do I open my "My Documents\1.txt" text file for reading?
Either fPtr = _wfopen(L"path_to\\1.txt", L"rt");
or fPtr = fopen("path_to\\1.txt", "rt");
works for me on Windows 10.
Try fopen
instead of _wfopen
which is a wide-character version of fopen
.