File.read(Text, Length);

This is a discussion on File.read(Text, Length); within the C++ Programming forums, part of the General Programming Boards category; Code: char *Text; File.seekg(0, ios::end); Length = File.tellg(); File.seekg(0, ios::beg); Text = new char[Length]; File.read(Text, Length); How come that every ...

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

    File.read(Text, Length);

    Code:
        char *Text;
    
        File.seekg(0, ios::end);
        Length = File.tellg();
        File.seekg(0, ios::beg);
    
        Text = new char[Length];
    
        File.read(Text, Length);
    How come that every time I use this code then Text will equal some weird signs like ",H×" allthough the file was completely empty. Other then that it allso tends to have problems with '/t' (tabulator) since sometimes Text "haves" them as it's supposed to and other times it have two in a road were only one was supposed to were.

    Why?
    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
    Code:
    AnsiString TFileHandle::Load()
    {
      ifstream File("EditAar.cfg");
    
      if (File.is_open())
      {
        File.seekg(0, ios::end);
        Length = File.tellg();
        File.seekg(0, ios::beg);
    
        Text = new char[Length];
    
        File.read(Text, Length);
        ShowMessage(Text);
      }
    
      File.close();
    
      return Text;
    }
    
    Private: // You get the idea
    char *Text;
    int Length;
    Last edited by Zahl; 10-18-2002 at 06:49 AM.
    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
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Code:
    AnsiString TFileHandle::Load()
    {
      ifstream File("EditAar.cfg");
    
      if (File.is_open())
      {
        File.seekg(0, ios::end);
        Length = File.tellg();
        File.seekg(0, ios::beg);
    
        Text = new char[Length + 1];
    
        File.read(Text, Length);
        Text[length] = '\0';
        ShowMessage(Text);
    
        File.close();
      }
    
      return Text;
    }
    
    Private: // You get the idea
    char *Text;
    int Length;
    Okay I've tryed thise before, but since you asked I thought I'd try once more, but ofcourse it didn't work.

    AnsiString is a Borland object (BTW I'm using Borland version 6.0) which is maid to handle strings and such. It have a overloaded operater like operator=(char*) so it's able to do this. The explanation to why I don't just return a char* is that I want it to be easy to use with Borlands objects which all is using AnsiString when somekind of string is involved.
    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

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Code:
    AnsiString TFileHandle::Load()
    {
      ifstream File("EditAar.cfg");
    
      if (File.is_open())
      {
        File.seekg(0, ios::end);
        Length = File.tellg();
        File.seekg(0, ios::beg);
    
        Text = new char[Length + 1];
        Text[0]='\0'; 
        File.read(Text, Length);
        Text[length] = '\0';
        ShowMessage(Text);
    
        File.close();
      }
    
      return Text;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    I can't even descripe how thankfull I am!
    It worked and it worked damn well!!!
    I've been asking so many people but all the ansvers I've got have been something I already knew and/or had tryed.

    *Runs around in joy*

    THX!
    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

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Funny though, that I had to make this change before it all worked.
    How can that be? And how come that no site on the net I found said anything about this.

    Code:
    AnsiString TFileHandle::Load()
    {
      ifstream File("EditAar.cfg");
    
      if (File.is_open())
      {
        File.seekg(0, ios::end);
        Length = File.tellg();
        File.seekg(0, ios::beg);
    
        Text = new char[Length];
    
        for (int i = 0; i <= Length; i++) // CHANGE!
        {
          Text[i] = '\0';
        }
     
        File.read(Text, Length);
        Text[length] = '\0';
        ShowMessage(Text);
    
        File.close();
      }
    
      return Text;
    }
    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

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    As Salem said, read doesn't add a '\0'
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Well, I just did that and it gave me one of those weird strings like before. Doesn't make much sense huh.

    And I'm aware that read doesn't add '\0'
    That's why I remembered to add:
    Text[length] = '\0';
    Last edited by Zahl; 10-18-2002 at 10:28 AM.
    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

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Zahl

    That's why I remembered to add:
    Text[length] = '\0';
    And where did you remember to initalize length?
    Remember length != Length
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Yeah, I used a big L insted of a little one.
    Now is it possible to give an ansver on what's wrong or is it somekind of vodoo?

    Remember it worked when I used the "for" loop, but that's inded kind of wierd since it shoudln't be necesary to do more than putting and '\0' in the start of the array.
    Last edited by Zahl; 10-18-2002 at 04:35 PM.
    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

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    What! Is this too much of a challenge for anyone to solve on this forum?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 08:38 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 11:45 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21