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.
116 messages · Page 1 of 1 (latest)
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.
would sscanf() work
i tried this
but its not the output that i want
this is how the output should look like
can anyone help
Why are you trying to replace strtok?
(i mean, i know a few good reasons, just want to know yours)
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
Hmm instead of writing your own alternative, why not use strtok_r?
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?
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?
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
Oh wait! I think i get what you want to do
You print integers with %d. If you enable compiler warnings it should tell you about it
So you want to parse a line
text more text some words 123 456
Into text part and two numbers?
exactly
so what im doing is, reading a text file that has _ which is being replaced with spaces
Hmm let me think for a minute
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
please how, ive been dying
Like that, that's all
Just have a buffer long enough for the string
where do i put those
Do you know how to use scanf?
in which part of the code
nope thats why im asking
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
sscanf instead of parsing standard input, parses a string that's already in memory
wouldnt that be better to use
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
really
Mhm
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;
}
!format
@neat oriole, your formatted code (missing deletion permissions):
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
i dont think that would work
im basically just organizing this file
I tell you that would work
could i ask what am i putting in the ...
where is int for num
Well i told you already... I'm on a phone and not with a lot of time
im a beginner, apologies
well, where's the problem?
so basically im trying to replace strtok() with something else
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()
strtok will modify the original string
Man, I gave you a solution already. Look up how scanf works on examples and apply it
i looked stackoverflow and everything, i dont see how it would work in 5 lines or so
i give up
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
thats what i did here
That's not a valid code.. you can't define a function inside of a function
how come it works
A GCC extension i guess. Still not a valid C
return null by strtok
like that?
0 is basically NULL
• 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
```cpp
int main() {
return 0;
}
```
int main() {
return 0;
} ```
And then our tools will do it
#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;
}
!format
@neat oriole, your formatted code (missing deletion permissions):
why you use strlen ?
to obtain the string
sorry bro i swear im not dumb but c makes me feel dumb
that is in array
I understand it, it does it to most
A few things: main should return 0 when everything works as expected
ret 2 ?
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
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
@neat oriole Has your question been resolved? If so, run !solved :)
fucks sake i cant get it