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
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.
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!!!!
Problem solved! UNICODE strikes again! Hexedited the bulkfile.csv and found the issues! Thanks for any help. Terry
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.
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