Results 1 to 13 of 13

Thread: My Computer Is @#*$!!!

  1. #1

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228

    My Computer Is @#*$!!!

    OK, i guess you can tell that im upset!! .... For some reason my computer $#%* up when i try to use a char *or a TCHAR *... here

    Code:
    char *flnm, *str, ch; int i = 0;
    
    cout<<"Enter the file name to be read:"<<endl;
    getline(cin, flnm, '<');
    
    ifstream fin(flnm);
    while (fin.get(ch)) {
        str[i] = ch;
        i++;
    }
    fin.close();
    cout<< The file reads..."<<endl<<endl<<str<<endl;
    When I run the program it starts up and the computer does its clicking noise a bunch and then it gives me: test has created and error in test.exe, test.exe will now close. and it quits!!!!!!!!!! WHY IS IT DOING THAT!!!!????? Can any1 tell me? I know its the char * thats messing up cause when I comment those lines out (the str[i] = ch; line) it runs through fine (except that i get no return data.
    To protect time is to protect everything...

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    flnm and str are wild pointers, point them to buffers or use those directly, ex:
    char flnm[100];
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    I know they are... I need an array of chars without knowing the exact number of indexes (an undifined array)... as far as i know there isnt any way to do that except with char *.... am i wrong?
    To protect time is to protect everything...

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    number of indexes is rather known as array size, arrays are defined, what you mean is dynamic arrays, and are allocated on the heap, I don't think you need a dynamic array in this case. you'll make it with a buffer of say 255 chars.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    ok, i'll try ....... I just tried debugging my app when the fatal error appeared... and its coming up with and error on the str variable:

    Error: CXX0030: Expression cannot be evaluated.... ???? The str's value is 0xcccccc "". Whats wrong... I know that it works because it works on my other computer.
    To protect time is to protect everything...

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That's usually just a matter of luck. It may work, it may not, but even if it works, it is a very bad thing to do.
    Since nobody has filenames longer than 256 characters, it's okay if flnm is a
    char flnm[256];
    I recommend using a dynamic array for str:
    char *str;

    // get length of file, don't know how with fstream
    int iLength = LengthOfFile(flnm);
    str = new char[iLength];

    Don't forget to use delete or delete[] when done.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What about NTFS? That doesn't have a 260-char limit
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I didn't talk about limits, I talk about practical use. You can extend the array to MAX_PATH to be sure on this side. (You must include windows.h for this constant.)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    if you dont know how long its gonna be you can use

    Code:
    char *tmp = new char[]
    if you're using VC++ and use strlen(), it'll be the length of the contents (i've tested it, it works, its in that other thread)
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  10. #10

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    do you have to use the delete or delete[] function after using the char *tmp = new char[];??
    To protect time is to protect everything...

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I still can't imagine that this gives you valid results...
    They may work now, but it could cause problems later...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    Ok, i am asking which is the best way to do it. I need a way to be able to read and write from some kind of string variable in a way that i can look at one character at a time (just like str[i] = ?). So whats the best kind of variable to use and how do you use it ( i know that the different types of string variables have different functions with them ) What about string (basic_string)?
    To protect time is to protect everything...

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    string whatsit("Hello World");
    
    for(int i = 0; i < whatsit.length(); ++i) {
        cout << whatsit[i] << endl;
    }
    
    for(string::iterator k = whatsit.begin(); k != whatsit.end(); ++k) {
        cout << *k << endl;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width