Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Null CString?

Posted on 1999-07-22
16
Medium Priority
?
1,249 Views
Last Modified: 2012-06-27
I saved a full path name of a file into a txt_file,when I get the name from the file and put into a CSting object, the value is like "C:\somefile.txt", then I judge whether the object is null,but it returns TRUE.How should I do?
0
Comment
Question by:ylix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +2
16 Comments
 
LVL 4

Expert Comment

by:nil_dib
ID: 1200779
>>object is null ...
which object, the CString ?

>>How should I do?
What ??

0
 
LVL 1

Expert Comment

by:kentkt
ID: 1200780
how do you judge whether it is null? calling CString::IsEmpty()? or compare a CString object with NULL with ==? Also, you might need to change the path to "c:\\somefile.txt" because '\' is the escape character
0
 

Author Comment

by:ylix
ID: 1200781
>>Yes,the CString object.It has a value,but I call IsEmpty() function,it returns TRUE.
>>I want to know how this happens,and how to know whether the CString object is NULL.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:nil_dib
ID: 1200782
Are you on Unicode?
Look at CString::GetBuffer() ...
post your code.

0
 

Author Comment

by:ylix
ID: 1200783
(Reply kentkt)
>>I use CString::IsEmpty().
I get the file name by using CFileDialog::GetPathName(),and
 save it to file using fprintf(),retrive it using fscanf().
Did I use them wrongly?

0
 

Author Comment

by:ylix
ID: 1200784
(Reply nil_dib)
No,I don't on Unicode.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1200785
A CString object (any object) cannot be NULL. Either its a valid object or not an object at all.
Seems you are confused with using pointers:

CString s;   // s is a complete object, never NULL
CString* pS; // pS is a pointer, can be NULL

0
 

Author Comment

by:ylix
ID: 1200786
(reply KangaRoo)
sorry,I made a mistake.I should say "empty string",because
I use CString,not CString*.
0
 
LVL 4

Expert Comment

by:nil_dib
ID: 1200787
post your code
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1200788
Can you at least post the fscanf() line?
Do you call CString::GetBuffer()?
Do you release with CString::ReleaseBuffer()?

If we know how the data gets into the CString, we can tell you what value should be there.
0
 

Author Comment

by:ylix
ID: 1200789
>>
Sorry,last night I didn't use the Internet.
My code is below:
...
CFileDialog openfile(true,...);
CString filname;
FILE *savefile;
if(openfiledlg.domodal()==idok)
{
    filename=openfiledlg.getpathname();
    savefile=fopen("c:\sometxt.txt","w");
    fprintf(savefile,"%s",filename);
}
...
FILE *getfile;
CString savedfilename;
getfile=fopen("c:\sometxt.txt"."r");
fscanf(getfile,"%s",savedfilename);
if(savedfilename.isempty())
{
  //do something
}
else
{
   //do other thing
}
...
I didn't use the CString::GetBuffer() function.

0
 
LVL 6

Accepted Solution

by:
Triskelion earned 400 total points
ID: 1200790
Try it with the GetBuffer({LONGEST_EXPECTED_LENGTH})
Then call ReleaseBuffer(-1);
Then check the value.
Turn on warning level 4 and some of your problems will disappear (after the errors and warnings are corrected :-))
0
 

Author Comment

by:ylix
ID: 1200791
>>(To Triskelion)
Thank you,I'll try this method later(Because I have not the vc env. now).
And would you tell me whether you tested this method and it's ok?Thanks.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1200792
//Yes, I tried it and it works.
//I made a small console app to test it, not a full WinApp
void main(void)
{
      CString filename="clarence.txt";
      CString strSomeTextName="sometxt.txt";
      FILE * savefile=fopen(strSomeTextName,"wb");
      fprintf(savefile,"%s",filename);
      fclose(savefile);
      //
      CString savedfilename;
      FILE * getfile=fopen(strSomeTextName,"rb");
      fscanf(getfile,"%s",savedfilename.GetBuffer(128));
      savedfilename.ReleaseBuffer();
      fclose(getfile);
      //
      if(savedfilename.IsEmpty())
            {
            printf("empty");
            return;
            }
      printf(savedfilename);
}
0
 

Author Comment

by:ylix
ID: 1200793
Enn...I have a problem,if you change the value of strSomeTextName to "c:\\sometxt.txt",will it work as before?
I doubt about it.Thank you.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1200794
Yes, it will work.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question