New to CString. Why won't this compile?

Discussion in 'C++' started by Susan Rice, Dec 1, 2006.

  1. Susan Rice

    Susan Rice Guest

    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;
    }
     
    Susan Rice, Dec 1, 2006
    #1
    1. Advertising

  2. Susan Rice

    John Carson Guest

    "Susan Rice" <> wrote in message
    news:KxMbh.4549$
    > 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).

    --
    John Carson
     
    John Carson, Dec 1, 2006
    #2
    1. Advertising

  3. Susan Rice wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 1, 2006
    #3
  4. Susan Rice

    Earl Purple Guest

    Susan Rice wrote:
    > 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
    <string> and use std::string.

    > #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!"
     
    Earl Purple, Dec 1, 2006
    #4
  5. Susan Rice

    Gavin Deane Guest

    Earl Purple wrote:
    > <cctype> (does that exist?)


    Yes. <ctype.h> is in the C standard library.

    std::tolower, std::toupper, std::isdigit etc.

    Gavin Deane
     
    Gavin Deane, Dec 1, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AdamB
    Replies:
    5
    Views:
    621
  2. Rajesh Garg

    Why is CString not preferred

    Rajesh Garg, Jul 22, 2003, in forum: C++
    Replies:
    11
    Views:
    814
    Mike Wahler
    Jul 24, 2003
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    977
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,216
    Smokey Grindel
    Dec 2, 2006
  5. Nagaraj
    Replies:
    1
    Views:
    913
    Lionel B
    Mar 1, 2007
Loading...

Share This Page