My code looks like that:
char *token;
token = strtok(myStrings, delimiter);
char newArray[MAXCHARS];
while (token != NULL) {
printf("%s\n", token);
strcpy(newArray, &token);
token = strtok(NULL, delimiter);
}
I want to add the token to my char array but it doesn't work.
It prints the right token (e.g. Martin) but I can't add it to my char array.
Related
I am doing a project which involves using .csv files handling in c. I have been bothered with this question and can't find a solution, I want to know how to edit/ update my csv file.
My csv file is something like:
Username, Pin, Balance
Right now I am able to get the contents of the csv file, but I am struggling on how to update the contents.
I want to make it so that a username can change its pin, so basically changing the value at pin to be something else.
After reading the comments I have tried the replacing method, it goes like this.
void change_pin(char *C){
char line[200];
char user[50];
char date[50];
char action[50];
char user_n[50];
char date_n[50];
char action_n[50];
FILE *str = fopen("Example.csv", "r+");
FILE *new = fopen("new.csv", "w");
if (str == NULL) {
printf("Error opening file");
fclose(str);}
if (new == NULL) {
printf("Error opening file");
fclose(new);}
while(fgets(line, 200, str)){
char *token;
token = strtok(line, ",");
while (token != NULL){
if (strcmp(token, C) == 0){
strcpy(user, token);
token = strtok(NULL, ",");
strcpy(date, token);
token = strtok(NULL, ",");
strcpy(action, token);
token = strtok(NULL, ",");
strcpy(date, C);
fprintf(new, "%s,%s,%s\n", user, date, action);
}
else{
strcpy(user_n, token);
token = strtok(NULL, ",");
strcpy(date_n, token);
token = strtok(NULL, ",");
strcpy(action_n, token);
token = strtok(NULL, ",");
fprintf(new, "%s,%s,%s\n", user_n, date_n, action_n);
}
}
}
remove("Example.csv");
rename("new.csv", "Example.csv");
fclose(str);
fclose(new);
}
The strcpy (date, C) is just for testing
This works!, but the problem is it doesn't work the second time, I don't know why, when I want to do it the second time it gives a error.
I got it to work now just by removing the '\n' in fprintf. Thank you everyone for helping me out
I have a char array (buf) that exists out of multiple lines and each line is split up by multiple tabs. I want to separate this. I use the following code for this:
char copy[4096];
char* split_request = strtok(buf, "\r\n");
strcpy(copy, split_request);
while(split_request != NULL) {
if (strchr(copy, '\t') != NULL) {
printf("We have a tab");
//If I uncomment this line I get an assertion error
//char* temp = strtok(copy, '\t');
}
printf(split_request);
split_request = strtok(NULL, "\r\n");
if (split_request != NULL) {
strcpy(copy, split_request);
}
printf("\n");
}
If I uncomment that one line of code, only the first line is processed. In addition, it is printed 5 times, and each time one tabbed column disappears. It feels like despite the strcpy, the original string is still affected...
I was experimenting with an alternative approach to your problem. I have used strtok_r for separating lines and each line is processed using strtok for tabs. The code is given below.
void lineParser(char *singleLine){
const char tab[] = "\t";
char *token = NULL;
if(strchr(singleLine, '\t') != NULL){
token = strtok(singleLine, tab);
while(token != NULL){
printf("%s\n", token);
token = strtok(NULL, tab);
}
}
}
int main()
{
char buf[] = "Stack\tOverFlow\r\nStack\tExchange\r\n";
char *rest = buf;;
char* token = NULL;
const char tab[] = "\t";
const char newline[] = "\r\n";
while ((token = strtok_r(rest, " ", &rest))) {
lineParser(token);
token = strtok_r(rest, newline, &rest);
}
}
This question already has answers here:
Nested strtok function problem in C [duplicate]
(2 answers)
What are the differences between strtok and strsep in C
(3 answers)
Closed 5 years ago.
I have a function that takes a string. I make a copy of the string and a char *token;. I say token = strtok(myString, " "); and then start a while loop with while(token != NULL) {...}, and I do some stuff in the while loop. At the end of the while loop I say token = strtok(NULL, " "); which seems to follow examples I've seen online. My input string is "this is a string I will mess with" and my first token is "this" which is great, but then after I say token = strtok(NULL, " "); token appears to be "(null)" instead of "is". I am wondering why. Here is my code:
int numReps2(char *s) {
printf("inside numReps2 with \"%s\"\n", s);
// iterate through s until you find a space
// do matchingwords with the word and an accumulator string that you build char x char
int l = mystrlen(s);
char *copy = malloc(1+(sizeof(char) * l));
mystrcpy(copy, s);
char *accu = malloc(1+(sizeof(char) * l));
int ret = 0;
printf("about to tokenize \"%s\"\n", copy);
char *token;
const char delim[2] = " ";
token = strtok(copy, delim);
while(token != NULL) {
if(matchingWords(accu, copy)) {
printf("match-> accu is \"%s\" and token is \"%s\"\n", accu, token);
// we have a match
ret++;
} else {
// no match, add to accu
char *temp = mystrappend(token, " "); // stick a space after it
printf("no match, adding \"%s\" to \"%s\", token is still \"%s\"\n", temp, accu, token);
accu = mystrappend(accu, temp);
printf("accu is now \"%s\"\n", accu);
free(temp);
printf("after freeing temp, token is \"%s\"\n", token);
}
printf("here is what's after token: %c\n", *(token+strlen(token)+2));
token = strtok(NULL, delim);
printf("just tok'd again, now it's \"%s\"\n", token);
}
free(copy);
free(accu);
return ret;
}
what I want to do is given an input string, which I will not know it's size or the number of tokens, be able to print it's last token.
e.x.:
char* s = "some/very/big/string";
char* token;
const char delimiter[2] = "/";
token = strtok(s, delimiter);
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, delimiter);
}
return token;
and i want my return to be
string
but I what I get is (null). Any workarounds? I've searched the web and can't seem to find an answer to this. At least for C programming language.
If you tokenize on a specific character, i.e. '/' in your example, you do not need to tokenize the string at all: call strrchr to find the position of the last '/', and add 1 to the resultant pointer to skip the delimiter, like this:
char *s = "some/very/big/string";
char *last = strrchr(s, '/');
if (last != NULL) {
printf("Last token: '%s'\n", last+1);
}
Demo.
Just use another variable to store last token before it gets null
char s[] = "some/very/big/string";
char * token, * last;
last = token = strtok(s, "/");
for (;(token = strtok(NULL, "/")) != NULL; last = token);
printf("%s\n", last);
I want to split strings received from the terminal input, if they are contained on a buffer. If they are I want to print them.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* fich[5]={"ha","he","hi","ho","hu"};
int main(){
char passaarg[70];
const char space[2]= " ";
char *token;
int i;
while(1){
read(STDIN_FILENO, passaarg, 70);
for (i = 0; i < 5; i++){
if(strncmp(passaarg, fich[i], 2) == 0){
token = strtok(passaarg, space);
while(token != NULL){
printf("%s\n", token);
token = strtok(NULL, space);
printf("%s\n", token);
}
break;
}
}
}
return 0;
}
My output is the following one:
ha he
ha
he
he
Segmentation fault (core dumped)
I suspect your problem is here:
token = strtok(passaarg, space);
while(token != NULL){
printf("%s\n", token);
token = strtok(NULL, space);
printf("%s\n", token);
}
That second printf will cause undefined behavior (likely a crash) when strtok returns NULL, as it will when there are no more tokens in the string. Just remove that line.
Stylistically, I'd use a for loop here:
for(token = strtok(passaarg, space); token != NULL; token = strtok(NULL, space)) {
printf("%s\n", token);
}
while(token != NULL){
printf("%s\n", token);
token = strtok(NULL, space);
}
The while loop will fail when the token is NULL. At this time you are trying to print this pointer using your second printf() in the while loop which will lead to undefined behavior.
Get rid of your second printf()