|
C File Handling |
ngiacomelli
Member #5,114
October 2004
|
File handling and string manipulation has always been a blindspot for me, as far as C is concerned. Basically, I've got a textfile that contains information on items within my game. I want to store this information as a plain textfile so that other people can edit it easily. Here's some messy code that does work, but I feel there are a few problems with it. My first question: when using fgets, is it safe to just have a global buffer size (1000), like the one I'm using already? Or should I be specifying dependant on the string I'm grabbing (name, desc, etc). Is fgets even the best solution? Also: the strings copy with a strange character at the end (^). I'm assuming this is is because the string isn't null terminated. But as I stated earlier, I really want to create and edit this file via a text editor (read: Notepad). What can I do? An example file layout: The item name. Some fantastic description! 1 Some code:
|
gnolam
Member #2,030
March 2002
|
Quote: My first question: when using fgets, is it safe to just have a global buffer size (1000), like the one I'm using already?
The fgets is safe, but the sprintf copying isn't. If you're just going to copy a string, use strncpy instead of sprintf (and if you actually do need sprintf's functionality, use snprintf)! Quote: Also: the strings copy with a strange character at the end (^). That's an LF, as in a CRLF line ending. Open your file in text mode and it should go away. I.e.:FILE *f = fopen(filename, "rt"); -- |
ngiacomelli
Member #5,114
October 2004
|
Some good points, some updated code. Changing to "rt" hasn't solved the character problem, though.
|
Matt Smith
Member #783
November 2000
|
mingw fgets() acts just like in Unix. It only recognises \n as EOL, and so when you read text files with \r\n endings, you get a stray \r at the end. The following code will strip these. This code is safe with ASCII and with UTF-8, afaics |
Kitty Cat
Member #2,815
October 2002
|
Quote: mingw fgets() acts just like in Unix. It only recognises \n as EOL, and so when you read text files with \r\n endings, you get a stray \r at the end. Actually, they read up to and including the line-ending. If the line-ends in the file are \r\n, then you'll have \r\n at the end of the string. If it's just \n, you'll have \n at the end. Opening the file in non-b mode should automatically change \r\n to just \n, though. -- |
ngiacomelli
Member #5,114
October 2004
|
Still not having much luck with it
|
Tomasz Grajewski
Member #4,284
February 2004
|
Maybe you could try the Allegro config routines, here is an example how it could look like (should compile as C++, don't know about C):
Now your text file should be something like that: [ITEM_0] name = The item name. desc = Some fantastic description! stackable = 1 [ITEM_1] name = Some other item. desc = Some another fantastic description! stackable = 0 Sorry, but code I've posted isn't tested, but I hope that it will be useful somehow EDITED few times. ___________________________________________________________________________________________ |
ngiacomelli
Member #5,114
October 2004
|
The code doesn't work for me. I have a question, though: config_is_hooked(section) Where do we actually hook the section?
|
Tomasz Grajewski
Member #4,284
February 2004
|
Ups, sorry Nial. I've used wrong function. I wanted to check if a section exists in a file. It seems that Allegro doesn't have such a function, but you may try the int list_config_sections(const char ***names); function, but you will need of course to change the source code I've posted greatly. ___________________________________________________________________________________________ |
ngiacomelli
Member #5,114
October 2004
|
Tomasz, not a problem! I've used the Allegro config routines once before (long ago). So I'll go RTFM. Thanks for taking the time to type all that up! Edit: Tomasz, you're right! How the heck can I check (reliably) if a section is not found? I could compare the item name with empty_string, I suppose.
|
Tomasz Grajewski
Member #4,284
February 2004
|
I've wrote a fixed version, this time using the int list_config_sections(const char ***names); function (it's introduced in the version 4.2.1 of Allegro). Also this time it compiles as C++, so you will need to change it in few places to use it as C.
Now your text file could look like this: [ITEM_SWORD] name = Magic Sword desc = This is some text about the Magic Sword. stackable = 1 [ITEM_GUN] name = Big F*cking Gun desc = This is some text about BFG. stackable = 0 I hope that this time it will work for you. You could try to tweak this function, maybe even change it, so it will allocate the items[] array dynamically for you ___________________________________________________________________________________________ |
Arthur Kalliokoski
Second in Command
February 2005
|
When I use fgets, I make the buffer size and the bytes requested slightly larger than a MAX_STRING_SIZE #define, which is largest allowed string. Try a 1000 char line in a C file and try to compile it!
They all watch too much MSNBC... they get ideas. |
|