Originally Posted by
fallenshadow
So I got the file writing going which was easy. The reading is a bit more involved.
Using fscanf I got the return value which is 1. Therefore its finding my value. The question is, how does one get at the actual value?
The return value from fscanf() will indicate how many patterns in your format criteria were satisfied. If you are getting a value of 1, then this implies that one format specifier was matched.
Can you elaborate on your question "... how does one get at the actual value?". My crystal ball has been furloughed and hence is not functioning.
P.S. Based on your original posts in this thread, I conjured the following simple program to demonstrate the writing of data using fprintf() and the reading of such using fscanf(). Perhaps this may help you.
Code:
#include <stdio.h>
#include <string.h>
#include <assert.h>
struct MyData
{
int value1, value2;
char str[80];
};
void writeData(struct MyData* data, FILE* fp);
void readData(struct MyData* data, FILE* fp);
int main()
{
struct MyData w_data;
struct MyData r_data;
w_data.value1 = 10;
w_data.value2 = 20;
strncpy(w_data.str, "This is a good day", sizeof(w_data.str));
FILE* fp = fopen("MyData.txt", "w+");
if (fp)
{
writeData(&w_data, fp);
fclose(fp);
}
fp = fopen("MyData.txt", "r");
if (fp)
{
readData(&r_data, fp);
fclose(fp);
}
assert(w_data.value1 == r_data.value1);
assert(w_data.value2 == r_data.value2);
assert(strcmp(w_data.str, r_data.str) == 0);
return 0;
}
void writeData(struct MyData* data, FILE* fp)
{
assert(data != NULL);
assert(fp != NULL);
fprintf(fp, "%d\n", data->value1);
fprintf(fp, "%d\n", data->value2);
fprintf(fp, "%s\n", data->str);
}
void readData(struct MyData* data, FILE* fp)
{
assert(data != NULL);
assert(fp != NULL);
fscanf(fp, "%d [^\n]", &data->value1);
fscanf(fp, "%d [^\n]", &data->value2);
fscanf(fp, "%79[^\t\n]", data->str);
}
Bookmarks