Only getting 2 tokens per line from strtok() - c

I'm trying to make tokens from an input file. So, I get one line with fgets and feed it to a helper method that takes in a char* and returns a char* of the token. I am utilizing strtok() with delimiter as " " since the tokens are all separated by " ". But, I can't figure out why the code only makes 2 tokens per line and just moves on to the next line even though there is more in that line needed to be tokenized. Here is the code:
char *TKGetNextToken( char * start ) {
/* fill in your code here */
printf("Entered TKGetNextToken \n");
printf(&start[0]);
char* temp = &start[0];
//Delimiters for the tokens
const char* delim = " ";
//store tempToken
char* tempTok = strtok(temp, delim);
//return the token
return tempTok;
}
Here is how I'm storing the tokens in the main method:
//call get next token and get the token and store into temptok
while (temp!= NULL) {
tempTok = TKGetNextToken(temp);
printf("tempTok: %s\n",tempTok);
token.charPtr[tempNum] = tempTok;
tempNum++;
printf("Temp: %s\n",tempTok);
temp = strtok(NULL, " \0\n");
}
So, lets say I have a file.txt with:
abcd ef ghij asf32
fsadf ads adf
The tokens created would be "abcd" and "ef" and it will go on to the next line without making tokens for "ghij" and "asf32".

Use the proper syntax for strtok
char *tempTok = strtok(line, " "); //initialize
while (tempTok != NULL) {
//do the work
tempTok = strtok(NULL, " \n"); //update
}
If you do like above, then you can get the tokens quite easy. Please have a look at this example, which is similar to your code, just remember how you use strtok properly, then it will work. Look at strtok and how it is used in the loop, updating and consumes the char *.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *fp = fopen("data.txt", "r");
char line[256];
while (fgets(line, sizeof(line), fp)) {
char *tempTok = strtok(line, " ");
while (tempTok != NULL) {
printf("token %s\n", tempTok);
tempTok = strtok(NULL, " \n");
}
}
fclose(fp);
return 0;
}
File data.txt
abcd ef ghij asf32
fsadf ads adf
Output
./a.out
token abcd
token ef
token ghij
token asf32
token fsadf
token ads
token adf

Related

execvp not executing commands given by user input

I am attempting to execute command lines given by user input but for some reason the execvp() function isn't executing the command. I read in a user input and split it, store it into an array and use the excevp() function to execute it. I even printed out the spots in the array to make sure it was placing the token into the right spot and it is. Here is my code in C
char b[100];
int i = 0;
char *token;
char *array[3];
printf("Please enter the command you want to use: ");
fgets(b, 100, stdin);
token = strtok (b, " ");
while (token != NULL){
array[i++] = token;
printf("%s\n",token);
token = strtok(NULL, " ");
}
printf("%s", array[0]);
printf("%s", array[1]);
execvp(array[0], array);
So for example if I were to type in "ls" into the command line in the program and hit enter it will just go to the next line and nothing will execute. Are there any recommendation to fix this because I am lost on where to begin?
The problem is array[0] is ls\n (as fgets reads newline character as well) and not ls you have to remove \n as well.
You can simply create an array of delimiters like this:
char delimiters[] = " \t\n";
and then simply do
token = strtok(b, delimiters); // Use delimiters instead of only " " (whitespace)
while (token != NULL) {
array[i++] = token;
printf("%s\n", token);
token = strtok(NULL, delimiters); // Use delimiters instead of only " " (whitespace)
}

How to use strtok()/strtok_r() to split a line

I am using C to write a basic shell. The structure of the shell is pretty simple:
get input,
parse input,
exe the commemt.
To get the input, my professor requires to use getline(), so the type of return for my input is char *.
I pass the input to function parse_input(line) and use strtok() / strtok_r() to split the input. but I have problem here. I get null when I split. My professor requires me to use strtok() / strtok_r() to split. I have attached an image of my code here. I can split if I use char[] but here is char*. I don't know how to fix this.
char *parse_line(char *line){
int index = 0;
char *token;
// char **tokens = malloc(buffs * sizeof(char*));
//token = strtok_r(line,TOKEN_SPLIT,&line);
token = strtok(line, TOKEN_SPLIT);
printf("token: %s\n",token);
while(token != NULL){
// tokens[index++] = token;
printf(" %s\n", token);
token = strtok(NULL, TOKEN_SPLIT);
}
return token;
}

C - having trouble with strtok

