VC++ : System and IO namspaces are not found

Hi,

I am refering this page http://msdn.microsoft.com/en-us/library/19czdak8(VS.80).aspx to create a file and write the values inside but when I use the same program given at that location it gives me below errors:

Error      52      error C2871: 'System' : a namespace with this name does not exist
Error      53      error C2653: 'System' : is not a class or namespace      
Error      54      error C2871: 'IO' : a namespace with this name does not exist

I am trying to add this code in our build application. The file extension is .cxx.

Thanks,
LVL 1
deshawAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

numberkruncherCommented:
Are you using either a managed C++ or a C++/CLI project? If you are just using a win32 console application, then the .NET namespaces required in your code will not be available.
0
deshawAuthor Commented:
>>Are you using either a managed C++ or a C++/CLI project?
Could you tell how can I verify this? and what should I change to make "Using System" works. Actually I am not familiar with VC++ but I now C# very well.

Thanks
0
numberkruncherCommented:
If you go to the project properties window (right-click your project in the panel and select properties), you should have one of the following values for Common Language Runtime Support:

Common Language Runtime Support (/clr)
Pure MSIL Common Language Runtime Support (/clr:pure)
Safe MSIL Common Language Runtime Support (/clr:safe)
Common Language Runtime Support, Old Syntax (/clr:oldSyntax)

Though, I recommend the first of these (unless you are using the old MC++ syntax in which case you would want the last of these). The top one supports the special .NET specific reference operator ^
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

numberkruncherCommented:
The easiest solution might be to just create a new project of the type "CLR Console Application" because it is then already prepared to compile the above program.
0
deshawAuthor Commented:
It says:

Error 27      Command line error D8016 : '/MT' and '/clr' command-line options are incompatible      
0
numberkruncherCommented:
To get rid of that error change

C/C++ > Code Generation > Runtime Library to
    Multi-threaded DLL (/MD) for Release
    Multi-threaded DLL Debug DLL (/MDd) for Debug
0
deshawAuthor Commented:
Thanks but I think some of files are there in my project which cannot compile with CLR option. it gives me below error:

Error 213      Command line error D8045 : cannot compile C file '.\d3des.c' with the /clr option      

When I have created new project CLR project the file extension I got is .CPP but my whole project have .cxx extensions.

What should I do?

Thanks,
      
0
numberkruncherCommented:
Right-click your .c file and select Properties. Then change:

C/C++ > Code Generation > Runtime Library to
   No Common Language Runtime Support

It sounds like your project is primarily written in C and C++. Is your only reason for using the .NET libraries to work with file handling? If so, it might be worth considering the standard file handling routines available in C and C++.
0
deshawAuthor Commented:
Yes I think so. Actually this project is already written and it came to me for some enhancements. I am professional in C#.Net.

>>it might be worth considering the standard file handling routines available in C and C++.
That will be really great,. How could I do this? Can you point me some links then it will be great.

Could you tell me what is the difference between .cxx , .c and .cpp extension. I know .c and .cpp and I could see that .cxx is meant for VC++ file then is there any differece in normal C++ and VC++?

I have increased the points value. Thanks for being with me.

Thanks,
0
numberkruncherCommented:
> That will be really great,. How could I do this? Can you point me some links then it will be great.

There are two major approaches to this. One approach is to use the standard C libraries, the other is to use the C++ STL (standard template library).

In general the C libraries make your code much more portable, and if you work with DLL's a lot, then they remove some of the complications that templates introduce. I also find that the C approach is more intuitive than the STL because it is function based as opposed to template based.

Please see the source example below for an example of the standard C approach.

Here are some links which demonstrate this:
http://www.learn-programming.za.net/programming_c_learn10.html
http://msdn.microsoft.com/en-us/library/xkh07fe2(VS.71).aspx

This links to an index of available commands.
http://msdn.microsoft.com/en-us/library/c565h7xx(VS.71).aspx

Also, if you want your code to work with other compilers, ignore the "_s" at the end of the command names because this feature is Microsoft specific. For example, don't use "fprintf_s" use "fprintf".


Some quick points:

- fprintf outputs text
- fscanf reads text

