0

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?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Ezani
  • 535
  • 3
  • 18
  • 1
    You appear to be failing to understand a very fundamental concept: [Fully qualified vs. relative paths](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#fully-qualified-vs-relative-paths). It appears to be the theme of all of your [recent questions](https://stackoverflow.com/q/60666022/1889329). You're going to have to fix that first to be able to make any progress. – IInspectable Mar 16 '20 at 07:17
  • @IInspectable: Windows CE's file system does not work like a full-fledged Windows file system. In Windows CE, the file system is compressed and flattened. You won't find paths like "C:\1.txt" in Windows CE. – Ezani Mar 16 '20 at 09:10
  • 1
    @Ezani Try `fopen` instead of [`_wfopen`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=vs-2019) which is a wide-character version of `fopen`. Or use `fPtr = _wfopen(L"path_to\\1.txt", L"rt");` – Rita Han Mar 17 '20 at 06:25
  • 1
    @Use [`CreateFile`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea) if you can, when it fails you can call `GetLastError()` to retrieve the error code. – Rita Han Mar 17 '20 at 06:27
  • @Rita Han, wow you guys from Microsoft are amazing! It works when I changed the file open code to `fPtr = fopen("\\My Documents\\1.txt", "r");`. The other thing was WinCE was hiding the ".txt" extension by default so I mistakenly added that extension and was actually accessing "1.txt.txt" lol. So it was a combo of that and ditching the wide-character version of fopen. Thanks and upvoted Rita Han from Microsoft! – Ezani Mar 17 '20 at 07:04
  • @Ezani Glad to help. I post an answer you can accept it. Feel free let me know if you still have any question about this issue. – Rita Han Mar 17 '20 at 07:12
  • @Rita Han, I know that to "accept" an answer I need to click the tick mark on the left of your answer. But your answer above is actually a comment. Maybe you can move it to the Answer section so I can tick? – Ezani Mar 17 '20 at 08:03

2 Answers2

1

It appears SHGetSpecialFolderPath is indeed available on Windows CE.

Call that to get the full path to the My Documents folder.

selbie
  • 100,020
  • 15
  • 103
  • 173
1

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.

Rita Han
  • 9,574
  • 1
  • 11
  • 24