简体   繁体   中英

(C/C++) fscanf_s Missing Integer Arguments error When Reading in Chars to Array from txt file

First Post, will do my best to follow guidelines and make this a proper setup. If ANYTHING is needed, let me know!

The error in VS 2019 is "C6064: Missing integer argument to 'fscanf' that corresponds to conversion specificer '2'." The code below is given in the part where my code breaks. The rest up to this point is fine and compiles correctly. The line in error is the fscanf.

ENTIRE CODE: https://pastebin.com/DCgEa64g (Excuse any missing variables, they have been dealt with!)

fp = fopen("codefile.txt", "r");
if (fp == NULL)
{
    printf("could not open codefile.txt\n");
    return 1;
}

i = 0;
while (!feof(fp))
{

    fscanf(fp, "%c", &code[i]);
    i++;
}

This portion I posted is me scanning a file, codefile.txt, and saving each character inside to a string array, then comparing that array to a "ciphered message" to get an answer. The code compiles the correct answer, but I can't get this error to go away. The other message present is "'fscanf': not enough arguments passed for the format string" but I assumed only %c was needed for a character in a string array?

This also puts each element in codefile.txt into the code array individually, which IS intended. I finished the rest of this assignment, so I'm not asking for somebody to do anything else at all for me. I will gladly post any other code needed, The goal is to read in a separate message file with numbers, put them into an array. descramble and compare to the codefile.txt string and get the result, I did 95% of the work, and need some help: Any clarification, just ask! Turning this in tomorrow evening :)

Your actual code (at the link, not in your question) uses fscanf_s() , whose documentation says:

The main difference between the more secure functions (that have the _s suffix) and the other versions is that the more secure functions require the size in characters of each c, C, s, S, and [ type field to be passed as an argument immediately following the variable. For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Width Specification.

Your error is in failing to provide the additional size argument.

Thank You John Zwinck; I eventually got it working through adding SIZE to the argument when using fscanf_s specifically. Answer looks like this:

    while (!feof(fp))
{
    fscanf_s(fp, "%c", &code[i], SIZE); //SIZE was added here!
    i++;
}

Thanks for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM