-
Help with a C program
Code:
I am having a bit of trouble doing a C Program using fscanf
any help would be great
i'm using a structure
typedef struct
{ char color[20];
char size [1];
int qty [5];
double price[20];
}PAINTDATA;
and i am supposed to fscanf in data like
red q 16 25.99
from a data file but i am currently stumped on how to make a workable loop and function for the program
would i need a function such as
void getData (PAINTDATA );
and how would i set up a good loop to input the date
fscanf(infile, "%s %s %d %lf", ??? as to what to put here)
I'm assuming a need a loop but am unsure how to do it
again any help would be great
thanks!!!
-
Why is size an array of 1 character? Why use an array there? Do you know how to use scanf? If so, fscanf works the same way, except it supplies a file pointer from which it reads.
Get your program working where it reads the input from you, then change the function name to use fscanf, and supply the file pointer.
Quzah.
-
Code:
i guess i should set the size of the array to 4
such as
typedef struct
{ char color[20];
char size [1];
int qty [5];
double price[20];
}PAINTDATA[4];
and i understand how to use scanf but the biggest problem i'm having is how to set up the loop so it read till the end of file
i tried
for (i=0; i<4; i++)
{
fscanf (infile, "%s %s %d %lf", PAINTDATA[i].color, PAINTDATA[i].size, PAINTDATA[i].qty, PAINTDATA[i].price);
}
but when i do so it gives me an error saying paintdata not found....
am i setting up the loop wrong or would it be a problem in the way i'm passing the data back and forth in the function???
again thank you for the help
-
Code:
here is the code i have so far i guess i should have posted it originally
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ELMNTS 100
#define ANLYS_RNG 20
//structure declaration
typedef struct
{
char color[MAX_ELMNTS];
char size[MAX_ELMNTS];
int qty[ANLYS_RNG];
double price[ANLYS_RNG];
} PAINTDATA[4];
//function declarations
void getdata (PAINTDATA);
void printdata (PAINTDATA);
//Functions in Other File
int main()
{
getdata (PAINTDATA);
printdata (PAINTDATA);
return 0;
}
void getdata (PAINTDATA)
{
FILE* infile;
int i;
infile = fopen ("paint.data", "r");
if (!infile)
{
printf ("File does not exist");
exit(1);
}
if(i=0; i<4; i++)
{
fscanf (infile, "%s, %s, %d, %lf ", PAINTDATA->color[i], PAINTDATA->size[i], &PAINTDATA->qty[i], &PAINTDATA->price[i]);
}
return;
}
void printdata (PAINTDATA)
{
int i;
if(i=0; i<4; i++;)
{
printf( "%s, %s, %d, %lf ", PAINTDATA->color[i], PAINTDATA->size[i], &PAINTDATA->qty[1], &PAINTDATA->price[i]);
}
return;
}
-
No, you missed the point, a couple of them actually.
1 - You have a variable named size. HINT: It's in your structure.
2 - It's an array, with a size of 1. If you use an array, typically it should be bigger than 1 element.
Forget the loop for now. Create a scanf line that works correctly to fill one structure. Then make an array of structures, and just try to fill one of those via you inputting the information into a scanf call. Skip the file bit of it until you know you can do that first task correctly. To make sure you are doing that bit correctly, try printf statements to show that what you're reading is ending up in your structure as you expect it to.
Quzah.
-
You have a few errors there but just look at this example that works and you should be able to get yours right:
Code:
typedef struct
{
char color[20];
char size [1];
int qty [5];
double price[20];
}PAINTDATA;
int main()
{
size_t i;
FILE * fp;
PAINTDATA pdata[4];
if( (fp = fopen("myfile", "r")) == NULL )
{
fprintf(stderr, "error %d\n", errno);
exit(1);
}
for (i=0; i<4; i++)
{
fscanf(fp, "%s %s %d %lf", pdata[i].color, pdata[i].size, pdata[i].qty, pdata[i].price);
}
printf("and %s\n", pdata[0].color); // just to prove that the data was actually filled in
return 0;
}
-
thank you for your help!!!
-
Quote:
Originally Posted by
charlitos
this example that works
No it doesn't.
Quote:
Originally Posted by
charlitos
Code:
typedef struct
{
char color[20];
char size [1];
...
fscanf(fp, "%s %s %d %lf", pdata[i].color, pdata[i].size,
}
Why does no one ever listen to me?
Quzah.