You are here:

C++/Regarding files

Advertisement


Question
HI,

In my program i have a string variable "FilePath". I later on assigned it as follows:
FilePath= 's:\Desktop\Test Folder\Result';

When compiling it gives me an error as
error C2015: too many characters in constant
error C2593: 'operator =' is ambiguous

Why is this so??HOw can I resolve this?

I am trying to store my results in the above path. Is there any other way in C++ to do the same.

Please help.

Thanks,
Rahul


Answer
Firstly this has nothing much to do with files - as you probably realised once you had finished entering the question. It has to do with string and character handling and it just happens that the data making up the strings in this case represent file path names.

Secondly, the term 'string variable' is ambiguous. Do you mean a C++ std::string type or a C-style zero terminated char array or some other string type specific to your work or some library you are using such as a BSTR or CString which are various Microsoft string types (the errors you show look as if you might have been using a Microsoft Visual C++ compiler)?

However, to the question. The most obvious error is that you are trying to delimit a string literal using single quotes which in C and C++ indicate a character literal - that is a single character such as 'a' or '+' or some such. You use double quotes - " - to delimit a string literal in C and C++:

   FilePath = "s:\Desktop\Test Folder\Result";

The next problem you have is that the back slash character that forms the path separator in Microsoft operating systems is also the escape character used for C and C++ character literals that allows entry of certain characters that generally are difficult to enter into string or character literal, the most common use being the newline character '\n'. To resolve this either escape the back slashes correctly by doubling them up - '\\' resolves to a single back slash character:

   FilePath = "s:\\Desktop\\Test Folder\\Result";

Or use the UNIX style forward slash character as a path separator that is also accepted by Microsoft operating systems internally (the reason for the requirement for back slash characters as path separators is that on command lines Microsoft operating system commands use the forward slash as a command option indicator):

   FilePath = "s:/Desktop/Test Folder/Result";

Now the string literal you require assigned to your string is specified correctly the only remaining problem would be if your string type of FilePath did not allow assignment from char string literals. Most do allow such assignments but as you give no specifics on the type of string you are using I cannot say for sure.

Using a char array example which would cause a problem as C/C++ have no assignment of arrays that copies all the elements from one array to another. In such cases you would need to use the C-string operation function strcpy. On the other hand you can initialise a char array with a string literal:

   char FilePath[30]; // path has 29 chars plus 1 for zero terminator
   FilePath = "s:/Desktop/Test Folder/Result"; // BAD, no array assignment

   char FilePath[50];
   strcpy(FilePath, "s:/Desktop/Test Folder/Result"); // OK

   char FilePath[] = "s:/Desktop/Test Folder/Result";  // OK, initialisation


Another reason I can think of that may cause a problem is if you are using a wide string type (i.e. one in which the characters are formed from wchar_t elements rather than char) - in which case you may find that you need to assign from a wide string literal. To convert the narrow (char) string literal to a wide string literal prefix it with L (for long):

   WideFilePath = L"s:/Desktop/Test Folder/Result";

Hope this helps.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.