C strtok token manipulation - c

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.

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)
}

Segmentation fault (core dumped) c

Here is a weird problem:
token = strtok(NULL, s);
printf(" %s\n", token); // these two lines can read the token and print
However!
token = strtok(NULL, s);
printf("%s\n", token); // these two lines give me a segmentation fault
Idk whats happened, because I just add a space before %s\n, and I can see the value of token.
my code:
int main() {
FILE *bi;
struct _record buffer;
const char s[2] = ",";
char str[1000];
const char *token;
bi = fopen(DATABASENAME, "wb+");
/*get strings from input, and devides it into seperate struct*/
while(fgets(str, sizeof(str), stdin)!= NULL) {
printf("%s\n", str); // can print string line by line
token = strtok(str, s);
strcpy(buffer.id, token);
printf("%s\n", buffer.id); //can print the value in the struct
while(token != NULL){
token = strtok(NULL, s);
printf("%s\n", token); // problem starts here
/*strcpy(buffer.lname, token);
printf("%s\n", buffer.lname); // cant do anything with token */
}}
fclose(bi);
return 1;}
Here is the example of string I read from stdin and after parsed(I just tried to strtok the first two elements to see if it works):
<15322101,MOZNETT,JOSE,n/a,n/a,2/23/1943,MALE,824-75-8088,42 SMITH AVENUE,n/a,11706,n/a,n/a,BAYSHORE,NY,518-215-5848,n/a,n/a,n/a
<
< 15322101
< MOZNETT
In the first version your compiler transforms printf() into a
puts() and puts does not allow null pointers, because internally
invokes the strlen() to determine the lenght of the string.
In the case of the second version you add a space in front of format
specifier. This makes it impossible for the compiler to call puts
without appending this two string together. So it invokes the actual
printf() function, which can handle NULL pointers. And your code
works.
Your problem reduces to the following question What is the behavior of printing NULL with printf's %s specifier?
.
In short NULL as an argument to a printf("%s") is undefined. So you need to check for NULL as suggested by #kninnug
You need to change you printf as follows:
token = strtok(NULL, s);
if (token != NULL) printf("%s\n", token);
Or else
printf ("%s\n", token == NULL ? "" : token);

Only getting 2 tokens per line from strtok()

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

Not able to split input string from the given format in c?

I was trying to take out individual digits from input string like 1 2 3 4,5 6 7 8,9 10 11 12 in c program by splitting using strtok() function. For this i wrote below programming but it was reading till first comma , delimiter( Note : input size can vary. like in given example comma is after 4 digits but it can be after k(5,6,7, etc) digits based on give testcases ).
fgets(str,80,stdin);
/* read str with comma(,)delimiter */
token = strtok(str, ",");
/* walk through other tokens */
while( token != NULL )
{
// read token string with space delimiter
token2 = strtok(token, " ");
while( token2 != NULL )
{
printf("%s \n", token2);
token2 = strtok(NULL, " ");
}
token = strtok(NULL, ",");
}
strtok is not reentrant -- you can only tokenize one string at a time. If you want to simultaneously tokenize multiple strings at a time, use strtok_r instead. Better yet, ALWAYS use strtok_r in preference to strtok, as it is never less capable:
char *inner, *outer;
fgets(str,80,stdin);
/* read str with comma(,)delimiter */
token = strtok_r(str, ",", &outer);
/* walk through other tokens */
while( token != NULL )
{
// read token string with space delimiter
token2 = strtok_r(token, " ", &inner);
while( token2 != NULL )
{
printf("%s \n", token2);
token2 = strtok_r(NULL, " ", &inner);
}
token = strtok_r(NULL, ",", &outer);
}
You might also want to investigate strsep as well.
The implementation of strtok works with the last string that was passed to it that was not NULL.
Hence, the line:
token = strtok(NULL, ",");
does not work.
If you know the exact number of tokens, you can use sscanf.
/* read str with comma(,)delimiter */
token = strtok(str, ",");
/* walk through other tokens */
while( token != NULL )
{
int num1;
int num2;
int num3;
int num4;
if ( sscanf(token, "%d %d %d %d", &num1, &num2, &num3, &num4) != 4 )
{
// Problem.
}
else
{
// Use the numbers
}
token = strtok(NULL, ",");
}

Reading in file and using Strtok, some fields read successfully, some fields do not

I'm making a function to read in data from a file. The file input is a name and and ID#.
Example:
"George Washington, 2345678
John Adams, 3456789
Thomas Jefferson, 4567890"
I can read in all the names correctly, but the id numbers do not read correctly. The numbers that are read in are all right around 2682824. None of the ID's are close to that. Sometimes the numbers go up or down by 5 but stay around those numbers. Here is the code I have now. Any help would be greatly appreciated.
void readDataIn(){
struct pres tempFill[30];
char s[2] = ",";
char t[50], j[1900];
char *token, *token2;
int *h;
int i = 0, f;
FILE* file;
if((file=fopen("AssignmentOneInput.txt", "r")) != NULL){
while(fgets(j, sizeof(j), file)){
token = strtok(j, ",");
printf("%s\n", token);
token = strtok(NULL, "\n");
printf(" %i\n", token);
}
pause;
}
pause;
fclose(file);
return;
}
When you call strtok second time in the while loop, pass NULL as the argument instead of j.
token = strtok(NULL, "\n");
When you pass NULL in the first argument, strtok resumes parsing from the last token. When you pass j, it parses starting from j again.
Please check out strtok documentation.
The other problem is that you are using
printf(" %i\n", token);
token is still a string. "%i" is not the right format specifier to use here. You need to use:
printf(" %s\n", token);

Resources