|
I have a template function that returns a variable of type T.
Here is the signature of that function:
T GetData();
When everything goes well, GetData() perform some tasks then returns a valid variable of type T.
Sometimes, for different reasons, the function cannot return a valid variable of type T. In these cases, I would have like to return NULL to notify the calling function that GetData() could not 'get' any 'data'.
Here is my problem:
I cannot return NULL since type T could be a int, a CString, a MyCar, a pointer or anything.
So I declare a local variable of type T inside the GetData() function, load it with stuff when possible, and return this local variable at the end of the function.
The problem is now that sometimes, my software crash with an exception telling me that I'm using a local variable that has not been initialized.
Also, I cannot just 'initialize' it as I would with any other kind of variables since this one is of type T, it could be anything.
Anyone has an idea how to get around this problem?
Thanks!
Benjamin Racette
CAE software developer
racette@cae.com
|
|
|
|
|
Throwing an exception ? That will avoid a lot of troubles I think.
|
|
|
|
|
Cedric Moonen wrote: Throwing an exception
That is pure genius!
Of course!
I can't believe I didn't think about it.
Thanks a lot!
Benjamin Racette
CAE software developer
racette@cae.com
|
|
|
|
|
Hello,
does anyone has any idea to get current url from firefox?
thanks
|
|
|
|
|
Does it have a COM interface?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I think mozilla firefox doesnt have com interface, that is why it is hard to get url from mazilla
|
|
|
|
|
Gofur Halmurat wrote: I think mozilla firefox doesnt have com interface, that is why it is hard to get url from mazilla
You should ask same question in MOZILA forums, you will got lot of reply there, if you have asked about IE here, you will getting lots of reply here!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Hello
It uses Xpcom technology(cross plotform technology) it is like ms com, but little bit diffirent, have u ever used xpcom in your project before?
thanks
|
|
|
|
|
I was wondering if explicitly casting a variable of type int
to a variable of type char, is identical to assigning an int to a char variable?
Again, Tnx
Tzumer
|
|
|
|
|
e.tzumer wrote: I was wondering if explicitly casting a variable of type int
to a variable of type char, is identical to assigning an int to a char variable?
could you show an example?
|
|
|
|
|
c - char
i - integer
<br />
c = (char)i;<br />
VS<br />
c = i;<br />
Tzumer
|
|
|
|
|
e.tzumer wrote: if explicitly casting a variable of type int
to a variable of type char, is identical to assigning an int to a char variable?
Both of your examples are assignment operations. One is an implicit cast the other an explicit cast. Perhaps you should do some reading on the subject[^].
|
|
|
|
|
is the standard input file (stdin) always the keyboard?
is the standard output file (stdout) always the screen?
is the standard error file (stderr) always the screen?
Thanks
Tzumer
|
|
|
|
|
no, that are their defaults.
you can modify a program's stdin/stdout/stderr in many ways.
e.g. change stdout by invoking it from a DOS prompt with a > as in
prompt> mycommand /options > outputfile
that is how you can "dir" to a file.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
|
stdin and stdout can both be redirected. I'm not sure if stderr can or not.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
command 2> error.log
Search Windows Help (Start Menu -> Help) for "Using command redirection operators".
The numbers before > are:
STDIN 0 Keyboard input
STDOUT 1 Output to the Command Prompt window
STDERR 2 Error output to the Command Prompt window
Florin Crişan
|
|
|
|
|
I have the following code to read data from a list of files. The code read the first file and read the data correctly. I used break point debug and found at "fclose(f_ptr2)" giving error: "Access Violation"
Please help and many thanks to Gurus.
FILE *f_ptr2;
//
for (int ii=0;ii
|
|
|
|
|
You really need to debug this yourself if you can't post relevant code.
Things to check for:
1) fopen() returning NULL
2) Any of the fscanf() calls returning 0, EOF, or some other unexpected value
3) buffer overrun(s) in any of the fscanf calls - there's safe versions of fscanf() to help
catch/prevent these
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I checked that the program open the fist file without problem in the loop and the program also read the data of the first file correctly, but it can not close the file - giving error message like: "Access Violation or memery can not be read"
I need the program to close the file and open the next file in the array of the file using the same file pointer p_ftr2.
Thanks a lots for looking onto the problem.
|
|
|
|
|
fopen() may fail; it will indicate that by returning NULL.
all subsequent file operations must be qualified by f_ptr2!=NULL
and yes, it is a pitty fclose does not perform a zero test internally...
if this does not solve it, please show all the relevant code, that is actual code,
and please please use PRE tags so it is readable.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Please check the code for me. Thanks a lots.
void CBlast_vib_procDlg::Next()
{
FILE *f_ptr1;
FILE *f_ptr2;
const int MAX=15;
CBlast_vib_procDlg rr;
float north[2000];
float east[2000];
float elv[2000];
CString fname[2000];
char buffer[MAX];
UpdateData();
if(m_outputFileName == "" )
{
MessageBox("All file names have to be typed in !");
rr.m_outputFileName = m_outputFileName;
rr.DoModal();
UpdateData(false);
}
#define BUFSIZE MAX_PATH
WIN32_FIND_DATA FindFileData;
LPTSTR DirSpec;
HANDLE hFind = INVALID_HANDLE_VALUE;
CString fileName[2000];
int i=0;
DirSpec = (LPTSTR) malloc (BUFSIZE);
DirSpec=TEXT("*.txt");
hFind = FindFirstFile(DirSpec, &FindFileData);
fileName[0]=FindFileData.cFileName;
while (FindNextFile(hFind, &FindFileData) != 0)
{
i+=1;
fileName[i]=FindFileData.cFileName;
}
int nfile=i;
FindClose(hFind);
for (int ii=0;ii<nfile;ii++)
{
if((f_ptr2 = fopen(fileName[ii],"r")) == NULL) {
MessageBox("output file open problems !");
}
fscanf( f_ptr2,"%s %s %s\n",buffer,buffer,fname[ii].GetBuffer(MAX_PATH));
fname[ii].ReleaseBuffer();
fscanf( f_ptr2,"%s %s %s %s %s\n",buffer,buffer,buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %s\n",buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %s\n",buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %s %s\n",buffer,buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %s %s\n",buffer,buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %f\n",buffer,buffer,&elv[ii]);
fscanf( f_ptr2,"%s %s %s %s\n",buffer,buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %s %s\n",buffer,buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %s %s\n",buffer,buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %s %s %s\n",buffer,buffer,buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %s\n",buffer,buffer,buffer);
fscanf( f_ptr2,"%s %s %f\n",buffer,buffer, &east[ii]);
fscanf( f_ptr2,"%s %s %f\n",buffer,buffer,&north[ii]);
fclose(f_ptr2);
}
if((f_ptr1 = fopen(m_outputFileName,"w")) == NULL) {
MessageBox("output file open problems !");
rr.m_outputFileName = m_outputFileName;
rr.DoModal();
UpdateData(false);
}
fprintf (f_ptr1, "%i\n", nfile);
char * tittle = "number of monitors";
fprintf (f_ptr1, "%i\n", nfile);
char * tittle_item = "fineName Easting(m) Northing(m) elv(m)";
fprintf (f_ptr1, "%s\n", tittle_item);
for (ii=0;ii<nfile;ii++)
{
fprintf (f_ptr1, "%s %f %f %f\n", fname[ii],east[ii],north[ii],elv[ii]);
}
fclose( f_ptr1);
CDialog::OnOK();
}
|
|
|
|
|
Hi,
[Added] ignore this reply, it is wrong! [/added]
there is a problem in fileName[i]=FindFileData.cFileName;
this line does NOT copy the filename, it copies the pointer to the cFileName field
in your unique FindFileData struct, hence in all iterations it will point to the
buffer containing the last data written into it.
If you want to hold all the different filenames, you must copy them, which you could
do with strcpy() or strncpy().
BTW: your NULL test shows a MessageBox but then continues the program execution,
which will result in failure of fscanf and/or fclose. The right way to handle this is
to have an if-then-else with all file actions (fscanf/fclose) in one part, and the
error handling (I do not really like MessageBox !) in the other part.
-- modified at 13:39 Monday 26th November, 2007
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Luc Pattyn wrote: there is a problem in fileName[i]=FindFileData.cFileName;
this line does NOT copy the filename, it copies the pointer to the cFileName field
in your unique FindFileData struct, hence in all iterations it will point to the
buffer containing the last data written into it.
If you want to hold all the different filenames, you must copy them, which you could
do with strcpy() or strncpy().
Not necessary at all. The statement is correct, since CString has an assignment operator that internally does the copying. fileName[0] , fileName[1] , fileName[2] , etc will each contain different data.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
My mistake, was looking at it as a C function.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|