P-Scanf is a lightweight C library that provides safer and more convenient input handling compared to the standard scanf.
- Validates input type: if a user enters a character when an integer is expected, an error is raised.
- Automatically clears the input buffer when using P-Scanf macros.
- Provides safe string handling without the need to predefine buffer sizes (strings are dynamically allocated).
- Automatically frees memory if a memory allocation failure occurs.
-
Copy the static library
libpscanf.libto:C:\Program Files\Microsoft Visual Studio 12.0\VC\libTo avoid the linker warning:
warning LNK4099: PDB 'libpscanf.pdb' was not found with 'libpscanf.lib' or at 'libpscanf.pdb'; linking object as if no debug infoPlace the
libpscanf.pdbfile in the same folder aslibpscanf.lib. -
Add the library in Visual Studio:
Go toProject -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependenciesand add:libpscanf.lib -
Copy the header file
pscanf.hto:C:\Program Files\Microsoft Visual Studio 12.0\VC\include -
Include it in your source code:
#include <pscanf.h>
-
Copy the static library
libpscanf.ato:C:\Program Files\Dev-Cpp\lib -
In Dev-C++, go to
Project -> Project Options -> Parameters -> Linkerand add:-lpscanf -
Copy the header file
pscanf.hto:C:\Program Files\Dev-Cpp\include
Download the library and header file here: PScanf Release v2.0
-
dataread(_format, _var, ...)
Reads any type of data from standard input. -
strread(_var, ...)
Reads a string from standard input. Memory is dynamically allocated.
Returns1if memory allocation fails.
-
pause()
Pauses the program and prints:Press enter to continue... -
sfree()
Frees all memory allocated by thestrreadmacro.
#include <stdio.h>
#include <pscanf.h>
int main(void) {
int a;
dataread("%d", &a, "Enter an int: ");
printf("%d\n", a);
return 0;
}If the user enters an invalid value (e.g., a character instead of an integer), the error message will be:
Error: Enter an integer value:
#include <stdio.h>
#include <pscanf.h>
int main(void) {
string name = { NULL };
strread(&name, "Enter a string: ");
printf("String: %s - Length: %d\n", name, name.length);
sfree();
pause();
return 0;
}name.lengthgives the string length.
#include <stdio.h>
#include <pscanf.h>
int main(void) {
int i;
string name = { NULL };
strread(&name, "Enter a string: ");
for (i = 0; i != name.length; ++i)
printf("%c\n", name.s[i]);
sfree();
pause();
return 0;
}name.sgives access to the characters.
#include <stdio.h>
#include <stdint.h>
#include <pscanf.h>
#define MAX_STRINGS (5)
/* Returns 0 if memory was successfully allocated, otherwise 1. */
uint8_t DataEntry(string* name) {
int i;
for (i = 0; i != MAX_STRINGS; ++i) {
/* Braces are required since strread expands into 2 lines of code. */
strread(&name[i], "Enter string %d: ", i + 1);
}
return 0;
}
void PrintData(string* name) {
int i, j;
for (i = 0; i != MAX_STRINGS; ++i)
printf("%s\n", name[i]);
printf("\n");
for (i = 0; i != MAX_STRINGS; ++i) {
for (j = 0; j != name[i].length; ++j)
printf("%c", name[i].s[j]);
printf("\n");
}
}
int main(void) {
string name[MAX_STRINGS] = { NULL };
if (DataEntry(name)) return 1;
PrintData(name);
sfree();
pause();
return 0;
}Note: Always initialize string variables to NULL.
- DevD4v3
Author and maintainer of P-Scanf. - Microsoft Corporation
For providing thecl.execompiler used to test the library.