Thread: Help with a C program

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    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!!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    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

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    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;
    }
    Last edited by zrepaer; 04-08-2009 at 03:31 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    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;
    }

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    thank you for your help!!!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by charlitos View Post
    this example that works
    No it doesn't.
    Quote Originally Posted by charlitos View Post
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread