fstream and ostrstream is undefined - visual-c++

HI,
I am converting my project from vc6 to latest using vs 2010. I get problem on compiling my code
Error 931 error C2065: 'ostrstream' : undeclared identifier
1100 IntelliSense: identifier "fstream" is undefined
I have included the required files as told in Google
#if ! defined(_FSTREAM_)
#include <fstream>
#endif
#if ! defined(_STRSTREAM_)
#include <strstream>
#endif
When i press F12 on the fstream or ostrstream it takes to the respective files where these class are defined. Is there any other includes i have to do, i have been searching for this for long time with no luck :(
Thanks
Arvind

Add these to your library list:
#include <stdio.h>
using namespace std;

Forget all the preprocessor stuff; it's redundant at best (the files in questiion will have reinclusion guards) and at worst an error (you're assuming the #defines used, which are arbitary). Just use code like this:
#include <fstream>
#include <strstream>
Also note that the strstream header is deprecated. You should use sstream instead, but note the newer classed in this file word differently to the deprecated ones).

Without using namespace std; as its good practice.
#include<fstream>
std::fstream fileio;
Where the fileio is the object created by fstream. The fstream class needs to be created using the std keyword as it is part of the standard library.
Hence using : using namespace std; in the code makes it work.
NOW using using namespace std; can cause conflicts in shared libraries.
#include <fstream>
using namespace std;
fstream fileio;

What worked for me was a combination of two answer. After some trial and error, fstream squiggly red lines vanished when I added,
#include <fstream>
using namespace std;

Related

Unexpected end of file while looking for precompiler header file, when instrument my code through squish coco

error C1010: unexpected end of file while looking for precompiled
header. Did you forgot to add #include "stdafx.h" to your source file?
I have already added #include "stdafx.h" file in my .cpp file
#include "stdafx.h"
msmq()
{
int a;
enter code here
}
But when I used Squish coco for code instrumentation at that time they ignore this precompiler file and give me error.
I expect it to instrument my code successfully without giving C1010 error.
I already tried Precompiler Disable option, but it didn't work successfully
Yes, because of some space error in the header file.
Ensure that between #include and "Header file name" there is only one space.
#include "stdAfx.h" (2 spaces between #include and header file name)
If you give only one space instead of more then one then simply solve it:
#include "stdAfx.h" (only one space) it's work correctly..

Applying _CRT_SECURE_NO_WARNINGS only to a fragment of code

Is there a way to apply _CRT_SECURE_NO_WARNINGS only to s fragment of code? I tried the obvious
#define _CRT_SECURE_NO_WARNINGS
// some code here
#undef _CRT_SECURE_NO_WARNINGS
It seems it doesn't work.
The _CRT_SECURE_NO_WARNINGS flag is applied to the declarations of the relevant functions when they're included in your source code. You can't really turn them on or off on a per-call basis, it's an all-or-nothing setting. That's why the #define _CRT_SECURE_NO_WARNINGS directive must come before the #include directives that include the relevant function declarations.
As an alternative, you can bracket your calls with #pragma warning directives to turn off that specific warning:
#pragma warning(push)
#pragma warning(disable:4996)
printf("Look Ma, no warnings!\n");
printf("This is awesome!\n");
#pragma warning(pop)
// or for just one call,
#pragma warning(suppress:4996)
printf("We'll look the other way just this once.\n");

#define new DEBUG_NEW in stdafx.h

I've noticed that several of our projects do the whole
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
thing in their stdafx.h.
Will this memory leak detection work as intended when specified in the stdafx?
Don't put it in stdafx.h. Doing so can give you undesired side effects.
Here's why.
In most cpp files, you have something like this:
#include "stdafx.h"
#include <AcmeHeader.h>
#include "MyHeader.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
Note that the redefinition of new is explicitly supposed to happen after all headers are included. If you define DEBUG_NEW in stdafx.h, then that definition will also be applied to AcmeHeader.h and MyHeader.h, which can cause problems with headers that try to redefine operator new.
I've also run into cases where I've wanted to remove the redefinition of "new" for just one or two files, but that's a rare situation.

winsock deprecated no warnings

I am trying to create a UDP multicast socket program using VS2015 (C++ console application).
I got the following error,
Error C4996 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings mulitcast_listener
I tried to add _WINSOCK_DEPRECATED_NO_WARNINGS symbol to my project settings via "Project"->"Properties"->"Configuration properties"->"C/C++"->"Preprocessor"->"Preprocessor definitions"
.. But still it says the same.
And then I tried to add symbol above #include "stdafx.h"
like
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1
and then No(/sdl-) on "Project"->"Properties"->"Configuration properties"->"C/C++"->General->SDL checks
now I get a error message saying
Warning C4603 '_WINSOCK_DEPRECATED_NO_WARNINGS': macro is not defined or definition is different after precompiled header
Finally I tried to implement
inet_pton(AF_INET, HELLO_GROUP, (PVOID *)(&mreq.imr_multiaddr.s_addr));
instead of
mreq.imr_multiaddr.s_addr = inet_addr(HELLO_GROUP);
I need to understand why the error didn't resolved even after adding the _WINSOCK... macro.
Thanks in advance.
As noted in the comments, the solution is to make sure that the line
#define _WINSOCK_DEPRECATED_NO_WARNINGS
is placed after
#include "stdafx.h"
but before the other #include statements.
While the previous advice works, it is ignoring the purpose of stdafx.h. The idea is that you place #include statements for header files that don't change frequently inside stdafx.h in order to make use of precompiled headers. Therefore you should ideally place
#define _WINSOCK_DEPCRECATED
inside stdafx.h, before other #include statements that it affects, in particular before including winsock2.h or other winsock related headers.
// pch.h
#ifndef PCH_H
#define PCH_H
#define _WINSOCK_DEPRECATED_NO_WARNINGS // defined here and it worked
#include "framework.h"
#include "xxx.h"

Question about CImage save function

I have encountered a problem while saving the CImage class data. The following are my brief set up:
#include "stdafx.h"
#include <cstring>
#include <atlimage.h>
#include <vector>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CImage myimage;
myimage.Create(100,100,24);
// save an image in BMP format
string impath = "image1.bmp";
myimage.Save((LPCTSTR)impath.c_str());
return 0;
}
I have run it as a WIN32 console application under VC++ 2008. There is no error. However, after the program done, there is NO file created under my directory. I have run this code on two machines, and the results are the same.
Thank you very much for your time and effort.
Maybe you are just looking in the wrong place for the file. Give an absolute path. The relative path is to the current working directory of the process. If you run from the debugger, it could be the output directory of the executable, the project directory or somewhere else.
Also, instead of the cast use the _T macro on c_str() -- in case it needs a conversion.
If you still can't figure it out, get PROCMON (free utility from Microsoft). Run it and filter the output so it's just looking for file with path contains "image1.bmp" -- it will tell you all file operations that were tried with that path and what happened.
myimage.Save((LPCTSTR)impath.c_str());
1) check result
2) why did you use type casting? this can mask the bug, e.g. if you use UNICODE build (default one in VS2008), filename will be invalid

Resources