BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FILE Problem

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Marco mliorniNoMail.com
Guest





PostPosted: Mon Mar 07, 2005 10:09 am    Post subject: FILE Problem Reply with quote




Hi everyone,
i use "File *My_file" to create, save and read file in
my applycation but sometimes it doesn't work and i can't
understand why.
Is there some Win-XP settings that could give problems?

Thanks in advanced.
Marco
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Mon Mar 07, 2005 10:27 am    Post subject: Re: FILE Problem Reply with quote



"Marco" mliorni"NoMail.com wrote:

Quote:
i use "File *My_file" to create, save and read file in
my applycation but sometimes it doesn't work and i can't
understand why.

What do you mean by that? This is the newsgroup section for questions
relating to the Windows API and that means using CreateFile() to create
a file. If it were the .language section than "FILE *" would be
meaningful but "File *My_file" isn't.

I suggest you post a short, preferably compilable example.

Remember that to get meaningful help you have to ask a meanginful
question :)

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Marco
Guest





PostPosted: Mon Mar 07, 2005 11:04 am    Post subject: Re: FILE Problem Reply with quote




This is an example of my code:

//-------------------------------------------------------------
void TCompCodici::SaveFile()
{
FILE *file = fopen (".\Cfg\CompCod.dat", "w");
if (file == NULL)
{
ShowMessage("Error! Can't open file CompCod.dat");
return;
}
fprintf (file, "[Costanti compilatore]n");
fprintf (file, "NrCodici=%dn",FLista.Count());

for (int i=0; i fprintf (file, "%d=%sn",FLista[i]);

fclose (file);
}
//-------------------------------------------------------------void TCompCodici::OpenFile()
{
char szBuff[121];
int i = 0;
FILE *file = fopen (".\Cfg\CompCod.dat", "rt");
if (file == NULL)
{
ShowMessage("Error! Can't open file CompCod.dat");
return;
}
while (!feof(file))
{
fgets(szBuff, 120, FMsgFile);
FLista[i] = szBuff;
i++;
}
}
//-------------------------------------------------------------

FLista is a stringlist.
My problem is that if i try to open file on my PC sometimes
error occours but if i copy all my folder on another PC all
work correctly (always).
Could WinXP (API or somethingelse) give the problem?

Thanks.
Marco

Back to top
Hans Galema
Guest





PostPosted: Mon Mar 07, 2005 11:13 am    Post subject: Re: FILE Problem Reply with quote

Marco wrote:

Quote:
FILE *file = fopen (".\Cfg\CompCod.dat", "w");

FILE *file = fopen (".\Cfg\CompCod.dat", "rt");

My problem is that if i try to open file on my PC sometimes
error occours

Please explain exactly what kind of error. Why must we guess ?

But I think that the file can not be found because
you use relative paths.

Better use absolute paths.

Hans.

Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Mon Mar 07, 2005 11:31 am    Post subject: Re: FILE Problem Reply with quote

Marco wrote:

Quote:
My problem is that if i try to open file on my PC sometimes
error occours but if i copy all my folder on another PC all
work correctly (always).
Could WinXP (API or somethingelse) give the problem?

FILE *file = fopen (".\Cfg\CompCod.dat", "w");
if (file == NULL)
{
ShowMessage("Error! Can't open file CompCod.dat");
return;
}

Oh dear, oh dear - I see so many programs that handle file I/O like
that. Even Borland are guilty of that kind of sloppy I/O
implementation. I/O operations usually provide a mechanism for
obtaining an error code and if they don't IMO programmers shouldn't use
them :)

In this case Borland's help seems to be a bit poor. The help for
fopen() doesn't bother to mention 'errno'. This variable will contain
an error code from whatever runtime library function last failed. You
even get a textual message for a code by using it as an index into the
'_sys_errlist' array.

Alternatively if you are coding for Windows you can just use
GetLastError() - this should probably be your first port of call when
you get any kind of I/O or API related problems. If you are using the
VCL it can be combined with SysErrorMsg() to get a system error message
for a system error code.

