rca_rodrigo

I wrote this little program to a small bussines.

In the code, first i have to check that a serial number that the customer have in the file :(c:\windows\file.bin) is the same as the program in its pc.

/** Microsoft Visual Studio 2005
Version 8.0.50727.762 (SP.050727-7600)
Microsoft .NET Framework
Version 2.0.50727 **********************/

errno_t err=0;

err=fopen_s(&fp, "d:\\windows\\file.bin","r");

but I get an error (err is set to "2" wich means file or path not found).

But when i use this line under the turboc compiler it works ok:

Turbo C v3.0 by Borland

fp=fopen("d:\\windows\\file.bin","r");

As you can see both are the same path but just the last one seems to work.

Is it a compiler problem

Am i doing something wrong

I will appreciate any help

Thanks in advance

rca_rodrigo @ hotmail.com



Re: Visual C++ General Problem opening a fiel using fopen_

greg.in.software

Have you tried to use use fopen() instead The fopen_s() documentation says "Files opened by fopen_s and _wfopen_s are not sharable". Since the file is in the Windows directory they I presume that this file is being already opened.

Greg.





Re: Visual C++ General Problem opening a fiel using fopen_

rca_rodrigo

Greg.

Yes I Tried. The file im trying to open was made for me and it constains a single string with the serial number of the aplication.. so i dont know ( i really dont) whow it file could be shareable.

Then i get this msg:

warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

And when i run it , the file doesnt open either

8(

Then it raises another question: Is there any other method to open a file.

Thanks





Re: Visual C++ General Problem opening a fiel using fopen_

greg.in.software

This warning is only ... a warning. The fopen() is still here and will be here.

If your fopen() fails then I'm really surprized since the code you showed (assuming the one using fopen() was correct) has no reason to fail.

The sharing problem could occur if the file was already opened, but if you say that nobody uses it then there is no reason why fopen() could return errno = 2 (file not found.)

I would guess that you don't have read access to the file since it is in the windows/ directory but then why would Borland's function work for you !

Can you simply notepad the file As the same user as one that runs your program that fails

Greg.





Re: Visual C++ General Problem opening a fiel using fopen_

rca_rodrigo

This is the code:

/*****************************************************************/

char serial_leidoDevil;
FILE *archivo;
errno_t err=0;

err=fopen_s(&archivo, "d:\\windows\\data_pin.bin","r");
//archivo=fopen("d:\\windows\\data_pin.bin","r"); // BorlandC

if(err!=0)

// if(archivo!=NULL) //BorlandC

{
fscanf_s(archivo, "%s", &serial_leido,6); //6 cause it's the size of the string

// fscanf(archivo, "%s", &serial_leido); //BorlandC, and it obteins the Serial number I am asking.

if(strcmp(SERIAL,serial_leido))
{
printf("\n ...

/*****************************************************************/


in "serial_leido" i got nothing...

I checked that this file is in that location... ¡®cause I¡¯m doing it in my machine.
I got ADM privileges¡­ so there¡¯s no problem accessing that file

If u need i can send you the whole program and u can check it by yourself.

... and Thanks again





Re: Visual C++ General Problem opening a fiel using fopen_

greg.in.software

Let's reasume:

You have admin privileges so you don't have file access restriction.
The file does exist and nobody/nothing is keeping the file opened.
fopen() does not work for you either.

There is a small thing I would like to notice:
a) I don't know how big the serial_leido is - the small devil has replaced the size of the buffer ;)
b) I assume you commented out the err variable checking just to let it work.
c) I don't have 2005 at home, but if you can c/p the main file for me, so I can take a look at it (sorry for such big delay in the response. home stuff...)

Greg.





Re: Visual C++ General Problem opening a fiel using fopen_

Simple Samples

 rca_rodrigo wrote:
if(err!=0)
Should this be:

if(err==0)

Also, is your project set to use Unicode by default

Also, are you sure there is not any code in your program before the code you posted Are you using both versions in the same computer

 rca_rodrigo wrote:
char serial_leidoDevil;
If the serial number is 6 characters, then you need at least 7 characters for serial_leido, as in:

char serial_leido[7];

Because a string must have a terminating null at the end. Be sure to also change the 6 in fscanf_s. I don't know what fscanf_s does when the string won't fit, but it seems likely that it won't read in any of the string.






Re: Visual C++ General Problem opening a fiel using fopen_

rca_rodrigo

Let's reasume:

You have admin privileges so you don't have file access restriction. True.
The file does exist and nobody/nothing is keeping the file opened.
True
fopen() does not work for you either.
True

There is a small thing I would like to notice:
a) I don't know how big the serial_leido is - the small devil has replaced the size of the buffer ;).
Jajajaja. It was [ 6 ] but Microsoft decided to change it (I think this machine thinks we are in messenger) jajajaaja .
b) I assume you commented out the err variable checking just to let it work.
The er variable is (in the inside) an integer. When I compile i use the watch and the evaluate options and the values that are shown are always the same: "2".
c) I don't have 2005 at home, but if you can c/p the main file for me, so I can take a look at it (sorry for such big delay in the response. home stuff...). It's ok Greg. After my last post i went to bed 'cause I was really tired. (11:00 pm). I'm kind of jelaous about my code to post it on public. Its very simple written but it was my creation. What I can do I sent it direcly to you by mail. Here It will take more than 700 lines.

