Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
For example it prints '(night' despite tokenizing (), why does this happen?
char* word = strtok(&c, ",.;()");
while(word!= NULL)
{
word = strtok(NULL, ",.;()");
printf("%s ", &c);
}
Your code just prints &c on every iteration (whatever that is). You never print word, which is your next token. That's why you never see the results of your tokenization. If you want to see the tokens, you have to print word, not c.
On top of that it is completely unclear why you are applying & operator to your c. If c is a string pointer or a char array, that & there makes no sense at all.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 26 days ago.
Improve this question
I am using char arr[] insidea struct
I used temporary array to get string from user and it works fine ,
when I try to copy this temporary array to the variable array inside the struct I get \command files at end of string
for (u32 i=0;i<name_size;i++)
{
printf("\nin While");
pn->name[i]=temp_name[i];
}
if you must do it byte by byte then do this
u32 i;
for (i=0;i<name_size;i++)
{
printf("\nin While");
pn->name[i]=temp_name[i];
}
pn->name[i] = '\0';
ie - add the trailing zero. simpler would be strcpy
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
This is input: Francetic, Petra#13/12/1930 Trg zrtava Uskoka 156 (Skopje) 800893452/2008
It wont fscanf properly it show's m=0 and that's how I know that while loop was not successful.
while(fscanf(mrtvaciTxt, "%[^,],%[^#]#%d/%d/%d %[^0-9]%d (%[^)]) %[^/]/%[^\n] ",
&pomrli[m].prezime, &pomrli[m].ime,
&pomrli[m].dan,&pomrli[m].mjesec,&pomrli[m].godina, &pomrli[m].adresa,&pomrli[m].brUlice,
&pomrli[m].brOsobne, &pomrli[m].godSmrti )== 9)
{
m++;
}
printf("%d\n\n", m);
How can I fscanf this and is there any tutorial how can i be better at this because it takes me so much time.
Francetic
Petra
13
12
1930
Trg zrtava Uskoka 156 (Skopje)
800893452
2008, I want fscanf to look like this
There are multiple errors in the scanf format.
Here is the correct scanf format string:
" %[^,], %[^#]#%d/%d/%d %[^(](%[^)]) %[^/]/%d"
Here is the list of errors:
there are 11 % while you expect only 9 fields
lacking a space in front of the format string to consume the newline
there is a / in front of the first %d which shouldn’t be there
you are using a [^\n] where you should use [^(]( for the adresa
you are using %*[\n] for unknown reason
you should use [^)] to get the grad
etc.
you modified you question, so I don’t know what else you did.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This function make a strange error after using it several times and I really can't understand the reason behind it.
char *get_range(char *str,int min,int max){
char *_res=(char *)malloc(sizeof(str));
int cur=0;
while (min<max){
_res[cur]=str[min];
min++;
cur++;
}
return _res;
}
The problem is that after using this function several times, the output comes with additional chars and I don't understand why.
Notice: The additional chars are allway used returned by the function beffor
char *_res=(char *)malloc(sizeof(str));
is wrong. sizeof(str) is measuring the size of a char pointer. This is either 4 or 8 (typically) depending on your system (32 or 64 bit).
You need
char *_res=(char *)malloc(strlen(str) + 1);
strlen returns the number of characters in the string, and you need to add 1 for the terminating 0;
Second you have to add a terminating zero at the end, do:
_res[cur] = '\0';
before returning
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
char ch [1000] ;
while ( fgets(ch,1000,f) !=NULL);
{
printf("%s",ch) ;
}
The file f contains multiple lines but this part of the program only shows the last line
Remove the semicolon here:
while ( fgets(ch,1000,f) !=NULL) ;//This one
The semicolon makes the loop equivalent to
while ( fgets(ch,1000,f) !=NULL)
{}
and therefore, has an empty body. The loop goes on executing the fgets until it returns NULL and when it breaks, ch will contain the last line and this is what your printf prints.
There is an extra ; at the end of the while condition.
Use this style to avoid such mistakes:
while (fgets(ch, 1000, f) != NULL) {
printf("%s", ch);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am comparing two strings each coming from arrays.
while(countX<10){
if(strcmp(scanWord[countX], currentWord[countX]) == 0)
{scoreCurrent++;scoreCurrent++;}
countX++;
}
"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are. It works if I compare things that aren't those and I have printed them to screen to check too. They just don't seem to play well. Thanks in advance.
When you're reading the line, remove the newline:
char *line = fgets(currentWord[countX], 20, stdin);
if (line) {
int len = strlen(line);
if (line[len-1] == '\n') {
line[len-1] = 0;
}
}
The question as I see it:
"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are.
How do you KNOW the two values are the same? Print them out. Print out the length too. If dealing with Unicode of any sort, print out the individual bytes in hexadecimal, because two Unicode characters that look identical may actually be different.
Because the computer will never say the strings are different if they are really the same. The answer to the question as I read it is that the strings actually are different.