Thread: Long CHAR

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Long CHAR

    I'm having a slight problem with a program I'm working on : I need the user to be able to input a large number of characters ( this is why CHAR is not suitable, it limits characters to 256 ) and I want the user to be able to use new lines. I tried something like this :

    Code:
    char data[256];
    
    ...
    
    cin.getline(data, 256, '*');
    but that limits it to 256, so I tried this :

    Code:
    wchar_t data[50000];
    
    ...
    
    cin.getline(data, 50000, '*');
    However, the getline command doesn't seem to work with wchar_t variables. I haven't worked much with strings, will they work, and can they take multiple lines ?

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    "char" limits your string to 256 *different* characters. Unless you plan to display things like japanese text, etc, you can use strings of chars. It places no limits on the length of the string; as long as you have memory, you could in theory have 4 billion characters before you have a problem. The 256-character limit means you can only use ASCII characters, you can't use Unicode characters, which are 16-bits wide.

    wchar_t is used if you're using Unicode (16 bit characters instead of 8-bit). To use it, you need to use all Unicode functions. If you're not compiling for Unicode, don't use it.

    It's perfectly acceptable to do "char c[50000];" if you needed 50,000 characters. getline only returns one line of data, but there are other types of reads which will return multiple lines; strings can have multiple lines in them if you want them to.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    For wide character streams, use std::wcin, std::wcout, sdt::wcerr, wclog. They're all the same as their normal equivalents. They're also in the same library: <iostream>.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       wchar_t str[64];
    
       wcin.getline(str, 64);
       wcout << L"You entered: " << str << endl;
    
       return 0;
    }

  4. #4
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Alright, thank you, now my problem is as follows :

    Code:
    fout << (char)(data^key[count++]);
    Returns :
    Code:
    invalid operands `char[50000]' and `char' to binary `operator ^'
    How do I solve this ? Thanks.

  5. #5
    Noob
    Guest
    yes i have the same question

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by Korhedron
    Alright, thank you, now my problem is as follows :

    Code:
    fout << (char)(data^key[count++]);
    Returns :
    Code:
    invalid operands `char[50000]' and `char' to binary `operator ^'
    How do I solve this ? Thanks.
    Well, what is that line of code supposed to do? Without knowing that, it's hard to say.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It seems like you're trying to do a binary XOR between a single char and a char[5000] which is wrong. Or did you mistake it for the power-operator? But raising a char to the power of a string still seems kinda weird .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Hehehehe, no, I didn't mistake it for the power operator.

    Char KEY is a coding key, a long line of complicated characters, and Char data[50000] is a long line of data I want to be able to type in. Basically, I want the user to type in some stuff, including being able to miss lines, whatever, and I want the program to write to a file what the user gave in, but encrypted through XOR.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Show some more code then, especially the definitions of Key and Data and the loop. I tried this and that works:
    Code:
       char Key = 45;
       char Buffer[SIZE] = "Hello";
    
       for(int i=0; i<strlen(Buffer); i++)
       {
           cout << (char)(Key ^ Buffer[i]);
       }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Code:
    char filename[256];
    char data[50000];
    int count  = 0;
    char key[52] = "&^%5/{?>:]!@A1c%~<,.;yX+_|`':8$^M6h #6Hmd 5/-+?,;:'";
    
    ...
    
    void createcrypt()
    {
    clrscr();
    t("Enter the name of the file you wish to create.\nInclude the extension [EG. .doc, .txt]\n");
    cin.getline(filename, 256, '\n');
    t("\n\nEnter your data. End it wish a \"*\" ( without quotes ) followed by [ENTER].\n");
    cin.getline(data, 50000, '*');
    
    clrscr();
    t("Preparing to encrypt your data.\n");
    t("Creating empty file : ");
    t(filename);
    done();
    fout.open(filename, ios::binary | ios::out);
    fout << (char)(data^key[count++]);
    if(count == strlen(key))
    {
    count = 0;
    }
    	fout.close();
    }
    Void t is simply like COUT, but using typewriter effect.

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    char data[50000];
    char key[52] = "&^%5/{?>:]!@A1c%~<,.;yX+_|`':8$^M6h #6Hmd 5/-+?,;:'";
    
    fout << (char)(data^key[count++]);
    As mentioned above, you do bitwise XOR on a char and a char[50000] which is plain wrong. You must use the same datatype on both sides.
    If you want to do it on every charcater in Data, create a loop.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ text file
    By statquos in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2008, 01:42 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM