I have been stalking the forums for more than a month now and it has answers to most of my questions. But I have been struggling through one difficulty which I couldn't really find the answer around here(there is other similar solutions, but it didn't not solved mine)
I have tried to simplify my huge code block to the one shown below,
#define DATA_ROW 1 /*I have one data per line*/
int freqstring;
char objectstring;
char string[10];
FILE * file;
freqstring = 0;
while (fscanf_s(file, "%s", &objectstring, 8) == DATA_ROW)
{
string[freqstring] = objectstring;
freqstring = freqstring + 1;
}
for (freqstring = 0; freqstring < 10; freqstring = freqstring + 1)
{
printf("%s", string[freqstring]);
}
Started learning C programming for over 2 months now, and this is the first time I'm using arrays. And I've encountered the following error message in the output box
warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 1 has type 'int'
You have some misunderstanding of the string concept.
A string is a zero-terminated array of chars. The individual char in that string is still integer type.
So string[freqstring] has integer type. And that is exactly what the error message says. The printf expects a char* type (aka string) due to use of %s but you give it an integer type.
To get the string, just use string, i.e.
printf("%s\n", string);
Further, it seems you have problems with the scanning:
fscanf_s(file, "%s", &objectstring, 8)
You ask for a string (%s) but objectstring is just a single char. You claim the size is 8 which it isn't. Use sizeof instead of a constant.
Did you really intend to read chars, then use %c instead of %s.
In case you want to read chars, the code could be something like:
freqstring = 0;
string[freqstring] = '\0';
while ((fscanf_s(file, "%c", &objectstring, sizeof(char)) == 1) &&
(freqstring < 9))
{
string[freqstring] = objectstring;
freqstring = freqstring + 1;
string[freqstring] = '\0';
}
printf("%s\n", string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With