static void retrieveData(const char* filename)
{
FILE *f;
char s[256];
int a[10];
int b[10];
char c[10][10];
...
.....
long j[10];
sprintf(s, "some code %s", filename);
if ((f = popen(s, "r")) != NULL) {
while (fgets(s, sizeof(s), f)) {
if (strncmp(s, "A........ {
sscanf (s,"........
a[0] = var1;
b[0] = var2;
c[0] = var3;
...
.....
}
else if (strncmp(s, "B........ {
sscanf (s,".........
a[1] = var4;
b[1] = var5;
c[1] = var6;
...
.....
}
else if .........more codes
}
pclose(f);
}
}
I would like to get all data in arrays a,b,c….,j.
void getData(int argc, char **argv)
{
int n;
int a[10];
int b[10];
char c[10][10];
...
.....
long j[10];
retrieveData("filename1");
for (n = 0; n < 10; ++n) {
printf("%d\n", a[n]);
}
for (n = 0; n < 10; ++n) {
printf("%d\n", b[n]);
}
...... more codes
retrieveData("filename2");
for (n = 0; n < 10; ++n) {
printf("%d\n", a[n]);
}
for (n = 0; n < 10; ++n) {
printf("%d\n", b[n]);
}
...... more codes
}
filename1 and filename2 contain same data structure but difference values.
Please someone show me how…
I’m compiling under linux c.
sorry for my poor english.
You probably need to define them as global variables, and not local, then you can access them in any function.
Put this at the top of the
.cppfile that contains both functions.And please use better names 🙂
Alternatively, since in your case the one function calls the other, you can pass the arrays in as pointers as arguments to the function, but that will be painful in the case of the 2D pointers atleast.
A third, syntactically simpler, option is to wrap the 3 arrays within a
structand pass a pointer to thatstructaround, thanks Basile Starynkevitch.