I'm trying to load some highscores of a game to this kind of struct:
typedef struct{
char date_time[20];
int record_minutes;
int record_seconds;
int plays;
} Highscore;
The highscores are stored in a txt file like so: "nrplays minutes seconds date_and_time"
e.g. 15 2 10 31/12/2017-23:00:20
The first bit of the code I am using to read the file (which has only 3 lines, that's why I didn't create a loop) is the following
void loadHighscores(){
FILE *f;
if (check_ifEmptyFile()==-3)
return;
f=fopen("highscores.txt", "r");
char linha[30];
char *token;
High1 = (Highscore *)malloc(sizeof(Highscore));
fgets(linha, 30, f);
printf("linha: %s", linha);
token = strtok(linha, " \n");
High1->plays=atoi(token);
printf("%d\n", High1->plays);
token = strtok(NULL, linha);
High1->record_minutes=atoi(token);
printf("%d\n", High1->record_minutes);
token = strtok(NULL, linha);
High1->record_seconds=atoi(token);
printf("%d\n", High1->record_seconds);
token = strtok(NULL, linha);
snprintf(High1->date_time, 20*sizeof(char), "%s",token);
printf("%s",High1->date_time);
}
The output was this
linha: 15 2 10 31/12/2017-23:00:20
15
2
0
/
which means strtok isn't doing what I intended it to do. Any tips?
Note that High1 was defined previously, the malloc isn't wrong and also that High1->date_time should be the whole 31/12/2017-23:00:20 string.
In strtok() the second parameter should be the delimeter.
So, in your case, should be the character space: ' '.
token = strtok(linha, " ");
and for successive calls:
token = strtok(NULL, " ");
where you parsing every line in linha.

C strtok token manipulation

So i have a program that reads a txt file. Then I use strtok to tokenize the strings using ";" as a separator. The problem is that I have to horizontally format those tokens similar to an sql query. The only thing I get is to make the tokens be printed vertically. I am so confused.
This is my code.
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
int i, j;
FILE *fdin;
char *p;
char s_line[120];
char *token;
const char s[2] = ";";
int main()
{
if ((fdin = fopen("raw14.txt", "r")) == NULL)
return 1;
while (fgets(s_line, sizeof(s_line), fdin))
{
/*get first token */
token = strtok(s_line, s);
p = &s_line[strlen(s_line) - 1];
if (*p < ' ')
*p = '\0';
/*Loop for Serarating*/
while (token){
printf("Description")
printf(" %s\n", token);
token = strtok(NULL, s);
}
}
printf("\n\n");
return 0;
}
The result should look like this:
ITEM ID DESCRIPTION QTY IN QTY OUT BALANCE
240201 AEROSIL | 253.50 231.00 | 22.50
240202 ALCOHOL | 663.00 412.78 | 250.22
the txt part is like that:
240201;AEROSIL;253.5;231
240202;ALCOHOL;663;412.78
Please note that I have no idea how to create an extra balance column and also put the pipelines only with an strtok. I do get that I should also use left side justification and format the printf in order to get the result.
I have to horizontally format those tokens
So why then you print a newline ('\n') after each token?
Instead of
while (token) {
printf("Description")
printf(" %s\n", token);
token = strtok(NULL, s);
}
you might do
while (token) {
printf("Description")
printf(" %s", token);
token = strtok(NULL, s);
}
printf("\n");
You have a \n (newline character) in your printf, so each token is printed on its own line.
Change this
while (token){
printf("Description")
printf(" %s\n", token);
token = strtok(NULL, s);
}
to this
while (token) {
printf(" %s", token);
token = strtok(NULL, s);
}
printf("\n"); // Put the newline character outside the loop.
You should also print the Description ID BALANCE text outside the outer loop.
You're printing a new line each time you're printing the token.
If you want them to print like CHOCOLATE 1 4, just use printf("%s ", token); in your loop. And you shouldn't print Description inside your loop too.

strtok and strncat error

I want to add string "ay" to each word by using both strtok and strncat. But there seemed to be a conflict somewhere that I cannot find. It only gives me the first word "Computeray" for an output. Help?
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[] = "Computer science is hard";
char* Token;
char* work = "ay";
Token = strtok(str, " ");
while (Token != NULL)
{
strncat(Token, work, 2);
printf("%s", Token);
Token = strtok(NULL, " ");
}
return 0;
}
You're modifying the string (with strcat) and expecting strtok to still behave properly - that's not going to work. Instead of using strcat, just print the "ay" separately:
while (Token != NULL)
{
printf("%say ", Token);
Token = strtok(NULL, " ");
}
Even if it were working the way you'd like, you'd be overwriting a bunch of your input along the way. Probably not what you were going for - if you need to build up a whole new string, you should do it into a new buffer, instead of overwriting the input.

Resources