1

In my C code I am given the following string as input:

"c:\tc\bin\a c j k.jpg"

I tried to read the input with scanf but it failed (passing the input both with and without quotation marks) and I am looking for an other solution.

My code is as follows:

char keytext[16],zzz,source[100],dest[100];
short int a;
size_t readcount;

clrscr();
printf("input the key text(max 16 characters):");
scanf("%s",&keytext);

printf("input file name:");
scanf("%s",&source);

/*fgets(source,100,stdin);
namelen=strlen(source);
if(source[namelen-1]=='\n')
source[namelen-1]='\0';*/

printf("output file name:");
scanf("%s",&dest);
3
  • Is C:\tc\bin\a c j k.jpg to be treated as a single string? Commented Sep 2, 2012 at 7:53
  • Can you give us more detailed explanation? Commented Sep 2, 2012 at 7:54
  • @hmjd yes as am going to read that file's content Commented Sep 2, 2012 at 7:57

2 Answers 2

1

The format specifier %s will stop consuming input when the first whitespace character is encountered, so it will only read until the end of C:\tc\bin\a. To read up to the k.jpg you can use a scanset:

char input[128];
if (1 == scanf("%127[^\n]", input))
{
}

The format specifier "%127[^\n]" means read up to the next newline character but no more than 127 characters to prevent buffer overrun. You should check the return value of scanf() before using its output, it returns the number of assignments made.

Sign up to request clarification or add additional context in comments.

4 Comments

what about the characters \t and \b and \a in that string
The are not part of a string literal, they are input so will be copied into the input array.
come on you have used sscanf on ideone and gave scanf in here, how could i come to know about that
@user1026137, I had to use sscanf() on ideone as there is no way to provide input (AFAIK). However, sscanf() uses the same format specifiers as scanf(), the only difference being the source of input. See reference page for sscanf() and scanf().
0

scanf function with %s specificator expects pointer to char char *. But you pass the pointer to pointer to char char **. In order to fix it use the following code instead:

scanf("%s", keytext);
scanf("%s", source);
scanf("%s", dest);

Note, it doesn't require & symbol. If you want to read whole string with spaces, you can use fgets function.

fgets(keytext, sizeof(keytext), stdin);
fgets(source,  sizeof(source),  stdin);
fgets(dest,    sizeof(dest),    stdin);

4 Comments

But that will not solve OP question of scanning with white spaces.
@fasked if i use fgets after doing the first input using scanf, it won't work at all.
@user1026137 I got you! After using scanf stream keeps \n symbol. You need to clear the stream. Try to scanf("%*[^\n]") before fgets.
@user1026137 it seems we just don't understand your problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.