- fwrite outputs binary data
- fread reads binary data


> Could you tell me what is the difference between .cxx , .c and .cpp extension. I know .c and .cpp and I could see that .cxx is meant for VC++ file then is there any differece in normal C++ and VC++?

Generally, the extension is specific to a compiler. For Microsoft products .cpp is the standard extension for C++.  The .cxx extension is basically the same, but some compilers are really picky about which extension is used. Personally I always use .cpp because it works for me on both Windows and UNIX.
#include "stdafx.h"
 
#include <time.h>
#include <stdio.h>
 
void PrintDateTimeNow(FILE *);
 
int main(int argc, const char **argw)
{
	FILE *pfh = fopen("textfile.txt", "w");
	if (pfh)
	{
		// Note: The following uses an escape sequence \n instead of WriteLine!
		fprintf(pfh, "A text file was born!\n");
		fprintf(pfh, "You can use WriteLine");
		fprintf(pfh, "...or just Write\n");
		fprintf(pfh, "and do %s output too.\n", "formatted");
		// Note: In C/C++ you are not working with objects, so you don't have a .ToString
		//		 method available to you by default. This means that you have complete
		//		 control over what is happening. So to display the date and time you need
		//		 to use the time header file.
		fprintf(pfh, "You can also send non-text objects:\n");
		PrintDateTimeNow(pfh);
		fclose(pfh);
	}
	else
	{
		// There was an error whilst opening file!
	}
	return 0;
}
 
void PrintDateTimeNow(FILE *pfh)
{
	time_t t = time(NULL);
	struct tm *local = localtime(&t);
	fprintf(pfh, asctime(local));
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsmeandnobodyelseCommented:
>>>> In general the C libraries make your code much more portable

????
STL is C++ standard. So if you don't use libraries older than 10 years (C++ standard was released in 1998!!!), you won't get more portation issues as you would get using C runtime (actually any portation issues are due to the underlying file system and you have a fair chance that newer C++ versions of STL would deal with newer filesystems better than C runtime, e. g. with UNICODE support).

>>>> and if you work with DLL's a lot, then they remove some of the complications that templates introduce.

templates only can arise problems if you use them in the interface between the application and the dll. That normally isn't necessary but even if you do, the only thing to consider is to use the same compiler, STL version and threading model (multi-threaded or single-threaded) for all executables. But the latter must be kept in mind for C runtime as well. If you want to export classes from your dll you can't use C runtime. So, the decision simply is whether you want to use C++ or C. And if you come from C# you should go to C++. No question.

>>>> is more intuitive than the STL because it is function based as opposed to template based.

I object. The question was posted in the C++ TA. C++ is a OOP language and the iostream/fstream/iomanip headers of STL offer a class approach to the file system. You don't have to use any template syntax for normal file i/o as the template types are hidden in classes ifstream/ofstream or wifstream/wofstream (UNICODE text files).

The C++ file approach is safer and much more intuitive than the C runtime functions.



#include <time.h>
#include <errno.h>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
 
void PrintDateTimeNow(ofstream & ofs);
 
int main(int argc, const char * argw[])
{
      ofstream ofs("textfile.txt");
      if (ofs)
      {
	ofs << "A text file was born!" << endl;
	ofs << "You can use WriteLine" 
             << "...or just Write" << endl;
         ofs << "and do " << right << setw(15) << "formatted" 
             << "output too" << endl;
// Note: In C you are not working with objects, but in C++		ofs << "You can also send non-text objects: " << endl;
	PrintDateTimeNow(ofs);
// or simpler 
         time_t t = time(NULL);
         struct tm * local = localtime(&t);
         ofs << local->tm_month << "/" 
             << local->tm_mday << "/" 
             << local->tm_year << endl;  
 
	ofs.close()
      }
      else
      {
	 // There was an error whilst opening file!
          cout << "There was an error whilst opening file! " 
               << errno << endl;
      }
      return 0;
}
 
void PrintDateTimeNow(ofstream & ofs)
{
    time_t t = time(NULL);
    struct tm * local = localtime(&t);
    ofs << asctime(local) << endl;
}

Open in new window

0
deshawAuthor Commented:
Thanks both of you for much details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.