New to CString. Why won't this compile?

S

Susan Rice

I'm new to using CString. Why won't the following compile?
I'm using Microsoft Visual C++ 6.0

Line 37 which it complains about is the function:

37 CString ConvertFile(char *szFileName)

I tried throwing in a bunch of #includes but didn't help.
Here's the compiler errors:
Compiling...
UtoA.cpp
UtoA.cpp(37) : error C2146: syntax error :
missing ';' before identifier 'ConvertFile'
UtoA.cpp(37) : error C2501: 'CString' :
missing storage-class or type specifiers
UtoA.cpp(37) : fatal error C1004:
unexpected end of file found
Error executing cl.exe.

UtoA.exe - 3 error(s), 0 warning(s)

And here's the file UtoA.cpp I'm trying to compile:
(this code is from
http://www.codersource.net/win32_unicode_ascii.html)

#include "stdafx.h"
#include <cstring>
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
// http://www.codersource.net/win32_unicode_ascii.html

//Check if the file is UNICODE
int IsUnicodeFile(char* szFileName)
{
FILE *fpUnicode;
char l_szCharBuffer[80];

//Open the file
if((fpUnicode= fopen(szFileName,"r")) == NULL)
return 0; //Unable to open file

if(!feof(fpUnicode))
{
fread(l_szCharBuffer,80,1,fpUnicode);
fclose(fpUnicode);
if(IsTextUnicode(l_szCharBuffer,80,NULL))
{
return 2; //Text is Unicode
}
else
{
return 1; //Text is ASCII
}
}
return 0; // Some error happened
}

//Convert the file to ASCII type
CString ConvertFile(char *szFileName)
{
CString strTempFileName;
CString strInputFileName;
strInputFileName = szFileName;
char TempPathBuffer[260];
GetTempPath(260,TempPathBuffer);
FILE *fpASCII;
CStdioFileEx fpUnicode;

strTempFileName = TempPathBuffer;
strTempFileName += "TempUnicodecheck.txt";

if(IsUnicodeFile(szFileName) == 2)
{
//Open the UNICODE file
if(!fpUnicode.Open(szFileName,CFile::modeRead|CFile::typeBinary))
{
printf("Unable to open the unicode file\n");
return strInputFileName ;
}

//Create the temporary file
if((fpASCII = fopen(strTempFileName.operator
LPCTSTR(),"w+"))==NULL)
{
fpUnicode.Close();
printf("Unable to open the output file\n");
return strInputFileName;
}

CString strData;
while(fpUnicode.ReadString(strData))
{
strData += "\n";
fwrite(strData,sizeof(char),strData.GetLength(),fpASCII);
}
fflush(fpASCII);
fclose(fpASCII);
fpUnicode.Close();
return strTempFileName;
}
else
{
return strInputFileName;
}
}

int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
 
J

John Carson

Susan Rice said:
I'm new to using CString. Why won't the following compile?
I'm using Microsoft Visual C++ 6.0

Your question is Microsoft specific. You should ask in

microsoft.public.vc.language
Line 37 which it complains about is the function:

37 CString ConvertFile(char *szFileName)

I tried throwing in a bunch of #includes but didn't help.
Here's the compiler errors:
Compiling...
UtoA.cpp
UtoA.cpp(37) : error C2146: syntax error :
missing ';' before identifier 'ConvertFile'
UtoA.cpp(37) : error C2501: 'CString' :
missing storage-class or type specifiers
UtoA.cpp(37) : fatal error C1004:
unexpected end of file found
Error executing cl.exe.

UtoA.exe - 3 error(s), 0 warning(s)

And here's the file UtoA.cpp I'm trying to compile:
(this code is from
http://www.codersource.net/win32_unicode_ascii.html)

#include "stdafx.h"
#include <cstring>

cstring is the C++ version of the C standard library string header. It has
nothing to do with CString. CString was originally part of the MFC library
(and this is the case up to VC++ 6). From VC++ 7 onward, it was made
available independently of MFC.

Since you are using VC++6, you cannot use CString unless you are also using
MFC (unless you are going to hack the sources).
 
V

Victor Bazarov

Susan said:
I'm new to using CString. Why won't the following compile?
I'm using Microsoft Visual C++ 6.0

Line 37 which it complains about is the function:

37 CString ConvertFile(char *szFileName)

I tried throwing in a bunch of #includes but didn't help.
Here's the compiler errors:
Compiling...
UtoA.cpp
UtoA.cpp(37) : error C2146: syntax error :
missing ';' before identifier 'ConvertFile'
UtoA.cpp(37) : error C2501: 'CString' :
missing storage-class or type specifiers
[..]

Most likely you have forgotten to include the very file that
defines that class. We do not know what it should be, CString
is not a standard class. Please ask in 'microsoft.public.vc.mfc'
for help.

V
 
E

Earl Purple

Susan said:
I'm new to using CString. Why won't the following compile?
I'm using Microsoft Visual C++ 6.0

poor you if you have to maintain some horrible MFC legacy code.
Line 37 which it complains about is the function:

37 CString ConvertFile(char *szFileName)

That declares a function that takes a writable pointer and returns a
CString. Now it's obvious to me that this function is not going to
modify szFileName so it should take const char * or that horrible
LPCSTR or LPCTSTR that Microsoft uses.
I tried throwing in a bunch of #includes but didn't help.

but not the right ones, presumably.
UtoA.exe - 3 error(s), 0 warning(s)

And here's the file UtoA.cpp I'm trying to compile:
(this code is from
http://www.codersource.net/win32_unicode_ascii.html)

#include "stdafx.h"

The one thing I hate most about VC even version 8 is being told by
default to include this file and getting some fatal error if I haven't.
I always disable precompiled headers as a result. I'd rather my code
took a bit longer to compile than it having to have this horrible
header in it.
#include <cstring>

This file does not contain CString but contains C string functions such
as strlen and strcpy defined in namespace std.
#include <windows.h>

I have a funny feeling that unless you include this one first it messes
everything up which goes against the principles of header files.
#include <string.h>

You have already included <cstring>. Even better would be to include
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

If one does have to use these, then <cstdio> <cctype> (does that
exist?) and <cstdlib>

< snip some horrible C-style code that totally unnecessarily uses
CString >
int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}

At the end of all that your main ignores everything and prints "Hello
World!"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top