![]() |
| | #16 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| |
| itCbitC is offline | |
| | #17 |
| * Death to Visual Basic * Join Date: Aug 2001
Posts: 768
| So my best solution is a staticly sized string?
__________________ "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe http://www.Bloodware.net - Developing free software for the community. |
| Devil Panther is offline | |
| | #18 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
Incidentally, have you considered say, using SQLite? It has been described by its own author as a substitute for fopen(), and depending on your requirements it could suitably do just that.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #19 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| |
| itCbitC is offline | |
| | #20 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Depending on what you are doing, it may make the whole scheme a lot more complex - for example, reading a variable size record can be quite a lot more complex than a fixed size one - either we need to store the size and then read back the size again to know how much more to read. Or if we don't store the size, we need to read each byte, until we get a zero. A struct like this: Code: struct
{
int x;
char *name;
double d;
};
It gets even more messy if there are several strings in the structure. The other problem comes if we need to seek to get to element n. If each struct is stored with a different number of bytes, we can't actually use seek, unless we also keep a table of index -> offset. Or we have to read each element from first to whichever we look for. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #21 | |
| * Death to Visual Basic * Join Date: Aug 2001
Posts: 768
| Quote:
But when I write the char *name to the file it writes the pointer's address (as it should). How can I write the string itself?
__________________ "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe http://www.Bloodware.net - Developing free software for the community. | |
| Devil Panther is offline | |
| | #22 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
Code: struct stuff {
int nameSize;
char *name;
int num;
};
...
void writestuff(FILE *f, stuff *st)
{
fwrite(&st->nameSize, 1, sizeof(st->nameSize), f);
fwrite(st->name, 1, st->nameSize, f);
fwrite(&st->num, 1, sizeof(st->num), f);
}
[Note that I've assumed that nameSize includes the terminating zero. If it doesn't, you have to add one to it when reading and writing] -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #23 | |||
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Quote:
Code: struct stuff
{
int nameSize;
char *name;
int num;
};
Quote:
Quote:
| |||
| itCbitC is offline | |
| | #24 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #25 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #26 | |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Quote:
| |
| itCbitC is offline | |
| | #27 |
| * Death to Visual Basic * Join Date: Aug 2001
Posts: 768
| Thank you for all your help
__________________ "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe http://www.Bloodware.net - Developing free software for the community. |
| Devil Panther is offline | |
![]() |
| Tags |
| binary, file, fwrite, malloc, string |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help with comparing strings (I'm a beginner C programmer) | Bandizzle | C Programming | 2 | 04-29-2009 10:13 AM |
| Help with calloc and pointers to strings. | sweetarg | C Programming | 1 | 10-24-2005 02:28 PM |
| operator overloading and dynamic memory program | jlmac2001 | C++ Programming | 3 | 04-06-2003 11:51 PM |
| Dynamic Toolbars | nvoigt | Windows Programming | 1 | 01-11-2002 10:21 AM |
| array of strings + more | null | C Programming | 10 | 10-01-2001 03:39 PM |