![]() |
Reading very long strings in C
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: |
Re: Reading very long strings in C
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.
|
Re: Reading very long strings in C
1 Attachment(s)
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: |
Re: Reading very long strings in C
Problem solved!
UNICODE strikes again! Hexedited the bulkfile.csv and found the issues! Thanks for any help. Terry :) |
Re: Reading very long strings in C
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.
|
Re: Reading very long strings in C
Quote:
|
| All times are GMT +5.5. The time now is 04:45. |