[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VC++ : System and IO namspaces are not found

Posted on 2008-11-17
12
Medium Priority
?
756 Views
Last Modified: 2012-05-05
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,
0
Comment
Question by:deshaw
  • 6
  • 5
12 Comments
 
LVL 13

Assisted Solution

by:numberkruncher
numberkruncher earned 1600 total points
ID: 22975221
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
 
LVL 1

Author Comment

by:deshaw
ID: 22975265
>>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
 
LVL 13

Assisted Solution

by:numberkruncher
numberkruncher earned 1600 total points
ID: 22975322
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:numberkruncher
ID: 22975346
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
 
LVL 1

Author Comment

by:deshaw
ID: 22975385
It says:

Error 27      Command line error D8016 : '/MT' and '/clr' command-line options are incompatible      
0
 
LVL 13

Assisted Solution

by:numberkruncher
numberkruncher earned 1600 total points
ID: 22975406
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
 
LVL 1

Author Comment

by:deshaw
ID: 22975465
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
 
LVL 13

Assisted Solution

by:numberkruncher
numberkruncher earned 1600 total points
ID: 22975512
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
 
LVL 1

Author Comment

by:deshaw
ID: 22975750
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
 
LVL 13

Accepted Solution

by:
numberkruncher earned 1600 total points
ID: 22976006
> 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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 400 total points
ID: 22977990
>>>> 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
 
LVL 1

Author Closing Comment

by:deshaw
ID: 31517418
Thanks both of you for much details.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

873 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