phoenix-rtos / phoenix-rtos-project

Sample project using Phoenix-RTOS
https://phoenix-rtos.com
BSD 3-Clause "New" or "Revised" License
43 stars 32 forks source link

Problem with `fscanf` and `vfscanf` using brackets and `\n` #679

Open maska989 opened 1 year ago

maska989 commented 1 year ago

int main(void) { FILE *fd; char buff[12] = "abc\nabc"; char exp[12] = { 0 }; char c1, c2, c3, c4, c5, c6, c7; int sz;

fd = fopen("test", "w+");
fprintf(fd, "%s", buff);
rewind(fd);

fseek(fd, 0L, SEEK_END);
sz = ftell(fd);
rewind(fd);

printf("\nFile contetn length = %d\nScanf = %d =>", sz, fscanf(fd, "%[^x]", exp));
printf("%s<=\n\n", exp);
rewind(fd);
printf("\nFile contetn length = %d\nScanf = %d =>", sz, fscanf(fd, "%c%c%c%c%c%c%c", &c1, &c2, &c3, &c4, &c5, &c6, &c7));
printf("%c%c%c%c%c%c%c<=\n\n", c1, c2, c3, c4, c5, c6, c7);

}


 * Code output on ia32-generic-qemu and host-geneic-pc
![Screenshot from 2023-03-31 10-30-33](https://user-images.githubusercontent.com/49757239/229068654-267fc076-fc60-4784-9b12-efd04798da9e.png)
maska989 commented 1 year ago

https://github.com/phoenix-rtos/phoenix-rtos-tests/blob/c13ef424dff7c1f0f88f02e4a67104f6d9ed5d22/libc/stdio_scanf.c#L3972 https://github.com/phoenix-rtos/phoenix-rtos-tests/blob/c13ef424dff7c1f0f88f02e4a67104f6d9ed5d22/libc/stdio_scanf.c#L4060

jmaksymowicz commented 1 month ago

This happens because the parser for scanf, fscanf, vscanf and vfscanf loads only one line of the file using getline: https://github.com/phoenix-rtos/libphoenix/blob/ffca1e635e8146d77b5694077f5283cb88f0154b/stdio/scanf.c#L665