2

I have a simple C code as below :

#pragma warning(disable : 4996)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <windows.h>

#include <tchar.h>
int main()
{
    int ndsc;
    char *line = ' ';
    FILE *fd;
    int  x;
    wchar_t path[256];

    wcscpy(path, L"C:/");

    //wcscat(path, L"common\\descr.txt", wcslen(L"common\\descr.txt"));
    //Previous
    wcscat(path, L"common/descr.txt");



    if ((fd = _wfopen(path, L"r")) == NULL)
    {
        printf("Can't open %s for reading.\n", path);
        return 1;
    }
    for (ndsc = 0; fgetws((wchar_t*)line, 80, fd) != NULL; ndsc++)
    {
        x = wcslen((const wchar_t*)line);
        printf("Length of %d line is %d\n", ndsc, x);
    }
    fclose(fd);
    return 0;
}

I am trying to read a file called descr.txt from C:\Common directory. But in for loop it throws unhandled exception as below :

Unable to read memory

Exception

Can anybody explain me why I am getting this error and how to resolve. By the way this code is being used in an .arx file of AutoCad. I just extracted the part which is throwing error and tried to run it as a standalone project in VS 2015 Update 3. This code is a part of .C file being called by from .NET. Even when executing in the context of .arx ( From AutoCad Clicking on the desired menu ) got the same exception.

Thanks in Advance.

Cid
  • 14,968
  • 4
  • 30
  • 45
Abhilash D K
  • 1,223
  • 1
  • 23
  • 39
  • 3
    Please do not add error messages as images. They are pure text you can add to the question. – Gerhardh Aug 04 '17 at 11:49
  • Are you sure the compiler does not show any warning when you initialize `line`? You should read, understand and solve any warning you get. – Gerhardh Aug 04 '17 at 11:51
  • No The compiler did not show any warning. In my original code that variable is uninitialized. – Abhilash D K Aug 04 '17 at 11:52
  • But we are not talking about any original code. We only talk about the code from your question. This should definitely give a warning for wrong type. – Gerhardh Aug 04 '17 at 12:01

4 Answers4

4

Here: char *line = ' '; You assign a char to a char *.

Later you try to use a wide string buffer. You need wchar_t line[80] to actually allocate space for 80 wchar_t.

001
  • 13,291
  • 5
  • 35
  • 66
1

The variable line is a pointer, it points to the address 32 (using ASCII encoding).

That address is hardly a valid address to write a string of input.

You need to make it point to some memory big enough to fit the whole string you read (including the terminator). Or have it be an array.

Since the pointer is incorrect you will have undefined behavior, which makes your whole program ill-formed and invalid.

Furthermore, that you need to cast it in the call to fgetsw is another sign of you doing something wrong, and that is using the wrong type (char instead of wchar_t).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Line is a pointer to some illegal address casted from int value of ascii code of ' '.

instead of char *line = ' ';

make it (if you want line to be an array of chars)

 char line[80 * sizeof(wchar_t)];
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Is there any special reason why `line` should still have `char` type? Why not use `wchar_t line[80]`? Apart from the fact that this is already suggested in previous answer from Johnny Mopp... – Gerhardh Aug 04 '17 at 11:59
  • No. But he declared it as char - so I am not going to change his ideas – 0___________ Aug 04 '17 at 11:59
  • No no special reason. We are upgrading an arx project written for AutoCad 2014 to AutoCad 2018. The code already existed as I have posted. When loaded into AutoCad and executing the command Exception was throw in the for loop. So created a separate project. In this separate project it works after changing to wchar_t line[90]. But in .arx it is not even reading the file. Now it is showing error in the line where file is being opened for reading. Any Ideas why. – Abhilash D K Aug 04 '17 at 12:02
  • Do you have enough rights to open it? Run as administrator. Maybe this file is locked by another process - check the error. – 0___________ Aug 04 '17 at 12:04
  • Hi I ran VS as administrator. Still same error. The control goes inside if ((fd = _wfopen(path, L"r")) == NULL) and gives me a series on Unhandled Exception Messages. Any Ideas. If it is going inside this if that means fd == NULL. I don't know why. – Abhilash D K Aug 04 '17 at 12:19
0

You arre trying to assign a char to a char * by using simple quotes.

If you want to directly assign an int or a char to a char *, use double quotes (" "), or, if it is a single char, you might want to proceed as follows:

char line[0] = ' ';

Make sure that your pointer has been malloc'd and it should do the trick.

A string of digits can later be re-converted to an int using atoi() if you want.

ilomax
  • 568
  • 1
  • 4
  • 24