How do I split a string by character position in c -
i'm using c read in external text file. input not great , like;
0paul 22 acacia avenue 02/07/1986rn666
as can see have no obvious delimeter, , values have no space between them. know how long in character length each value should when split. follows,
id = 1 name = 20 house number = 5 street name = 40 date of birth = 10 reference = 5
i've set structure want hold information in, , have tried using fscanf read in file. find along lines of isn't doing need,
fscanf(file_in, "%1d, %20s", person.id[i], person.name[i]);
(the actual line use attempts grab input should see i'm going...)
the long term intention reformat input file output file made little easier on eye.
i appreciate i'm going wrong way, hugely appreciate if set me on right path. if you're able take easy on me in regard obvious lack of understanding, i'd appreciate also.
thanks reading
if have fixed column widths, can use pointer arithmetic access substrings of string str
. if have starting index begin
,
printf("%s", str + begin) ;
will print substring beginning @ begin
, end. if want print string of length
, can use printf
's precision specifier .*
, takes maximum length additional argument:
printf("%.*s", length, str + begin) ;
if want copy string temporary buffer, use strncpy
, generate null terminated string if buffer larger substring length. use snprintf
according above pattern:
char buf[length + 1]; snprintf(buf, sizeof(buf), "%.*s", length, str + begin) ;
this extract leading , trailing spaces, not want. write function strip unwanted whitespace; there should plenty of examples here on so.
you strip whitespace when copying substring. example code below isspace
function/macro <ctype.h>
:
#include <stdlib.h> #include <stdio.h> #include <ctype.h> int extract(char *buf, const char *str, int len) { const char *end = str + len; int tail = -1; int = 0; // skip leading white space; while (str < end && *str && isspace(*str)) str++; // copy string while (str < end && *str) { if (!isspace(*str)) tail = + 1; buf[i++] = *str++; } if (tail < 0) tail= i; buf[tail] = '\0'; return tail; } int main() { char str[][80] = { "0paul 22 acacia avenue 02/07/1986rn666", "1bob 1 polk st 01/04/1988rn802", "2alice 99 west highland causeway 28/06/1982rn774" }; int i; (i = 0; < 3; i++) { char *p = str[i]; char id[2]; char name[20]; char number[6]; char street[35]; char bday[11]; char ref[11]; extract(id, p + 0, 1); extract(name, p + 1, 19); extract(number, p + 20, 5); extract(street, p + 25, 34); extract(bday, p + 59, 10); extract(ref, p + 69, 10); printf("<person id='%s'>\n", id); printf(" <name>%s</name>\n", name); printf(" <house>%s</house>\n", number); printf(" <street>%s</street>\n", street); printf(" <birthday>%s</birthday>\n", bday); printf(" <reference>%s</reference>\n", ref); printf("</person>\n\n"); } return 0; }
there's danger here, however: when access string @ position str + pos
should make sure don't go beyond actual string length. example, string may terminated after name. when access birthday, access valid memory, might contain garbage.
you can avoid problem padding full string spaces.
Comments
Post a Comment