#help replacing strtok() into something else

116 messages · Page 1 of 1 (latest)

candid atlasBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

neat oriole
#

would sscanf() work

neat oriole
#

i tried this

#

but its not the output that i want

#

this is how the output should look like

#

can anyone help

solemn dome
#

Why are you trying to replace strtok?

#

(i mean, i know a few good reasons, just want to know yours)

neat oriole
#

ive heard that its bad and not safe to use so i want an alternative

#

but i cant seem to solve it

#

its taking me hours and i still cant figure it out

solemn dome
#

Hmm instead of writing your own alternative, why not use strtok_r?

neat oriole
#

apparently its "needlessly" difficult to do so i hard sscanf() is good to use in this situation

#

would u like to take a look at the whole code?

solemn dome
#

Dunno, i don't use C that often so I'm not sure about my advice here.

#

What strikes me as odd, why are you trying to print integers as %s?

neat oriole
#

if ur talking about using it in strtok() bcuz the output is how i wanted it
if ur talking abt sscanf then to be honest, just trial and error

solemn dome
#

Oh wait! I think i get what you want to do

solemn dome
#

So you want to parse a line
text more text some words 123 456
Into text part and two numbers?

neat oriole
#

exactly

#

so what im doing is, reading a text file that has _ which is being replaced with spaces

solemn dome
#

Hmm let me think for a minute

neat oriole
solemn dome
#

Oh wait, then it's simple!

#

Just use scanf

#

%s %d %d

#

That's all

neat oriole
#

this is how the file is and so all what im doing is basically
Quest Name Quest Level Experience

Destroy the worms in Princess Bubblegum's basement 1 10

neat oriole
solemn dome
#

Just have a buffer long enough for the string

neat oriole
#

where do i put those

solemn dome
#

Do you know how to use scanf?

neat oriole
neat oriole
solemn dome
#

Ok, then let's go back to basics :p

#

Scanf takes a "format string", that describes what kinds of values it will be parsing, and then pointers to where the parsed values will be stored

#

%s parses a single word

#

%d an integer number

neat oriole
#

so how would i implement that

#

what about sscanf

solemn dome
#

sscanf instead of parsing standard input, parses a string that's already in memory

neat oriole
#

wouldnt that be better to use

solemn dome
#

I guess if you open this file,a better solution would be to use fscanf

#

That parses a file

#

Overall what you are trying to do should be 4 lines total

neat oriole
#

really

solemn dome
#

Mhm

neat oriole
#

wth how

#

i have about ~40 lines of code

#

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//first things first, file pointer so program will keep track of file
FILE* file_to_read;
//defining variables
double quest_level,quest_experience;
int del_underscores;
char reference_of_arrays[80], *y, into_spaces;
int main() {
//open file as specfied which is in read mode
file_to_read=fopen("quests.txt","r");
//print out the name,level,xp with the appropriate spacing
printf("%-51s", "Quest Name"), printf(" Quest Level"" Experience\n\n");
//void is used so it doesnt return a value
void replace_scores_wspaces(char into_spaces[]){
//here we are replacing the underscores with spaces
for(del_underscores=1;del_underscores<strlen(into_spaces);del_underscores++){
if(into_spaces[del_underscores]=='_'){
//if there is underscores then replace it with a space
into_spaces[del_underscores]=' ';}}
}

//using function fgets() to read string from console
while((fgets(reference_of_arrays,80,file_to_read)!=0))
//read the rest of file and put the appropriate spacing
//for name
{y=strtok(reference_of_arrays," "),replace_scores_wspaces(y),printf("%-51s ",y);
//the empty spacing is to organize the output prompt as well as for strtok's arguements
//for level
y=strtok(0," "),printf("%-14s",y);
//for xp
y=strtok(0,""),printf("%-s",y);
}
/**while(fgets(reference_of_arrays,80,file_to_read))
{
int len=strlen(reference_of_arrays);
if(reference_of_arrays[len-1]=='\n')
reference_of_arrays[len-1]='\0';
sscanf(file_to_read,"%d %d\n", &quest_level, &quest_experience);
printf("%-51s %-14s %-s \n", reference_of_arrays, quest_level, quest_experience);
}*/

printf("\n");
//close file
fclose(file_to_read);
//return true when everything works as expected
return 2;
}

