So I don't understand why this is happening but the first method of this code fails and the buffer is not initialized I guess within the file read scope so sscanf has bad data to parse and fails. Once sscanf is outside of the file read scope the buffer is filled and sscanf works fine. I also used to file input data to feed a parse function that used sscanf and it also works, can anyone point out what I am missing?
#inlcude <stdio.h>
#include <window.h>
void readFromFile()
{
char buff[100];
int op = 0;
int lc = 0;
char st[25];
FILE* cmdBUffer;
std::string CMDFILE = "C:\\testFile.txt";
// example test file string 50 0005 SomeString
//----- This works and op and lc are in fact integers
cmdBuffer = fopen(CMDFILE.data(), "r");
if (cmdBuffer != NULL)
{
fgets(buff, sizeof(buff), cmdBuffer);
fclose(cmdBuffer);
}
sscanf(buff, "%d %d %s", &op, &lc, st);
printf(" %d %d %s\n", op, lc, st);
}
//----- this always fails ----
cmdBuffer = fopen(CMDFILE.data(), "r");
if (cmdBuffer != NULL)
{
fgets(buff, sizeof(buff), cmdBuffer);
sscanf(buff, "%d %d %s", &op, &lc, st);
fclose(cmdBuffer);
}
printf("%d %d %s, &op\n", &lc, st);// op and lc are always zero after sscanf fail
Aucun commentaire:
Enregistrer un commentaire