c - strtok not discarding the newline character -
so have input file bunch of names , numbers. started using strtok break string can extract data out of each string. seems working correctly, reason it's not discarding newline character.
int procfile(person **data, file* fpfile) { // local declaration char temp[1000]; char proc[15]; char *entry; char *loc; int success = 0; // statement if(fgets(temp, sizeof(temp), fpfile)) { (*data) = alomem(); // free entry = temp; loc = strtok(entry, " ()-"); strcpy(proc, loc); loc = strtok(null, " ()-"); strcat(proc, loc); loc = strtok(null, " ()-"); strcat(proc, loc); sscanf(proc, "%ld", &(*data)->phone); loc = strtok(null, "\0"); strcpy((*data)->name, loc); success++; printf("%s1", (*data)->name); } return success; }// procfile
i tried printing results see if it's working correctly , output.
brown, joanne 1south, frankie 1lee, marie 1brown, joanne 1trapp, ada eve 1trapp, david 1white, d. robert 1lee, victoria 1marcus, johnathan 1walljasper, bryan 1trapp, ada eve 1brown, joanne 1andrews, daniel
it's printing 1
after each name on newline, rather right after name. can explain me how can fix problem?
before tokenizing temp
, rid of newline follows:
char *newline = strchr( temp, '\n' ); if ( newline ) *newline = 0;
strchr
searches temp
newline character, , returns pointer (or null if newline character isn't found). overwrite newline 0 (string terminator).
Comments
Post a Comment