unexpected sign in fprintf, fscanf [duplicate]
This question already has an answer here:
How to read string separated by / with scanf
2 answers
I received data to stream using scanf() and sent it to client.txt using fprintf():
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("clear");
int servicestart;
char tmp1[100], tmp2[100], tmp3[100], tmp4[100], tmp5[100];
puts(">>Library Service<<n1.Register 2.Log in 3.Exit Program");
scanf("%d", &servicestart);
switch (servicestart)
{
case 1:
{
system("clear");
FILE* fp1 = fopen("client.txt", "w");
if (fp1 == NULL)
{
puts("Write Error!!");
return 0;
}
puts("You selected Register");
puts("Enter Student ID | Password | Name | Address | Phonenumber");
puts("ex)2018|ssu|DanielHong|Gaepo-dong|01031414473");
printf("nn");
scanf("%s|%s|%s|%s|%s", tmp1, tmp2, tmp3, tmp4, tmp5);
fprintf(fp1, "%s %s %s %s %s", tmp1, tmp2, tmp3, tmp4, tmp5);
fclose(fp1);
}
return 0;
}
However, when I entered 2018|asdf1234|danielhong|gaepo-dong|01023232323
there was 2018|asdf1234|danielhong|gaepo-dong|01023232323 p ���� in client.txt.
I didn't enter p ���� clearly.
I was in agony about it but don't having any clue about solution.
Can somebody help me for this problem?
c file-io printf scanf fopen
marked as duplicate by Antti Haapala
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 5:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to read string separated by / with scanf
2 answers
I received data to stream using scanf() and sent it to client.txt using fprintf():
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("clear");
int servicestart;
char tmp1[100], tmp2[100], tmp3[100], tmp4[100], tmp5[100];
puts(">>Library Service<<n1.Register 2.Log in 3.Exit Program");
scanf("%d", &servicestart);
switch (servicestart)
{
case 1:
{
system("clear");
FILE* fp1 = fopen("client.txt", "w");
if (fp1 == NULL)
{
puts("Write Error!!");
return 0;
}
puts("You selected Register");
puts("Enter Student ID | Password | Name | Address | Phonenumber");
puts("ex)2018|ssu|DanielHong|Gaepo-dong|01031414473");
printf("nn");
scanf("%s|%s|%s|%s|%s", tmp1, tmp2, tmp3, tmp4, tmp5);
fprintf(fp1, "%s %s %s %s %s", tmp1, tmp2, tmp3, tmp4, tmp5);
fclose(fp1);
}
return 0;
}
However, when I entered 2018|asdf1234|danielhong|gaepo-dong|01023232323
there was 2018|asdf1234|danielhong|gaepo-dong|01023232323 p ���� in client.txt.
I didn't enter p ���� clearly.
I was in agony about it but don't having any clue about solution.
Can somebody help me for this problem?
c file-io printf scanf fopen
marked as duplicate by Antti Haapala
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 5:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
If you mean client.txt , I created it in advance in home directory.
– Daniel Hong
Nov 27 '18 at 1:45
you could find out what the byte values for those pesky characters are using a hex viewer. Also what happens when you make this change:fopen("client.txt","w+");(Note the plus sign)
– HappyKeyboard
Nov 27 '18 at 1:48
I solved the problem from the question below , but I`m curious about what is p ���� too. I got 20 70 03 20 5C 03 20 A0 CB FF FF form hex viewer,
– Daniel Hong
Nov 27 '18 at 2:01
@DanielHong Thats just random data fromtmp2-tmp5that got written without being initialized before. If you had initialized your arrays (char tmp1[100] = { 0 }, tmp2[100] = { 0 }, tmp3[100] = { 0 }, tmp4[100] = { 0 }, tmp5[100] = { 0 };) you wouldn't have seen that garbage.
– Swordfish
Nov 27 '18 at 2:15
I was curious to see if there would be any new line feed or carriage return or line feed characters (you can look up ASCII Table to see what the values area)
– HappyKeyboard
Nov 27 '18 at 2:20
add a comment |
This question already has an answer here:
How to read string separated by / with scanf
2 answers
I received data to stream using scanf() and sent it to client.txt using fprintf():
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("clear");
int servicestart;
char tmp1[100], tmp2[100], tmp3[100], tmp4[100], tmp5[100];
puts(">>Library Service<<n1.Register 2.Log in 3.Exit Program");
scanf("%d", &servicestart);
switch (servicestart)
{
case 1:
{
system("clear");
FILE* fp1 = fopen("client.txt", "w");
if (fp1 == NULL)
{
puts("Write Error!!");
return 0;
}
puts("You selected Register");
puts("Enter Student ID | Password | Name | Address | Phonenumber");
puts("ex)2018|ssu|DanielHong|Gaepo-dong|01031414473");
printf("nn");
scanf("%s|%s|%s|%s|%s", tmp1, tmp2, tmp3, tmp4, tmp5);
fprintf(fp1, "%s %s %s %s %s", tmp1, tmp2, tmp3, tmp4, tmp5);
fclose(fp1);
}
return 0;
}
However, when I entered 2018|asdf1234|danielhong|gaepo-dong|01023232323
there was 2018|asdf1234|danielhong|gaepo-dong|01023232323 p ���� in client.txt.
I didn't enter p ���� clearly.
I was in agony about it but don't having any clue about solution.
Can somebody help me for this problem?
c file-io printf scanf fopen
This question already has an answer here:
How to read string separated by / with scanf
2 answers
I received data to stream using scanf() and sent it to client.txt using fprintf():
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("clear");
int servicestart;
char tmp1[100], tmp2[100], tmp3[100], tmp4[100], tmp5[100];
puts(">>Library Service<<n1.Register 2.Log in 3.Exit Program");
scanf("%d", &servicestart);
switch (servicestart)
{
case 1:
{
system("clear");
FILE* fp1 = fopen("client.txt", "w");
if (fp1 == NULL)
{
puts("Write Error!!");
return 0;
}
puts("You selected Register");
puts("Enter Student ID | Password | Name | Address | Phonenumber");
puts("ex)2018|ssu|DanielHong|Gaepo-dong|01031414473");
printf("nn");
scanf("%s|%s|%s|%s|%s", tmp1, tmp2, tmp3, tmp4, tmp5);
fprintf(fp1, "%s %s %s %s %s", tmp1, tmp2, tmp3, tmp4, tmp5);
fclose(fp1);
}
return 0;
}
However, when I entered 2018|asdf1234|danielhong|gaepo-dong|01023232323
there was 2018|asdf1234|danielhong|gaepo-dong|01023232323 p ���� in client.txt.
I didn't enter p ���� clearly.
I was in agony about it but don't having any clue about solution.
Can somebody help me for this problem?
This question already has an answer here:
How to read string separated by / with scanf
2 answers
c file-io printf scanf fopen
c file-io printf scanf fopen
edited Nov 27 '18 at 1:54
Swordfish
9,65811436
9,65811436
asked Nov 27 '18 at 1:35
Daniel HongDaniel Hong
327
327
marked as duplicate by Antti Haapala
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 5:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Antti Haapala
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 5:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
If you mean client.txt , I created it in advance in home directory.
– Daniel Hong
Nov 27 '18 at 1:45
you could find out what the byte values for those pesky characters are using a hex viewer. Also what happens when you make this change:fopen("client.txt","w+");(Note the plus sign)
– HappyKeyboard
Nov 27 '18 at 1:48
I solved the problem from the question below , but I`m curious about what is p ���� too. I got 20 70 03 20 5C 03 20 A0 CB FF FF form hex viewer,
– Daniel Hong
Nov 27 '18 at 2:01
@DanielHong Thats just random data fromtmp2-tmp5that got written without being initialized before. If you had initialized your arrays (char tmp1[100] = { 0 }, tmp2[100] = { 0 }, tmp3[100] = { 0 }, tmp4[100] = { 0 }, tmp5[100] = { 0 };) you wouldn't have seen that garbage.
– Swordfish
Nov 27 '18 at 2:15
I was curious to see if there would be any new line feed or carriage return or line feed characters (you can look up ASCII Table to see what the values area)
– HappyKeyboard
Nov 27 '18 at 2:20
add a comment |
If you mean client.txt , I created it in advance in home directory.
– Daniel Hong
Nov 27 '18 at 1:45
you could find out what the byte values for those pesky characters are using a hex viewer. Also what happens when you make this change:fopen("client.txt","w+");(Note the plus sign)
– HappyKeyboard
Nov 27 '18 at 1:48
I solved the problem from the question below , but I`m curious about what is p ���� too. I got 20 70 03 20 5C 03 20 A0 CB FF FF form hex viewer,
– Daniel Hong
Nov 27 '18 at 2:01
@DanielHong Thats just random data fromtmp2-tmp5that got written without being initialized before. If you had initialized your arrays (char tmp1[100] = { 0 }, tmp2[100] = { 0 }, tmp3[100] = { 0 }, tmp4[100] = { 0 }, tmp5[100] = { 0 };) you wouldn't have seen that garbage.
– Swordfish
Nov 27 '18 at 2:15
I was curious to see if there would be any new line feed or carriage return or line feed characters (you can look up ASCII Table to see what the values area)
– HappyKeyboard
Nov 27 '18 at 2:20
If you mean client.txt , I created it in advance in home directory.
– Daniel Hong
Nov 27 '18 at 1:45
If you mean client.txt , I created it in advance in home directory.
– Daniel Hong
Nov 27 '18 at 1:45
you could find out what the byte values for those pesky characters are using a hex viewer. Also what happens when you make this change:
fopen("client.txt","w+"); (Note the plus sign)– HappyKeyboard
Nov 27 '18 at 1:48
you could find out what the byte values for those pesky characters are using a hex viewer. Also what happens when you make this change:
fopen("client.txt","w+"); (Note the plus sign)– HappyKeyboard
Nov 27 '18 at 1:48
I solved the problem from the question below , but I`m curious about what is p ���� too. I got 20 70 03 20 5C 03 20 A0 CB FF FF form hex viewer,
– Daniel Hong
Nov 27 '18 at 2:01
I solved the problem from the question below , but I`m curious about what is p ���� too. I got 20 70 03 20 5C 03 20 A0 CB FF FF form hex viewer,
– Daniel Hong
Nov 27 '18 at 2:01
@DanielHong Thats just random data from
tmp2 - tmp5 that got written without being initialized before. If you had initialized your arrays (char tmp1[100] = { 0 }, tmp2[100] = { 0 }, tmp3[100] = { 0 }, tmp4[100] = { 0 }, tmp5[100] = { 0 };) you wouldn't have seen that garbage.– Swordfish
Nov 27 '18 at 2:15
@DanielHong Thats just random data from
tmp2 - tmp5 that got written without being initialized before. If you had initialized your arrays (char tmp1[100] = { 0 }, tmp2[100] = { 0 }, tmp3[100] = { 0 }, tmp4[100] = { 0 }, tmp5[100] = { 0 };) you wouldn't have seen that garbage.– Swordfish
Nov 27 '18 at 2:15
I was curious to see if there would be any new line feed or carriage return or line feed characters (you can look up ASCII Table to see what the values area)
– HappyKeyboard
Nov 27 '18 at 2:20
I was curious to see if there would be any new line feed or carriage return or line feed characters (you can look up ASCII Table to see what the values area)
– HappyKeyboard
Nov 27 '18 at 2:20
add a comment |
1 Answer
1
active
oldest
votes
The format specifier "%s" reads until the next whitespace. The next whitespace in your input is the newline ('n') at the end of the line. Use
scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5);
Instead. % is a scanset that matches all characters in the brackets. If the characters are prepended with a ^ they are excluded.
Please, never use "%s" or a scanset without specifying a width for the conversion specifier to limit the characters maximal read. Since your arrays are 100 chars long specify 99 so there is space left for the terminating '' character.
You should also check, if scanf was successful before proceeding to process the input. scanf() returns the number of conversions successfully performed. So in your case you schould compare the return value with 5:
if (scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5) != 5) {
; // handle error
}
I'm truly greateful for excellent answer for my first question. thx
– Daniel Hong
Nov 27 '18 at 2:11
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The format specifier "%s" reads until the next whitespace. The next whitespace in your input is the newline ('n') at the end of the line. Use
scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5);
Instead. % is a scanset that matches all characters in the brackets. If the characters are prepended with a ^ they are excluded.
Please, never use "%s" or a scanset without specifying a width for the conversion specifier to limit the characters maximal read. Since your arrays are 100 chars long specify 99 so there is space left for the terminating '' character.
You should also check, if scanf was successful before proceeding to process the input. scanf() returns the number of conversions successfully performed. So in your case you schould compare the return value with 5:
if (scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5) != 5) {
; // handle error
}
I'm truly greateful for excellent answer for my first question. thx
– Daniel Hong
Nov 27 '18 at 2:11
add a comment |
The format specifier "%s" reads until the next whitespace. The next whitespace in your input is the newline ('n') at the end of the line. Use
scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5);
Instead. % is a scanset that matches all characters in the brackets. If the characters are prepended with a ^ they are excluded.
Please, never use "%s" or a scanset without specifying a width for the conversion specifier to limit the characters maximal read. Since your arrays are 100 chars long specify 99 so there is space left for the terminating '' character.
You should also check, if scanf was successful before proceeding to process the input. scanf() returns the number of conversions successfully performed. So in your case you schould compare the return value with 5:
if (scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5) != 5) {
; // handle error
}
I'm truly greateful for excellent answer for my first question. thx
– Daniel Hong
Nov 27 '18 at 2:11
add a comment |
The format specifier "%s" reads until the next whitespace. The next whitespace in your input is the newline ('n') at the end of the line. Use
scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5);
Instead. % is a scanset that matches all characters in the brackets. If the characters are prepended with a ^ they are excluded.
Please, never use "%s" or a scanset without specifying a width for the conversion specifier to limit the characters maximal read. Since your arrays are 100 chars long specify 99 so there is space left for the terminating '' character.
You should also check, if scanf was successful before proceeding to process the input. scanf() returns the number of conversions successfully performed. So in your case you schould compare the return value with 5:
if (scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5) != 5) {
; // handle error
}
The format specifier "%s" reads until the next whitespace. The next whitespace in your input is the newline ('n') at the end of the line. Use
scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5);
Instead. % is a scanset that matches all characters in the brackets. If the characters are prepended with a ^ they are excluded.
Please, never use "%s" or a scanset without specifying a width for the conversion specifier to limit the characters maximal read. Since your arrays are 100 chars long specify 99 so there is space left for the terminating '' character.
You should also check, if scanf was successful before proceeding to process the input. scanf() returns the number of conversions successfully performed. So in your case you schould compare the return value with 5:
if (scanf("%99[^|]|%99[^|]|%99[^|]|%99[^|]|%99s", tmp1, tmp2, tmp3, tmp4, tmp5) != 5) {
; // handle error
}
edited Nov 27 '18 at 1:57
jxh
55.6k672146
55.6k672146
answered Nov 27 '18 at 1:48
SwordfishSwordfish
9,65811436
9,65811436
I'm truly greateful for excellent answer for my first question. thx
– Daniel Hong
Nov 27 '18 at 2:11
add a comment |
I'm truly greateful for excellent answer for my first question. thx
– Daniel Hong
Nov 27 '18 at 2:11
I'm truly greateful for excellent answer for my first question. thx
– Daniel Hong
Nov 27 '18 at 2:11
I'm truly greateful for excellent answer for my first question. thx
– Daniel Hong
Nov 27 '18 at 2:11
add a comment |
If you mean client.txt , I created it in advance in home directory.
– Daniel Hong
Nov 27 '18 at 1:45
you could find out what the byte values for those pesky characters are using a hex viewer. Also what happens when you make this change:
fopen("client.txt","w+");(Note the plus sign)– HappyKeyboard
Nov 27 '18 at 1:48
I solved the problem from the question below , but I`m curious about what is p ���� too. I got 20 70 03 20 5C 03 20 A0 CB FF FF form hex viewer,
– Daniel Hong
Nov 27 '18 at 2:01
@DanielHong Thats just random data from
tmp2-tmp5that got written without being initialized before. If you had initialized your arrays (char tmp1[100] = { 0 }, tmp2[100] = { 0 }, tmp3[100] = { 0 }, tmp4[100] = { 0 }, tmp5[100] = { 0 };) you wouldn't have seen that garbage.– Swordfish
Nov 27 '18 at 2:15
I was curious to see if there would be any new line feed or carriage return or line feed characters (you can look up ASCII Table to see what the values area)
– HappyKeyboard
Nov 27 '18 at 2:20