charred anchorBOT
#

@neat oriole, your formatted code (missing deletion permissions):

solemn dome
#
f = fopen(...
while(fscanf("...", ...) == 3)
{
    replace_underscore(text);
    printf("...", ...);
}
fclose();
#

*scanf returns the number of correctly parsed arguments

#

so as long, as it can parse new word and two new integers, you replace the underscore in integers and print as you want

neat oriole
#

i dont think that would work

neat oriole
solemn dome
#

I tell you that would work

neat oriole
#

could i ask what am i putting in the ...

molten dew
#

where is int for num

solemn dome
#

Well i told you already... I'm on a phone and not with a lot of time

neat oriole
neat oriole
molten dew
#

well, where's the problem?

neat oriole
#

so strtok(), this is my output (which is what i want)

#

i tried an alternative and this is what im getting

#

which isnt the correct output

#

so the problem is basically trying to replace strtok()

molten dew
#

strtok will modify the original string

solemn dome
#

Man, I gave you a solution already. Look up how scanf works on examples and apply it

neat oriole
#

i give up

solemn dome
#

It was an oversimplification. You need a separate function to replace _ with space

#

And then just put variable names into my code and be done with it

solemn dome
#

That's not a valid code.. you can't define a function inside of a function

neat oriole
#

how come it works

solemn dome
#

A GCC extension i guess. Still not a valid C

molten dew
neat oriole
neat oriole
neat oriole
solemn dome
#

God please format your code

#

If you don't know how put it here like that

charred anchorBOT
#
How to Format Code on Discord

• type three "backticks" (not quotes/apostrophes, on QWERTY layout, left of 1-key)
• on the same line, type the file extension for that language (c or cpp)
• enter your code on a new line and put another three backticks at the end

Example Input

```cpp
int main() {
    return 0;
}
```

Example Output
int main() {
    return 0;
} ```
solemn dome
#

And then our tools will do it

neat oriole
#

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//first things first, file pointer so program will keep track of file
FILE* file_to_read;
//defining variables
double quest_level,quest_experience;
int del_underscores;
char reference_of_arrays[80], *y, into_spaces;
int main() {
//open file as specfied which is in read mode
file_to_read=fopen("quests.txt","r");
//print out the name,level,xp with the appropriate spacing
printf("%-51s", "Quest Name"), printf(" Quest Level"" Experience\n\n");
//void is used so it doesnt return a value
void replace_scores_wspaces(char into_spaces[]){
//here we are replacing the underscores with spaces
for(del_underscores=1;del_underscores<strlen(into_spaces);del_underscores++){
if(into_spaces[del_underscores]=='_'){
//if there is underscores then replace it with a space
into_spaces[del_underscores]=' ';}}
}

//using function fgets() to read string from console
/**while((fgets(reference_of_arrays,80,file_to_read)!=0))
//read the rest of file and put the appropriate spacing
//for name
{y=strtok(reference_of_arrays," "),replace_scores_wspaces(y),printf("%-51s ",y);
//the empty spacing is to organize the output prompt as well as for strtok's arguements
//for level
y=strtok(0," "),printf("%-14s",y);
//for xp
y=strtok(0,""),printf("%-s",y);
}*/
while(fgets(reference_of_arrays,80,file_to_read))
{
int len=strlen(reference_of_arrays);
if(reference_of_arrays[len-1]=='\n')
reference_of_arrays[len-1]='\0';
sscanf(file_to_read,"%d %d\n", &quest_level, &quest_experience);
printf("%-51s %-14s %-s \n", reference_of_arrays, quest_level, quest_experience);
}

printf("\n");
//close file
fclose(file_to_read);
//return true when everything works as expected
return 2;
}

charred anchorBOT
#

@neat oriole, your formatted code (missing deletion permissions):

molten dew
#

why you use strlen ?

neat oriole
#

to obtain the string

neat oriole
neat oriole
solemn dome
#

I understand it, it does it to most

#

A few things: main should return 0 when everything works as expected

molten dew
#

ret 2 ?

solemn dome
#

0 is for successful exit, 1 for error

#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// first things first, file pointer so program will keep track of file
FILE* file_to_read;
// defining variables
double quest_level, quest_experience;
int del_underscores;
char reference_of_arrays[80], *y, into_spaces;
int main() {
    // open file as specfied which is in read mode
    file_to_read = fopen("quests.txt", "r");
    // print out the name,level,xp with the appropriate spacing
    printf("%-51s", "Quest Name"), printf(
                                       " Quest Level"
                                       "   Experience\n\n");
    // void is used so it doesnt return a value
    void replace_scores_wspaces(char into_spaces[]) {
        // here we are replacing the underscores with spaces
        for (del_underscores = 1; del_underscores < strlen(into_spaces);
             del_underscores++) {
            if (into_spaces[del_underscores] == '_') {
                // if there is underscores then replace it with a space
                into_spaces[del_underscores] = '  ';
            }
        }
    }

    // using function fgets() to read string from console
    /**while((fgets(reference_of_arrays,80,file_to_read)!=0))
        //read the rest of file and put the appropriate spacing
        //for name
    {y=strtok(reference_of_arrays," "),replace_scores_wspaces(y),printf("%-51s
    ",y);
        //the empty spacing is to organize the output prompt as well as for
    strtok's arguements
        //for level
    y=strtok(0,"    "),printf("%-14s",y);
        //for xp
    y=strtok(0,""),printf("%-s",y);
    }*/
    while (fgets(reference_of_arrays, 80, file_to_read)) {
        int len = strlen(reference_of_arrays);
        if (reference_of_arrays[len - 1] == '\n')
            reference_of_arrays[len - 1] = '\0';
        sscanf(file_to_read, "%d %d\n", &quest_level, &quest_experience);
        printf("%-51s %-14s %-s \n", reference_of_arrays, quest_level,
               quest_experience);
    }

    printf("\n");
    // close file
    fclose(file_to_read);
    // return true when everything works as expected
    return 2;
}
#

Uhhhh

#

Ok, so your replacing function is ok, even if strlen is kinda unnecessary

#

It should be before main

#

Then, you can remove everything you have

#

Create 3 variables, one for text two for numbers

#

Post my code

#

Put in the names of variables

#

And it will work

neat oriole
#

do i not use the variables already created?

#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//first things first, file pointer so program will keep track of file
FILE* file_to_read;
//defining variables
double quest_level,quest_experience;
int del_underscores;
char reference_of_arrays[80], *y, into_spaces;
void replace_scores_wspaces(char into_spaces[]){
        //here we are replacing the underscores with spaces
for(del_underscores=1;del_underscores<strlen(into_spaces);del_underscores++){
if(into_spaces[del_underscores]=='_'){
        //if there is underscores then replace it with a space
 into_spaces[del_underscores]='  ';}}
}
int main() {
    //open file as specfied which is in read mode
file_to_read=fopen("quests.txt","r");
    //print out the name,level,xp with the appropriate spacing
printf("%-51s", "Quest Name"), printf(" Quest Level""   Experience\n\n");
    //void is used so it doesnt return a value



while(fscanf("...", ...) == 3)
{
    replace_underscore(text);
    printf("...", ...);
}
    
    
printf("\n");
    //close file
fclose(file_to_read);
    //return true when everything works as expected
return (0);
}
#

ok so here

candid atlasBOT
#

@neat oriole Has your question been resolved? If so, run !solved :)

neat oriole
#

fucks sake i cant get it

solemn dome
#

Go back to hello world and get a name and age from a user

#

In one scanf