Im sorry to bother you with my stuff.

r c a _ r o d r i g o @ h o t m a i l . c o m





Re: Visual C++ General Problem opening a fiel using fopen_

rca_rodrigo

Thanks for your help.

The problem was in the path: At the beginnig i was sending this adress:

err=fopen_s(&archivo, "d:\\windows\\data_pin.bin","r");

and the data_pin.bin was in the c driver. That was i got the "2" in the error variable

Yes you are right (my skills in programing are from 10 years ago and now in use them again)

So under this structure it WOKRKS OK:

if (err==0) then /* if err==0 No error was generated and then the file was opened */

start to operate

else

show the msg "this is an unregistered copy of this program and is not gonna operated. If u want a licence ...."

When i was compiling, i was verifying the value of each argument, and when i look at the *FILE fp pointer value i got this:

+ fp 0x10311D60 { _ptr=0x00000000 _cnt=0 _base=0x00000000 ...} _iobuf*


0x1011d60 is the adress and the data into the braces{} i thought it was the info from the file but no.. that's why I got the idea that the *fp pointer was not opened.

Thanks Greg and Thanks U Simple Samples.

This is the link that i recommend to check out the values of error (er variable) :

ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.es/dv_vccrt/html/adbec641-6d91-4e19-8398-9a34046bd369.htm

If you cant' upload that page this is what is inside:

These global variables hold error codes used by the perror and strerror functions for printing error messages. The more secure functional versions listed in this table should be used in place of their global variable.

Global Variable Functional Equivalents

_doserrno

_get_doserrno, _set_doserrno

_errno

_get_errno, _set_errno

These variables are declared in STDLIB.H as

extern int _doserrno; 
extern int errno; 
extern char *_sys_errlist[ ];
extern int _sys_nerr;

Remarks

errno is set on an error in a system-level call. Because errno holds the value for the last call that set it, this value may be changed by succeeding calls. Always check errno immediately before and after a call that may set it.

On an error, errno is not necessarily set to the same value as the error code returned by a system call. For I/O operations only, use _doserrno to access the operating-system error-code equivalents of errno codes. For other operations the value of _doserrno is undefined.

Each errno value is associated with an error message that can be printed using perror or stored in a string using strerror. perror and strerror use the _sys_errlist array and _sys_nerr, the number of elements in _sys_errlist, to process error information.

Library math routines set errno by calling _matherr. To handle math errors differently, write your own routine according to the _matherr reference description and name it _matherr.

All errno values, defined as predefined constants in ERRNO.H, are UNIX-compatible and are listed below. Only ERANGE, EILSEQ and EDOM are specified in the ANSI standard.

Constant System error message Value

EPERM

Operation not permitted

1

ENOENT

No such file or directory

2

ESRCH

No such process

3

EINTR

Interrupted function

4

EIO

I/O error

5

ENXIO

No such device or address

6

E2BIG

Argument list too long

7

ENOEXEC

Exec format error

8

EBADF

Bad file number

9

ECHILD

No spawned processes

10

EAGAIN

No more processes or not enough memory or maximum nesting level reached

11

ENOMEM

Not enough memory

12

EACCES

Permission denied

13

EFAULT

Bad address

14

EBUSY

Device or resource busy

16

EEXIST

File exists

17

EXDEV

Cross-device link

18

ENODEV

No such device

19

ENOTDIR

Not a directory

20

EISDIR

Is a directory

21

EINVAL

Invalid argument

22

ENFILE

Too many files open in system

23

EMFILE

Too many open files

24

ENOTTY

Inappropriate I/O control operation

25

EFBIG

File too large

27

ENOSPC

No space left on device

28

ESPIPE

Invalid seek

29

EROFS

Read-only file system

30

EMLINK

Too many links

31

EPIPE

Broken pipe

32

EDOM

Math argument

33

ERANGE

Result too large

34

EDEADLK

Resource deadlock would occur

36

EDEADLOCK

Same as EDEADLK for compatibility with older Microsoft C versions

36

ENAMETOOLONG

Filename too long

38

ENOLCK

No locks available

39

ENOSYS

Function not supported

40

ENOTEMPTY

Directory not empty

41

EILSEQ

Illegal byte sequence

42

STRUNCATE

String was truncated

80





Re: Visual C++ General Problem opening a fiel using fopen_

Simple Samples

I spent a lot more time on that than I should have, but at least I helped; I hope I helped.




Re: Visual C++ General Problem opening a fiel using fopen_

dfranz

pwede paturo poh ng c programming language

pwede henge codes ng loop ng series na 100111000011111000000

salamat poh