problems reading from a file

This is a discussion on problems reading from a file within the C++ Programming forums, part of the General Programming Boards category; hi, im having a few problems at the minute which involve reading a certain amount of bytes from a file. ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    problems reading from a file

    hi, im having a few problems at the minute which involve reading a certain amount of bytes from a file. I have a program that will store an int value( could be anything depnding on the file.) However say for example my int has the value 50 stored in it I would then like to read 50 bytes from the file and store it into a string. I thought i had managed to achieve this but it only seems to read out the first couple of bytes into my string and that is it, could anyone see where ive gone wrong?

    Code:
    
     infile.seekg(posInFile, ios::cur);         //getting to the right pos in file
    
    int properpathstart;           //This is the int value telling me how much to strip from the file
    
    
    char* PATHSTRING = new char[properpathstart];        //not sure if this is the right type of struct to store it
    
    infile.read( PATHSTRING, properpathstart );             // attempting to read in file
    
       string pAtH = PATHSTRING;                                 //storing as a string

    thank you

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    This bit is, more or less, right. While not completely correct, it should get at least "properpathstart" bytes in the string, unless an end-of-file is reached. So make sure the file is large enough and the integer has the correct value (simply output it).

    The only actual bug I can see is that you read from the file with 'read', which is fine, except that it doesn't add a 0-terminator to the character array. Therefor, when converting it to a string, it may actually read more characters than actually read from the file.
    Better would be to change:
    string pAtH = PATHSTRING;
    into
    string pAtH(PATHSTRING, properpathstart);

    Hope that helps

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    thank you for your reply, ive been researching this all morning and found that the reason its not reading it all is because its sometimes reaching a blank space and i think its treationg it as a null character and stops the creation of the string As away round this ive decided to read the file charcter by character however im now stuck on adding all the charcters together to make one overal string that includes any 'spaces' that may be read. i hope that makes sense!

    Code:
    
                int i;
           for(i= 0; i < properpathstart; i++)     // loop while extraction from file is possible
      {
    
    
        c = infile.get();       // get character from file
    
    
        
              }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Strings have a push_back member function.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    thanks for the reply but if i reach a blank character in the middle and i try to push it on it just terminates my string and i end up with the rest of the characters missed off the end

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Whitespaces - assuming you mean those - really shouldn't break off a string. I see two possibilities here (I might miss some, but those I can think of right now):
    1. You do something else incorrectly with your string, causing it to be chopped off at the first whitespace
    2. It's a binary file and it contains 0 terminating characters. If this is the case, then you will have to know you simply can't use the character in strings (at least not if you wish to print those strings, it's fine to use them in STL strings).

    I don't think anyone can help you any more unless we get to see more of your program and the file that is being read (if it's a binary file. But copying it is worthless, we'd need a hex-dump).

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you interpret it as a C string, then that is what you want, since that is what a C string is. If you want the bytes themselves, don't interpret it as a C string (i.e., using .c_str() and the like). (I'm assuming (big assumption) that by "blank character" you mean \0, and not a space. Neither a C string, nor push_back, nor anything is going to be concerned about a space.)

    Is this supposed to be a string, or just a bunch of bytes? If it's a string, then either your number is wrong or something else is going bad. If it's just a bunch of bytes, then string wasn't appropriate in the first place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. I have some problems reading struc from file
    By samc2004 in forum C Programming
    Replies: 4
    Last Post: 12-18-2003, 01:47 PM

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