|
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.
|
|
|
|
|
This was the most appropriate place that I saw to post a C build system question that I saw so, sorry if there is a better place that I missed. Anyhow, I have run into an issue running configure where the linker is not finding the library that I'm specifying. I'm down to guesses so, any direction would be helpful. The configure command that I've constructed is below but, basically, I'm using LDFLAGS to point to the .so file that resides in a non-standard location. I'm using CFLAGS to point to the includes directory where the header files reside and LIBS to indicate the library that I'm using. There is another directory /usr/local/bin that contains three binaries that I don't know how to specify with the configure command. I'm trying to build GNU TLS from source after installing Nettle from the source files as well. Thank you in advance!
./configure CFLAGS="-I/usr/local/include/nettle/" LDFLAGS="-L/usr/local/lib64" LIBS="-lnettle"
|
|
|
|
|
Hi,
I think on Linux the LD_LIBRARY_PATH environment variable is used to find shared libs. You might be looking for ldconfig and /etc/ld.so.conf if you are on Linux.
|
|
|
|
|
You don't say whether make completes successfully or not. The compiler flags given tell the compiler where to find the pieces to buld the program. If the compile has completed successfully, but when you try to run the program you get a message
"error while loading shared libraries: <libname.so>: cannot open shared object file: No such file or directory then the problem is at runtime, not compile time. As Randor notes, you can tell the link-loader where to find the library with LD_LIBRARY_PATH
k5054@localhost$ LD_LIBRARY_PATH=/usr/local/lib64 myprog arg1 arg2 sets LD_LIBRARY_PATH for the given command. To set for a single session do
k5054@localhost$ export LD_LIBRARY_PATH=/usr/local/lib64
k5054@localhost$ myprog arg1 arg2 You can add that to your shell's .profile file. and it will be set every time you log in. If you want to set this up permanently for all users ont the system, then as root create a file /etc/ld.so.conf.d/local.conf containing the single line
/usr/local/lib64 Now, as root, run ldconfig , and you should be able to run your program from any login, without needing to set LD_LIBRARY_PATH in your .profile or per session/per command.
you can check what libraries are being loaded using the ldd command
<
k5054@localhost$ ldd /bin/bash
linux-vdso.so.1 (0x00007ffebf5f6000)
libtinfo.so.6 => /lib/x86_64-linux-gnu/libtinfo.so.6 (0x00007f7ec6539000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7ec6533000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7ec6341000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7ec66b5000)
If the link-loader can't find a library, it will show
libsomelib.so => not found
Keep Calm and Carry On
|
|
|
|
|
Hi,
I am trying to learn factory design pattern in c++, writing a program with separate header files and their class implementations in .cpp files. But i am facing problem. I was trying the ShapeFactory from where we implement Circle and Rectangle.
invalid new-expression of abstract class type 'Circle'
invalid new-expression of abstract class type 'Rectangle'
But i am getting these errors and also few other errors. Can someone please help on this.
Regards
Litu
|
|
|
|
|
"new" ... you can't instantiate an "abstract" class; you need to subclass it first or use an existing derived class.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
|
i wanna ask, what is the best environment for learning C++ for windows application? e.g. Win32 API, Qt, etc...
also, could anyone suggest what's the best e-books for references?
|
|
|
|
|
When (really) learning, they usually start you with Notepad and a command line compiler. The "environment" can suck up most of your time (and disk space).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
It depends on what you are trying to learn. If you know C++ then Win32 should be fairly easy. But if you do not know C++ then that is what you need to start with.
Here is a starter for C++: Learn C++[^]
And one for Windows: EFNet #Winprog[^]
|
|
|
|
|
I am compiling some old legacy code with MFC. For this line in cmath.h I get an error:
NODISCARD _Check_return_ inline float round(_In_ float _Xx) noexcept /* strengthened */ {
return _CSTD roundf(_Xx);
}
With first round statement highlighted I get this message:
C2062 type'int' unexpected
|
|
|
|
|
You should inspect the calling code, I believe (the compiler provides full information with the error message).
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
I don't understand that. It is a compile error in a core library file. I am not allowed to change it. Why would the calling function have anything to say?
|
|
|
|
|
Quote: Why would the calling function have anything to say?
Quote Selected Text With very high probability the calling code is wrong (possibly making incorrect assumptions on the called function, it passes the wrong type). Maybe a previous implementation of the library had relaxed checks on calling code correctness.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Use your development environment (VS or whatever) to check that there is a #define for each the following:
NODISCARD
_Check_return_
In
_CSTD
And if you're using C instead of C++, noexcept could be the problem. But I can't say, because I only write C++.
EDIT: type 'int' unexpected suggests that the caller is passing an int , not a float .
|
|
|
|
|
There are plenty of _Check_return_ statements that work.
If there is something wrong with the calling function, I should get a compile error when compiling the cpp file where the function is called? This is an error from inside the include file cmath.h.
Three errors reported, underscore is the highlighted error indication.
Line 210
_NODISCARD _Check_return_ inline float round(_In_ float _Xx) noexcept /* strengthened */ {
return _CSTD roundf(_Xx);
}
Line 446
_NODISCARD _Check_return_ inline long double round(_In_ long double _Xx) noexcept /* strengthened */ {
return _CSTD roundl(_Xx);
}
Line 663
_GENERIC_MATH1(round)
It seems I am not allowed to alter any include file, so I can not experiment with commenting the functions out, etc.
|
|
|
|
|
Check if round is also defined somewhere else, and check that _GENERIC_MATH1 is actually defined. Beyond that, I don't know.
|
|
|
|
|
I would be inclined to look at the preprocessor output to see what all of those non-standard "types" are.
"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
|
|
|
|