|
Why are you using GetActiveWindow rather than GetActiveView ? And what is the exact text of the error you receive and where does it occur?
|
|
|
|
|
Dear Richard
Firstly "HAPPY NEW YEAR"
Many thanks for your help, I was engaged with the CMyDoc error, finally I found it, simply it was redefinition of CMyDoc header in 2 different classes that I never guessed such mistake.
Best Regards
|
|
|
|
|
Glad you found it. Happy New Year (as much as it can be) to you also.
|
|
|
|
|
|
|
Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^]
this is the question. my code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
std::string s[t];
for(int i=0;i<=t-1;i++)
{
string st;
getline(cin,st);
int l=(int)st.length();
string s2="",s3="";
for(int i=0;i<=l;i++)
{
if(st[i]==' '||st[i]=='\0')
{
s3=s2+" "+s3;
s2.clear();
}
else
{
s2+=st[i];
}
}
s[i]=s3;
}
for(int i=0;i<=t-1;i++)
cout<<s[i]<<endl;
return 0;
}
the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?
|
|
|
|
|
for(int i=0;i<=l;i++)
Try changing to
for(int i=0;i<l;i++)
You're index is going out of bounds on the st variable 'st[i]', when i is equal to l.
Also, having nested for loops with the same index variable name is tough to read.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
After the following line
cin>>t;
there are still charactes in the input buffer, hence, the next call to getline gets them.
A commont way to deal with this issue is using cin.ignore (see, for instance What is the use of cin.ignore() in C++?[^]).
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Have you tried stepping through the code using a debugger? Doing so will let you know in a hurry what is wrong.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
In addition to other answers.
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
std::string s[t];
for(int i=0;i<=t-1;i++)
{
string st;
getline(cin,st);
int l=(int)st.length();
string s2="",s3="";
for(int i=0;i<=l;i++)
{
if(st[i]==' '||st[i]=='\0')
{
s3=s2+" "+s3;
s2.clear();
}
else
{
s2+=st[i];
}
}
s[i]=s3;
}
for(int i=0;i<=t-1;i++)
cout<<s[i]<<endl;
return 0;
}
Indentation style - Wikipedia[^]
Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Hello,
I am trying to draw an image (2nd image) over the other image (existing image file - 1st image) using gdi+ bitmap and graphics and then saving (overwriting) the same 1st image file. But saving is not getting done, when i use GetLasError(), it shows error message,
"The process cannot access the file because it is being used by another process"
Here is the code what i am doing.
<pre>Gdiplus::Bitmap *gpBitmap = new Gdiplus::Bitmap(mImagePath1); Gdiplus::Graphics *gr = Gdiplus::Graphics::FromImage(gpBitmap);
Gdiplus::Bitmap img(mImagePath2); Gdiplus::Bitmap* pThumbnail = static_cast<Gdiplus::Bitmap*>(img.GetThumbnailImage(32, 32, NULL, NULL));
gr->DrawImage(pThumbnail, newWpos, newHpos);
Gdiplus::Status stat = gpBitmap->Save(mImagePath1, pClsid);
I tried making copy also, but same issue.
please suggest.
Regards,
Gopi.
|
|
|
|
|
Your file mImagePath1 is still open to the Bitmap object. You should use a different path to save the new image.
|
|
|
|
|
Thank you Richard.
This helps me to solve my issue.
Regards,
Gopi.
|
|
|
|
|
In Visual C++ 2012 I have written this code:
strstream& operator>>(strstream& Stream,CComponent* pCmp){
..code
}
std::strstream read_file((char*)filbuf,file_size,ios_base::out);
pComp=new CComponent;
read_file>>pComp;
This works perfectly. But when I run it on my newly installed Visual C++ 2019 it compiles all right, but the output is wrong.
I see that std::strstream is deprecated, but it should still function. If I try to put std::stringstream instead, it will not accept the >> operator.
|
|
|
|
|
Haakon S. wrote: it compiles all right, but the output is wrong. You need to explain what that means.
|
|
|
|
|
Ok, some details. Everything seems to be identical when I check the debug data. The text file is referenced in the same way by filebuf.
The first member of CComponent is specified as WORD m_nNo and the this value in the first component is 56.
I put some test code on top of my function to try to find out what is going on.
std::strstream& operator>>(std::strstream& Stream,CComponent* pCmp){
char txt[4];
Stream >> txt;
.
.
.
}
The watch window displays the following output from the new C++ version:
- txt 0x00cfd5fc "" char[4]
[0] 0 '\0' char
[1] -52 'Ì' char
[2] -52 'Ì' char
[3] -52 'Ì' char
From my older version I get the correct output:
- txt 0x0055e25c "56" char[4]
[0] 53'5' char
[1] 54'6' char
[2] 0 '\0' char
[3] -52 'Ì' char
It seems that the extraction operator >> simply does not work anymore. In that case, what is the best alternative for reading data from a text file. If I replace strstream with stringstream I get the message that stringstream does not have a >> operator.
modified 23-Dec-20 4:35am.
|
|
|
|
|
If you are reading from a file then you should be using one of the fstream classes.
|
|
|
|
|
Looks to me like it is the *old* version that wasn't working: your code overwrites your text variable, so why would you expect it to still hold the old value? Or maybe it is just that the VS2012 debugger didn't show these contents properly because it didn't recognize that you were writing to it.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
First, stringstream is not strstream. You may need to specifically use ostringstream instead. But anyway, like Richard already stated, you should be using fstream, or ofstream.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Hi.
Thanks for helping.
ifstream accepts the >> operator and get() function.
std::ifstream read_file((char*)filbuf,file_size,ios_base::out);
.
.
pComp=new CComponent;
read_file>>pComp;
This function is called:
ifstream& operator>>(ifstream& Stream,CComponent* pCmp){
Stream>>pCmp->m_nNo;
Stream.get(pCmp->m_formula,15);
.
.
}
Everything works fine, except that nothing is actually read.
Regards, Haakon.
|
|
|
|
|
Haakon S. wrote: Everything works fine, except that nothing is actually read
Well both of those statements cannot be true.
|
|
|
|
|
Ah, yes, you're right, of course I meant ifstream, not ofstream.
But this statement shouldn't compile:
std::ifstream read_file((char*)filbuf,file_size,ios_base::out);
See ifstream::ifstream - C++ Reference[^] : none of the constructors match the one you're calling. Specifically no constructor does have three arguments, and none expect anything like a file size. (and why should they?)
Furthermore, I wonder what that variable filebuf is, and why it needs to be casted to char* ? A typecast is almost always an indication of an incorrect implementation, and in this case I think you are passing the wrong parameter! The first parameter, if you pass any, should always be the file name. What is the name of the file you want to read? You should pass it here.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Hi.
It does compile. But you are right. When the read file is invoked, it does not take filbuf along with it, I can see that in the debugger. I have to figure that out.
Anyway, thanks alot for your hjelp. Have a Merry Christmas. During the holiday I will try to fix the problem. If I do, I will report back.
Best regards, Haakon.
|
|
|
|
|
Hi.
I have managed to make things work, albeit not in the way I want to.
The data to be read is in a file called datf.txt. Having this file in the same directory as the exe file and simply writing
std::ifstream read_file("datf.txt");
is working. However, I want to have the data incorporated as part of the exe file. I have therefore included the CResourceTextFile class by Hans Dietrich. In the progarm resource:
IDU_COMP_FILE TEXT "datf.txt"
and further
CResourceTextFile rf;
rf.Open(NULL, _T("IDU_COMP_FILE"));
TCHAR* filbuf=rf.GetTextBuffer();
std::ifstream read_file(filbuf);
does not work anymore. It did for sstream in the older version of C++ but not for ifstream in the latest.
|
|
|
|
|
I think your original idea in your first post... std::stringstream was probably the best way.
You just needed to add a istream operator >> to handle the CComponent. Something like this:
class CComponent
{
private:
std::string something;
public:
CComponent() noexcept = default;
friend std::istream& operator >> (std::istream& in, CComponent& component);
};
istream& operator >> (istream& in, CComponent& c)
{
in >> component.something;
return in;
} Which could then be used like this:
const std::string test_haakon = "Lorem ipsum dolor sit amet";
CComponent test_component;
std::stringstream test_string_stream(test_haakon.c_str());
test_string_stream >> test_component; Merry Christmas to you and your family.
Best Wishes,
-David Delaune
UPDATE:
I really hate answering these modern C++ questions because it takes a lot more effort. I shouldn't have said that it was 'probably the best way' because if you use the code above it will make a copy of the data three times.
If you are able to use C++17 in your project then you will be able to avoid one of those copies by deriving a class from std::basic_streambuf as such:
class CComponent
{
private:
std::string something;
public:
CComponent() noexcept = default;
friend std::istream& operator >> (std::istream& in, CComponent& component);
};
template<typename T = CHAR, typename CT = std::char_traits<T> >
class make_stream_buffer_no_copy : public std::basic_streambuf<T, CT>
{
public:
make_stream_buffer_no_copy(CHAR* p, size_t len)
{
basic_streambuf<T>::setg(p, p, p + len);
basic_streambuf<T>::setp(p, p + len);
}
std::basic_stringstream<T> get_stream()
{
return std::basic_stringstream<T>(basic_streambuf<T>::pbase(), ios_base::in | ios_base::out);
}
};
istream& operator >> (istream& in, CComponent& c)
{
in >> c.something;
return in;
} Which you would use like this:
CHAR p2[] = "Lorem ipsum dolor sit amet";
CComponent test_component2;
make_stream_buffer_no_copy no_copy(p2,strlen(p2));
no_copy.get_stream() >> test_component2;
You can avoid ALL copying if you are willing to extend your CComponent class.
modified 26-Dec-20 11:03am.
|
|
|
|
|