If you aren't using the VCL you can use FormatMessage():

// From the help

LPVOID lpMsgBuf;

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);

// Display the string.
MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );

// Free the buffer.
LocalFree( lpMsgBuf );


Try changing your code to be:

FILE *file = fopen (".\Cfg\CompCod.dat", "w");
if (file == NULL)
{
ShowMessage("Error! Can't open file CompCod.dat:"+
SysErrorMsg( GetLastError() ) );
return;
}

And see what reason is given.

One other thing that I also dissapprove of is your use of a relative
path in a call to fopen(). It's okay (essential in fact) if you have a
collection of files that all relate to each other (such as a project
group) but then the path should be relative to a central file and
should be resolved into a fully qualified path internally at the first
opportunity. Several serious faults with the BCB IDE can be attributed
to Borland programmers using relative paths and not resolving them.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Marco
Guest





PostPosted: Mon Mar 07, 2005 11:58 am    Post subject: Re: FILE Problem Reply with quote


Hans wrote:

Quote:
But I think that the file can not be found because
you use relative paths.

Better use absolute paths.

Thank you Hans, absolute path resolve my problem.

HAndrue wrote:

Quote:
Oh dear, oh dear - I see so many programs that handle file I/O like
that. Even Borland are guilty of that kind of sloppy I/O
implementation. I/O operations usually provide a mechanism for
obtaining an error code and if they don't IMO programmers shouldn't use
them Smile

I'm terrible sorry, i'm not a really programmer
but i would want to become it.
From now I will write a better code, thank you very much.

Marco


Back to top
Wiljo
Guest





PostPosted: Mon Mar 07, 2005 12:00 pm    Post subject: Re: FILE Problem Reply with quote

Marco wrote:

Quote:
This is an example of my code:

FILE *file = fopen (".\Cfg\CompCod.dat", "w");
I also concur that you shouldn't use relative paths, unless you are absolutely

sure what the current directory is. But when your program is moved to another
directory in the future, your program might fail again!
Quote:
.....

for (int i=0; i fprintf (file, "%d=%sn",FLista[i]);

Here is another mistake, or probably a typo. The fprintf statement contains the

format "%d=%sn", specifying two additional arguments (one for %d and one for
%s). Only the latter one is given.

Another suggestion for the file reader:

Quote:
while (!feof(file))
{
fgets(szBuff, 120, FMsgFile);
FLista[i] = szBuff;
i++;
}

You can replace this by

while( fgets( szBuff, 120, FMsgFile ))
{
FLista[i] = szBuff;
++i;
}

The fgets function can be used to test for eof.
Or, because FLista is a string list, why don't you use the LoadFromFile member
function of the string list component itself?

Wiljo.

Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Mon Mar 07, 2005 12:17 pm    Post subject: Re: FILE Problem Reply with quote

Marco wrote:

Quote:
I'm terrible sorry, i'm not a really programmer
but i would want to become it.
From now I will write a better code, thank you very much.

My response was intended to be a lighthearted comment (as indicated by
the 'Smile' at the end) so I hope you haven't taken offense at it. As
regards becoming a real programmer you needn't worry. Plenty of real
programmers neglect to give appropriate diagnostic information to their
users. Sometimes they give too much but mostly they just don't give
enough.

In this case whoever wrote the help didn't do you any favours because
they neglected to mention 'errno'.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Marco
Guest





PostPosted: Mon Mar 07, 2005 12:52 pm    Post subject: Re: FILE Problem Reply with quote


Quote:
Andrue wrote:

My response was intended to be a lighthearted comment (as indicated by
the 'Smile' at the end) so I hope you haven't taken offense at it.

I haven't taken it like offense.
I like very much this "forum" for the
friendly atmosphere and i have interpreted your comment
like the comment of an expert friend.
Thank you for all.

Marco

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.