Thread: I'm back (same stupid problem)

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    I'm back (same stupid problem)

    Some of you who were helping me a few days ago might remember this thread:http://cboard.cprogramming.com/showt...999#post181999
    Heres a few changes but weird stuff just keeps happening.

    Code:
    AnsiString TFileHandle::Load() const
    {
      AnsiString Return;
      char *Text;
      char Char;
      int Length;
      int i = 0;
    
      ifstream File(itsName.c_str());
    
      if (File.is_open())
      {
        File.seekg(0, ios::end);
        Length = File.tellg();
        File.clear(); /* By using search I read that using this should
        solve it, but no matter if I remove it or not the outcome is stil 
        the same */
        File.seekg(0, ios::beg);
    
        Text = new char[Length];
        Text[0] = '\0';
    
        while (File.good())
        {
          File.get(Char);
    
          Text[i++] = Char;
        }
    
        Text[i-1] = '\0'; /* I have to do this though it uppose both what
    I think and what "The dog" recomended me in the last thread.
    Still I have to do it since Text othervise will "have" a sign at the end more then there should be.*/
        Return = Text;
        delete [] Text;
    
        File.close();
      }
    
      return Return;
    }
    I have found out that Length always is longer then the amount of charectors in the "opened" file. This could be some of the problem, but what worries me more is how come is it longer since Length use a "recomended" method to get the length of the file so why is this method just not working.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Okay two things:

    1. How do I then solve my problem using char

    2. What about the other things I mention as remarks in the code.
    They are strange.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Not sure about this:

    Text[i++] = Char;

    in your while loop.

    When is i actually incremented, before it is referenced or after? ( I can't remember?). If it is before, this will cause you problems because Text will only be filled starting at position 1. Because you have set Text[0], to be NULL, Text will always appear empty.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    No it wont because when you say i++ the compiler first use i like Text[i] = Char and then says i = i + 1 so that aint why.
    Besides it return the text I want. The problem is that it add some wierd not supposed to be there signs in the end.
    Last edited by Zahl; 10-22-2002 at 08:14 AM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I would try this:
    Code:
    Text = new char[Length + 1];//make room for all char in file plus one extra for the null terminating char
    
    //read first char 
    File.get(Char);
    while (File.good())//as long as the last read by File is good
    {
      Text[i++] = Char;//assign the value of Char to Text[i] and then increment i by one
       File.get(Char);//then read the next char
    }
    //when all done i will be empty from the last increment so add the null there
    Text[i] = '\0';

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Then this line is wrong.

    Text[i-1] = '\0'

    Imagine you only get one char from your while loop. This is what happens.

    i is set to zero

    (begin loop)

    char is read & placed in Text[0]

    i set 1

    (end loop)

    set Text [i - 1] = NULL;

    In other words, you write the null over the last char.

    There must be a much easier way to do all this. Why don't you just initialise Text to begin with. I.e.

    memset(Text, 0, size);

    Provided you don't write beyond the length of Text, you can forget about setting the null character.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    I know I write over the last char... that's the problem (what's weird that is) because if I don't the array haves a sign at the end of it that the file didn't have. So this is what I do, but the problem shoudn't be there in the first place... that's what I said in my remarks in the code.

    *edit* Hey elad, why do you use the get function before the while loop... in other words what do you gain.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Right, text[i] = 0;

    ...unless of course i == length.

    But also, remember: you should definately make sure length-1 > 0.

    memset(Text, 0, size);
    Well, if it's a 10000 char buffer, don't you think that memset would be slow? What I always do is this:

    if length < 1 then leave.
    if length < 2 then buff[0] = 0, leave;

    otherwise:

    buff[0] = ' '; //...so printf, etc doesn't crash..
    buff[1] = '\0'; //...to simulate a memset'ed string, but save time.
    buff[length -1] = '\0'; //..just for posterity

    Thus 3 operations, not 10000 are used and the string is ready to go.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, change this:

    Text = new char[Length];

    to this:

    Text = new char[Length+1];

    This does two things:

    It ensures that we get all of the data.

    It ensures that when we do text[length-1] = 0, we are never making an assignment to text[-1], which of course is an access violation.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >> if I don't the array haves a sign at the end of it that the file didn't have.

    You are probably detecting the EOF char and trying to enter it into the program.

    I believe good() returns false when EOF is encountered. The way the way you have the loop set up you read in EOF and "add it" to Text before testing File with good(). Thus the extra sign/symbol.

    Assume the file ends like this:

    ....last word.\r\nEOF

    with a loop like:

    while(fin.good())
    {
    File.get(Char);
    Text[i++] = Char;
    }

    everything goes fine until File reads in \n. The program adds \n to Text[i] and goes back to the while conditional. File is still good since \n is a valid char, so the loop is entered and File reads in EOF. It adds EOF to Text[i] and then goes back to while conditional. Only now does good() fail and the loop stop, _after_ EOF (or an extra symbol) has been added.

    Assuming this is the problem, see my previous post for the solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with passing back pointer pointed to string
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-21-2007, 07:55 AM
  2. Awkward problem in learning c++
    By TheUnknownFacto in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2007, 01:43 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM