added the case ' %[^\n]' and fscanf() tests using 'input.txt'
This commit is contained in:
parent
60d950e37c
commit
16b4404af9
|
@ -0,0 +1,8 @@
|
|||
123
|
||||
3.14
|
||||
Hello
|
||||
A
|
||||
10 3.14 20
|
||||
This is a sentence with spaces.
|
||||
1 2 3 4 5
|
||||
Invalid Input Test
|
116
src/main.c
116
src/main.c
|
@ -45,6 +45,99 @@ long double ld1 = 0;
|
|||
long double ld2 = 0;
|
||||
|
||||
|
||||
int fscan_test( void )
|
||||
{
|
||||
int line=1;
|
||||
FILE *fp = fopen("input.txt", "r");
|
||||
|
||||
if (fp == NULL) {
|
||||
dprint( 1, line, C_BLACK, "Error: Failed to open file\n");
|
||||
line+=10;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Test 1: Read an integer
|
||||
int i;
|
||||
fscanf(fp, "%d", &i);
|
||||
dprint( 1, line, C_BLACK, "Test 1: Read integer: %d\n", i);
|
||||
line+=10;
|
||||
|
||||
// Test 2: Read a floating-point number
|
||||
float f;
|
||||
fscanf(fp, "%f", &f);
|
||||
dprint( 1, line, C_BLACK, "Test 2: Read floating-point number: %f\n", f);
|
||||
line+=10;
|
||||
|
||||
// Test 3: Read a string with spaces
|
||||
char s[20];
|
||||
fscanf(fp, "%s", s);
|
||||
dprint( 1, line, C_BLACK, "Test 3: Read string: %s\n", s);
|
||||
line+=10;
|
||||
|
||||
// Test 4: Read a character
|
||||
char cc;
|
||||
fscanf(fp, "%*c%c", &cc);
|
||||
dprint( 1, line, C_BLACK, "Test 4: Read character: %c\n", cc);
|
||||
line+=10;
|
||||
|
||||
// Test 5: Read multiple values
|
||||
int a, b, d;
|
||||
float c1;
|
||||
fscanf(fp, "%d%f%d", &a, &c1, &b);
|
||||
dprint( 1, line, C_BLACK, "Test 5: Read multiple values: %d, %f, %d\n", a, c1, b);
|
||||
line+=10;
|
||||
|
||||
// Test 6: Read a string with spaces using %[^\n]
|
||||
char s1[50];
|
||||
fscanf(fp, " %[^\n]", s1);
|
||||
dprint( 1, line, C_BLACK, "Test 6: Read string with spaces: %s\n", s1);
|
||||
line+=10;
|
||||
|
||||
// Test 7: Read until EOF
|
||||
int num, count = 0;
|
||||
while (fscanf(fp, "%d", &num) == 1) {
|
||||
dprint( 1, line, C_BLACK, "Test 7: Read number: %d\n", num);
|
||||
line+=10;
|
||||
count++;
|
||||
}
|
||||
dprint( 1, line, C_BLACK, "Test 7: Total numbers read: %d\n", count);
|
||||
line+=10;
|
||||
|
||||
// Test 8: Error handling - invalid input
|
||||
int x;
|
||||
int ret = fscanf(fp, "%d", &x);
|
||||
if (ret == EOF) {
|
||||
dprint( 1, line, C_BLACK, "Test 8: End of file reached\n");
|
||||
line+=10;
|
||||
}
|
||||
else if (ret == 0) {
|
||||
dprint( 1, line, C_BLACK, "Test 8: Invalid input\n");
|
||||
line+=10;
|
||||
}
|
||||
else {
|
||||
dprint( 1, line, C_BLACK, "Test 8: Read number: %d\n", x);
|
||||
line+=10;
|
||||
}
|
||||
|
||||
|
||||
// Test 9: Error handling - end of file
|
||||
int y;
|
||||
ret = fscanf(fp, "%d", &y);
|
||||
if (ret == EOF) {
|
||||
dprint( 1, line, C_BLACK, "Test 9: End of file reached\n");
|
||||
line+=10;
|
||||
}
|
||||
else {
|
||||
dprint( 1, line, C_BLACK, "Test 9: Read number: %d\n", y);
|
||||
line+=10;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void clear( void )
|
||||
{
|
||||
|
||||
|
@ -81,6 +174,7 @@ main(void)
|
|||
{
|
||||
__printf_enable_fp();
|
||||
|
||||
|
||||
dclear(C_WHITE);
|
||||
|
||||
// results as per glic/sscanf are given in comments after the corresponding dprint()
|
||||
|
@ -434,7 +528,7 @@ main(void)
|
|||
|
||||
int val = 123;
|
||||
char buf[100];
|
||||
sprintf(buf, "%p", (void*)&val);
|
||||
sprintf( buf, "%p", (void*)&val);
|
||||
void *ptr;
|
||||
sscanf(buf, "%p", &ptr);
|
||||
int *iPtr = ptr;
|
||||
|
@ -667,8 +761,28 @@ main(void)
|
|||
dupdate();
|
||||
// #75 : rd 1[^] : [You can to use any combi.] cn=36 rt=1
|
||||
|
||||
clear();
|
||||
char str4[50] = "................................................";
|
||||
ret = sscanf( " This is a line with spaces.\n", " %[^\n]%n", str4, &n );
|
||||
assert( strcmp(str4, "This is a line with spaces.")==0 && n==33 && ret==1 );
|
||||
dprint( 1, 160, C_BLACK, "#76 : rd 1 [^] : [%s] cn=%d rt=%d", str4, n, ret );
|
||||
dupdate();
|
||||
// #76 : rd 1[^] : [This is a line with spaces.] cn=33 rt=1
|
||||
|
||||
getkey();
|
||||
|
||||
|
||||
dclear( C_WHITE );
|
||||
|
||||
gint_world_switch( GINT_CALL( fscan_test ));
|
||||
|
||||
dupdate();
|
||||
|
||||
getkey();
|
||||
|
||||
|
||||
|
||||
return 1;
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue