how to cut a string in C -
i have document telephone number , andresses. try copy numbers in 1 part of struct , adress another. @ moment able data of document can't put in struct please me
c code
#define _crt_secure_no_warnings #include <stdio.h> #include <string.h> struct telefon{ char nummer[16]; char adresse[128]; }; typedef struct telefon telefon; void main() { telefon tel; char buffer[256]; file *fp; int = 0; int countsemi = 0; fp = fopen("telefondatei.txt", "r"); if(fp == null) { printf("datei konnte nicht geoeffnet werden.\n"); } else{ while(fgets(buffer,1000,fp) != 0){ //printf("%s\n",buffer); while(buffer != 0){ i++; if(buffer[i] == ';'){ countsemi++; } while(countsemi <= 7){ strcpy(tel.adresse,buffer); printf("%s\n %d \n",tel.adresse,countsemi); } } } } }
example data in .txt document
"firma";"";"auto gmbh";"gasse 3";"5000";"mon";"";"0456";"45652" "firma";"";"adac";"";"50000";"mon";"";"2156";"545218"
you need use strtok
additionally. see example. however, please note example assumes data written in fixed format (and not work if data comes in other format - might want modify needs, illustration):
assumed data format each line:
address;telephonenumber;
#include <string.h> .. char * value; while(fgets(buffer,256,fp) != 0) { value = strtok(buffer, ";"); // address strcpy(tel.adresse, value); value = strtok(null, ";"); // number strcpy(tel.nummer, value); }
also this:
while(buffer != 0)
in code doesn't make sense. hardly buffer
0. array , value of buffer
memory address array starts. can't assign buffer
.
here post using strtok
.
Comments
Post a Comment