Two topics take you sscanf and sprintf

tags: C  C language

1. sprintf function

statement:

int sprintf ( char * str, const char * format, ... );

illustrate:

Writing data formatted string

If the format in printf, will print the same text, but the content sprintf not be printed, but in the form of a string C stored in the pointing character array str (buffer) in.

The size of the buffer should be large enough to contain the entire result string.

Will automatically append after the contents of aTerminating null character

After the format parameter, a function of the desired parameter of at least as many additional parameters required for the format.

format is that you usually write% of all types of format used in printf, sprintf also follow the same norms.
return value:
If successful, the total number of characters written is returned. This count does not automatically include other appended null character end of the string.

If it fails, it returns a negative number.

A small example

#include <stdio.h>

int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n",buffer,n);
  return 0;
}

Wherein the buffer is a character array, he will accept the format string, and a, b, a + b% will be embedded in the corresponding format.
sprintf receiving the return value of n, the total number of characters recorded in the buffer
Final output:

A question about the sprintf

You need to write a function to determine the value of an integer variable is not a palindrome.
247 is not a palindrome number 14741 is a palindrome
analyze:
Determine whether a number is a palindrome is not easy, because to get the number of digits, but also to record the numbers of each bit. However, as long as the palindrome determines whether or not character string using the double pointer lot easier.

int palindrome(char* str)// Function: Decision string is repayment
{
	char* left = str;
	char* right = strchr(str, '\0') - 1;
	while (left < right)
	{
		if (*left != *right)
		{
			return 0;
		}
		left++;
		right--;
	}
	return 1;
}

// Convert the digital to a string function
int numeric_palindrome(int value)
{
	char buffer[50];
	sprintf(buffer, "%d", value);// Replace the digital to a string
	return palindrome(buffer);
}

2.sscanf function

statement:

int sscanf ( const char * str, const char * format, ...);

illustrate:

Reading data from the formatted string str

Str read from the data format based on the parameters and stores them in a position to give an additional parameter, as used scanf same, but instead of reading the data from the standard input s (stdin).

return value

If successful, the function returns the number of items in the list parameters have been successfully filled in. In case of match failure, which can match the expected count the number of items or less (even zero).

If the input failure occurs before any successful interpretation of data, it returns EOF.

A small example

#include <stdio.h>

int main ()
{
  char sentence []="Rudolph is 12 years old";
  char str [20];
  int i;

  sscanf (sentence,"%s %*s %d",str,&i);
  printf ("%s -> %d\n",str,i);
  
  return 0;
}

SUMMARY get% s str simultaneously added at the end of '\ 0',
Used in scanf, * added portion will be ignored and will not be acquiring parameters, so% * s content is ignored, then 12 will be in the form of% d and an integer to the variable i.
The final output

A topic on the sscanf

topic:
A data file containing the age of family members. The age of the individual members of a family in the same row, separated by a space. For example, the following data
45 42 22
36 35 7 3
22 20
Describes the age of all the members of the three families, they were there three, five and two members.
// write a program that calculates the average age of all members of each family represented in this file.
// print format code program should% 5.2f an average age, followed by a colon and the input data. You can assume that the number of members of each family no more than 10.
analyze:
Obviously input is alphanumeric, numeric computation is required to perform the conversion,
Each time you need to read his string
The spaces separated string, and then converted to digital and stored in the array [10] where
Answers are as follows:

int main()
{
	char buffer[BUFSIZ];/ / Read a line of data each time

	while (fgets(buffer, BUFSIZ, stdin) != NULL)
	{
		int age[10];
		int members;// How many family members have been recorded
		int i;
		int sum = 0;
		members=sscanf(buffer, "%d %d %d %d %d %d %d %d %d %d",
			age, age + 1, age + 2, age + 3, age + 4, age + 5, age + 6, age + 7, age + 8, age + 9);/ / Successfully read the number of numbers in count
		if (members == EOF)// SSCANF is not read to any data Return EOF
			continue;
		for (i = 0; i < members; i++)
		{
			sum += age[i];
		}
		printf("%5.2f:%s", (double)sum / members, buffer);
		
	}
}

Summarize

  • Converting a string types --sprintf
  • The string into various types --sscanf

Intelligent Recommendation

: main sscanf sprintf

With a piece of code, as the first blog, look at the code Code function 1: Simple communication protocol between the upper computer and the lower computer that cannot be rude; 2: "OWIRA=f5N=3&quo...

Sscanf function - (reverse of sprintf)

2019 Unicorn Enterprise Heavy Gold Recruitment Python Engineer Standard >>> First example: head File #include<stdio.h> Define the function int sscanf (const char *str, const char * form...

sscanf and sprintf explain usage

\quad sscanf mainly used to re-enter the character string of the specified type variable, the variable of the specified type sprintf sucked into a string. Usage examples below, mainly for problems oj ...

About sscanf and sprintf

sscanf and sprintf It can be understood as string + scanf and string + printf scanf and printf can be written scanf (screen, "% d", & n); and printf (screen, "% d", n) In fact,...

sprintf,sscanf,fread,fwrite

sprintf function Msdn first look at the definition of this function; Write formatted data to a string. (Write data to a format string) int sprintf( char *buffer, const char *format [, argument] &helli...

More Recommendation

sscanf,sprintf,fread,fwrite

Sscanf read data string formatted scanf sscanf and similar, are used to input, to the latter promoter is a keyboard (stdin) as the input source, the former with a fixed string as the input source. spr...

C language sscanf () and sprintf ()

sscanf() sprintf() Note: Operating Environment sprintf sscanf...

Use sscanf and sprintf function

sscanf () - read data into the specified format matches a string from sprintf () - string formatting commands, the main function is writing data formatted in a string Averaging 1054 (20 minutes) The b...

Understanding of the sscanf and sprintf

sprintf Output 2048, 3.140000, hello sprintf is to n, db, str (the right parameter) to a certain format ( "% d% lf% s") str2 incoming (left parameter) parameter can have a plurality of the r...

sprintf, sscanf function Detailed

A, sprintf function prototype: int sprintf(char *str, const char *format, ...) Action is the format string, the specific features as follows: (1) converting the digital variable string. (2) the o...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top