PDA

View Full Version : Null Terminated Strings


nmailey
06-26-2003, 04:05 PM
Hey there...

I'm attempting to parse a fixed width ascii file to a file with each field delimited with a null character?

Any suggestions on how to go about this?

I'm currently attempting to append the nulls to each 'field' I extract and fwrite them to the new file but it doesn't seem to be working.

Any suggestions?

sicarius
06-26-2003, 04:27 PM
Well. C and C++ use the null terminator at the end of a string more for internal book keeping than actual display. Every string in C and C++ (aside from those you make your self) will have a null terminator at the end already. So adding another one doesn't do very much.

Functions like fwrite won't even put the null character in the output stream as far as I know so you will have trouble using that in a text file. Why not use tab delimited fields? Or some other character perhaps?

The problem of course becomes that you then can't use your delimiter character in a field itself...something that you may or may not be OK with.

nmailey
06-26-2003, 04:42 PM
Well, that's what I was thinking too.. (using something other than null). But, this is what is recommended when working with variable length strings (for PalmOS development).

I'm currently delimiting with the pipe character '|'
I think this might be good enough as it will take up the same amount of space no?

Thanks for your reply...


btw... your sig kicks a**

sicarius
06-26-2003, 04:59 PM
I supose it would, I've never developed on a Palm though. Those little guys might be a lot different.


Whats wrong with my sig? Did it get you? :)

I know my avatar is all screwy...but didn't know my sig sucked :)

Edit: I should note I pulled that sig off someone from /. I am not creative enough to do that by myself :)

nmailey
06-26-2003, 05:07 PM
Cool.. thanks, I'll look into it more and see if it makes a diff...

Whats wrong with my sig? Did it get you?
I meant that I thought it was awesome! I'm going to make that as a poster on my cubicle. Heh heh...

Thanks again.

skidooer
06-26-2003, 06:58 PM
Originally posted by nmailey
I'm currently delimiting with the pipe character '|'
I think this might be good enough as it will take up the same amount of space no?
| isn't ideal because you might want to put a | in the delmited strings.

This code will read your \0 delimited file. It assumes you are using C, and it tells you want you need to change if you are using Windows.

#include <stdio.h>

char * nextfield(char *p)
{
++p;
while(*p) ++p;
return p + 1;
}

int main(int argc, char **argv)
{
int len;
char buffer[1024];
FILE *f;

if(argc < 2) {
fprintf(stderr, "Usage: %s file\n", argv[0]);
return 1;
}

f = fopen(argv[1], "r"); /* If you are using Windows, use "rb" */
if(f == NULL) {
fprintf(stderr, "Unable to open %s\n", argv[1]);
return 1;
}

while(len = fread(buffer, 1, sizeof(buffer), f)) {
int i = 1;
char *p = buffer;

while(p < buffer + len) {
printf("Field %i: %s\n", i++, p);
p = nextfield(p);
}
}

fclose(f);

return 0;
}

nmailey
06-27-2003, 10:53 AM
Thanks, I'll try that snippet out.

But, how do I create a 'null' delimited ascii file? Or do I have to convert it all to hex first and back again.

i.e.
From this: 89763789 WIDGET EA
73123223 FLANGLE BX

To this: 89763789NULLWIDGETNULLEA
73123223NULLFLANGLENULLBX

skidooer
06-27-2003, 09:52 PM
Originally posted by nmailey

But, how do I create a 'null' delimited ascii file? Or do I have to convert it all to hex first and back again.

i.e.
From this: 89763789 WIDGET EA
73123223 FLANGLE BX

To this: 89763789NULLWIDGETNULLEA
73123223NULLFLANGLENULLBX

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
int fields = 6;
char strings[][100] = {
"89763789", "WIDGET", "EA",
"73123223", "FLANGLE", "BX"
};

FILE *f = fopen(argv[1], "w"); /* Use "wb" on Windows */
for(int i = 0; i < fields; i++) {
/* Using strlen + 1 so I include the \0 character */
fwrite(strings[i], 1, strlen(strings[i]) + 1, f);
}
fclose(f);

return 0;
}

Not exactly what you want, but it goes along with the other example. Basically what you need to add is a row seperator \n seems logical for that. And write it with one/read it with the other.

nmailey
06-30-2003, 10:06 AM
Interesting. I had something similar going and it didn't seem to work. I tried yours and worked like a charm. Thanks! :D