Reading very long strings in C

Discussion in 'C' started by tomurchu, Mar 5, 2007.

  1. tomurchu

    tomurchu New Member

    Joined:
    Mar 5, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Engineering Manager
    Location:
    Cork Ireland
    Hi,
    I'm trying to read very long strings (max 4096 characters) from a file but struggling.

    Using fgets to no avail. Tried a number of options indicated on net using malloc but once char count goes > 256 all seems to go pear shaped. Using bloodshed compiler with a DOS based application for simplicity.

    Long time since I programmed C (10yrs) but am getting really frustrated.
    Kind regards,

    Terry :eek:
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I think you'll find this compiler dependent. Microsoft C will handle 2048, but ISO 89 sets is considerably shorter, 509 (???) as I recall. I think you're going to have to roll your own string utilities for that, maybe use fread.
     
  3. tomurchu

    tomurchu New Member

    Joined:
    Mar 5, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Engineering Manager
    Location:
    Cork Ireland
    Hi,
    To further explain my issue, I have attached the C ptogram I'm using along with two test files. Testdata1.txt contains very large strings and is easily read by the c program. However bulkfile.txt (which is the file I need to read) cannot be read sucessfully. I think this is down to some funny characters in the file but I'm at a loss to be honest.

    Please help!!!! :confused:
     

    Attached Files:

  4. tomurchu

    tomurchu New Member

    Joined:
    Mar 5, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Engineering Manager
    Location:
    Cork Ireland
    Problem solved!

    UNICODE strikes again! Hexedited the bulkfile.csv and found the issues!

    Thanks for any help.

    Terry :)
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Yeah, a hex editor showed me the unicode. You may have a number of other issues. The length given to fgets does not specify how much you read, but a maximum to read if a newline is not found. You may be aware of this, but your code doesn't indicate so. I would still be leery of trying to read the super long strings with fgets, unless you find it works on YOUR system, and you have absolutely no qualms about non-portability. Personally, I'd use fread for copius amounts of material. I don't know what compiler you're using, but the definition of main, "int main (int argc, char *argv[0]){...}" won't work with a compliant compiler, as one can't declare an array (argv) of 0 bytes. Also, if opening file 1 succeeds, but opening file 2 fails, you don't close file 1 before exiting. The system will take care of this for you, generally, but you may lose file handles, of which there are a limited number. It's a bad habit to get into.
     
  6. tomurchu

    tomurchu New Member

    Joined:
    Mar 5, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Engineering Manager
    Location:
    Cork Ireland
    Thanks for the additional commentry. I will tidy up the closing of f1 if fail to open f2 but since I'm trying to engineer a cheap & cheerful solution for a current project & am not worried about portability I'll forge ahead on the fgets (I know the length of the largest string I'll encounter < 24K chars). All the best, Terry
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice