Warnings, warnings, warnings?

This is a discussion on Warnings, warnings, warnings? within the C Programming forums, part of the General Programming Boards category; Before you read from the file, you should create it first (I know that's obvious, but I don't see any ...

  1. #16
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Before you read from the file, you should create it first (I know that's obvious, but I don't see any file writing in your code).

    Now, I don't know what the purpose of the exercise is, are you learning link lists, function calls, struct use, I/O or what? There's a lot of different aspects in a program like this that you need to get to grip with, but best do it one thing a time.

    I could answer your questions, but without knowing what you trying to achieve with this and how much you are already confident with, I don't want to say too much and send you up a path that will end up confusing you more than necessary.

    Let me know, then I'll help more.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  2. #17
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Hammer
    Before you read from the file, you should create it first (I know that's obvious, but I don't see any file writing in your code).

    Now, I don't know what the purpose of the exercise is, are you learning link lists, function calls, struct use, I/O or what? There's a lot of different aspects in a program like this that you need to get to grip with, but best do it one thing a time.

    I could answer your questions, but without knowing what you trying to achieve with this and how much you are already confident with, I don't want to say too much and send you up a path that will end up confusing you more than necessary.

    Let me know, then I'll help more.
    I know how to use linked lists, func calls & structures but only the basics were taught to me. I'm in my 1st year of college and the profs aren't worth a ......... Tutoring is too expensive and all I really have is a book for reference. For this app I have been informed that linked lists are not allowed and only days before it is due. I had originally built it using a list because I have never built anything yet that uses comands like fprintf etc. Now I'm in a jam with only three days to complete the code and I'm feeling more lost everytime I work on it. I hope this explains things for you and I hope you'll be kind enough to help me out. Thx.
    Code:
    /*
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //************************************************
    //structure for writing to and reading from file**
    
    struct fclient{
    int client_code;
    char clientname;
    char street_address;
    char city;
    char pros;
    char country;
    char postalorzip_code;
    int deleted; //used as a boolean (0=false, 1=true)
    };
    
    //************************************************
    /* FUNCTION PROTOTYPES*********************/
    
    char menu(void);
    void add_record(void);
    void delete_record(void);
    void search(void);
    char print_label(void);
    void key_wait (void);
    
    /* END OF FUNCTION PROTOTYPES**************/
    
    
    int main(void)
    {
      FILE *dta_file;
      char choice;
      dta_file = fopen("client.dat", "r+b"); //opening in binary mode for read/write
        if (dta_file == NULL)
    	{	printf("\n\nFile does not exist: Creating file");
    		dta_file = fopen("client.dat", "w+b"); //creation in binary mode
    		key_wait();
    	}
      do
    	 {
    		choice = menu();
    		switch (choice)
    		{
    			case '1': add_record(); break;
    			case '2': delete_record(); break;
    			case '3': search(); break;
    			case '4': display(); break;
    			case '5': print_label(); break;
    		}
    
    	 } while (choice != '6');
      fclose(dta_file);
      return 0;
    }
    /************************************************************/
    char menu (void)
    /*
    Task: display a menu and return the choice of the user
    */
    /************************************************************/
    {
      char choice;
      printf("\t\t\tClient Inventory Management \n\n\n");
      printf("\t1. Add a Client\n\n");
      printf("\t2. Delete a Client\n\n");
      printf("\t3. Search for a Client\n\n");
      printf("\t4. Display all Clients\n\n");
      printf("\t5. Print shipping label\n\n");
      printf("\t6. Exit the program\n\n\n");
      printf("\tPlease enter your selection: ");
    
      do
      {
    	choice = getch();
      } while (choice < '1' || choice > '7');
      return (choice);
    }
    /************************************************************/
    void add_record(void)
    /*
    Task: Read from keyboard and add the record in the file
    */
    /************************************************************/
    {
      FILE *dta_file;
      int client_code;
      char clientname;
      char street_address;
      char city;
      char pros;
      char country;
      char postalorzip_code;
      int clientunique=1;//flag for client code validation
      int client_code2;
    
      if (dta_file!=NULL)
      {
    	dta_file = fopen("client.dat","r+b");
      }
      // Validate the client code************************
      while(!clientunique)
      {
      //reset the flag
    	clientunique=1;
        printf("Client code: ");
    	scanf("%d", &client_code2);
      //validate that the client name is unique
    	while(dta_file != NULL)
    	{
    	//	fread( ???....
    		if (client_code2 != client_code)
    		{
    			clientunique = 0;
    		}
    
    	dta_file=dta_file;
    
    	fprintf(dta_file,"%d",client_code);
    	printf("Client name: ");
        scanf("%s",&clientname);
    	fprintf(dta_file,"%s",clientname);
    	printf("\n");
        printf("Street address: ");
        scanf("%s",&street_address);
    	fprintf(dta_file,"%s",street_address);
    	printf("\n");
        printf("City: ");
        scanf("%s",&city);
    	fprintf(dta_file,"%s",city);
    	printf("\n");
        printf("Province or State: ");
        scanf("%s",&pros);
    	fprintf(dta_file,"%s",pros);
    	printf("\n");
        printf("Country: ");
        scanf("%s",&country);
        fprintf(dta_file,"%s",country);
    	printf("\n");
        printf("Postal or Zip code: ");
        scanf("%s",&postalorzip_code);
    	fprintf(dta_file,"%s",postalorzip_code);
    
    	if (!clientunique)
    	{
    		printf("This client name is taken. Please enter a different name...Press a key\n");
    		key_wait();
    	}
    	fclose(dta_file);
    	}
    }
    */

  3. #18
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Wow, what did you do to this:
    Code:
    tmp.c:59: parse error before `/'
    tmp.c:69: parse error before string constant
    tmp.c:69: warning: data definition has no type or storage class
    tmp.c:70: parse error before string constant
    tmp.c:70: warning: data definition has no type or storage class
    tmp.c:71: parse error before string constant
    tmp.c:71: warning: data definition has no type or storage class
    tmp.c:72: parse error before string constant
    tmp.c:72: warning: data definition has no type or storage class
    tmp.c:73: parse error before string constant
    tmp.c:73: warning: data definition has no type or storage class
    tmp.c:74: parse error before string constant
    tmp.c:74: warning: data definition has no type or storage class
    tmp.c:75: parse error before string constant
    tmp.c:75: warning: data definition has no type or storage class
    tmp.c:76: parse error before string constant
    tmp.c:76: warning: data definition has no type or storage class
    tmp.c:104: parse error before `if'
    tmp.c:113: parse error before string constant
    tmp.c:113: warning: data definition has no type or storage class
    tmp.c:114: parse error before string constant
    tmp.c:114: warning: data definition has no type or storage class
    tmp.c:124: initializer element is not constant
    tmp.c:124: warning: data definition has no type or storage class
    tmp.c:126: parse error before string constant
    tmp.c:126: warning: data definition has no type or storage class
    tmp.c:127: parse error before string constant
    tmp.c:127: warning: data definition has no type or storage class
    tmp.c:128: parse error before string constant
    tmp.c:128: warning: data definition has no type or storage class
    tmp.c:129: parse error before string constant
    tmp.c:129: warning: data definition has no type or storage class
    tmp.c:130: parse error before string constant
    tmp.c:130: warning: data definition has no type or storage class
    tmp.c:131: parse error before string constant
    tmp.c:131: warning: data definition has no type or storage class
    tmp.c:132: parse error before string constant
    tmp.c:132: warning: data definition has no type or storage class
    tmp.c:133: parse error before string constant
    tmp.c:133: warning: data definition has no type or storage class
    tmp.c:134: parse error before string constant
    tmp.c:134: warning: data definition has no type or storage class
    tmp.c:135: parse error before string constant
    tmp.c:135: warning: data definition has no type or storage class
    tmp.c:136: parse error before string constant
    tmp.c:136: warning: data definition has no type or storage class
    tmp.c:137: parse error before string constant
    tmp.c:137: warning: data definition has no type or storage class
    tmp.c:138: parse error before string constant
    tmp.c:138: warning: data definition has no type or storage class
    tmp.c:139: parse error before string constant
    tmp.c:139: warning: data definition has no type or storage class
    tmp.c:140: parse error before string constant
    tmp.c:140: warning: data definition has no type or storage class
    tmp.c:141: parse error before string constant
    tmp.c:141: warning: data definition has no type or storage class
    tmp.c:142: parse error before string constant
    tmp.c:142: warning: data definition has no type or storage class
    tmp.c:143: parse error before string constant
    tmp.c:143: warning: data definition has no type or storage class
    tmp.c:144: parse error before string constant
    tmp.c:144: warning: data definition has no type or storage class
    tmp.c:145: parse error before string constant
    tmp.c:145: warning: data definition has no type or storage class
    tmp.c:146: parse error before string constant
    tmp.c:146: warning: data definition has no type or storage class
    tmp.c:147: parse error before string constant
    tmp.c:147: warning: data definition has no type or storage class
    tmp.c:148: parse error before string constant
    tmp.c:148: warning: data definition has no type or storage class
    tmp.c:149: parse error before string constant
    tmp.c:149: warning: data definition has no type or storage class
    tmp.c:154: conflicting types for `key_wait'
    tmp.c:28: previous declaration of `key_wait'
    tmp.c:154: warning: data definition has no type or storage class
    tmp.c:155: parse error before `}'
    tmp.c:156: warning: parameter names (without types) in function declaration
    tmp.c:156: warning: data definition has no type or storage class
    tmp.c:157: parse error before `}'
    ...after fixing that,
    post all of your code from your text editor ( with indentation intact ) and ill have a look..

    [edit]
    post it as an attachement as we've seen the general idea before
    i want your latest copy, but attach it..
    The world is waiting. I must leave you now.

  4. #19
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    These are not valid comments:
    Code:
    / **************************************************
    **********/
    You have to start with /* and end with */ (no spaces between * and / ). Also, the // style comments are a modern effort, some compilers won't like them, so best stick to /* */ to be portable as poss for now.

    You're missing a closing } in the last function, but you probably can't see it easily because the indentation isn't correct.

    OK, another thing, what is the file client.dat to contain? Is it binary info made up of struct fclients, or is it pure text? The first is the way to go, but I don't know what you assignment requires. For now though, I'll assume it's the struct version.

    If link lists are not allowed (which is one of the best ways to store your data), then can you use arrays? If so, you could make an array of struct fclient's, with say 100 elements. That way your database can hold 100 entries.

    Assuming you're not allowed to do that either (which would be pretty dumb), the only other way I can think is to only have the one struct available, and populate it with data from the file, one at a time.

    And, some comments on your struct:
    Code:
    struct fclient
    {
    	int client_code;
    	char clientname;
    	char street_address;
    	char city;
    	char pros;
    	char country;
    	char postalorzip_code;
    	int deleted; //used as a boolean (0=false, 1=true)
    };
    The problem here is that each char variable is only one char long. You need to do something more like this:
    Code:
    #define MAX 100
    struct fclient
    {
    	int client_code;
    	char clientname[MAX];
    	char street_address[MAX];
    	char city[MAX];
    	char pros[MAX];
    	char country[MAX];
    	char postalorzip_code[MAX];
    	int deleted; //used as a boolean (0=false, 1=true)
    };
    As you're having trouble with the read/write part, here's some example code for you to ponder over. It simply :
    - creates a struct fclient with some default info and displays it.
    - Opens the client.dat file for binary append writing
    - Writes out the struct and closes the file.
    - Then, it opens the file again, this time for binary read.
    - Loops round, reading each struct in the file and displaying it.
    - Closes file, end of prog.

    The first time you run this it will create client.dat and store/retrieve one record (struct). But, run it again, and it will store 1 struct, and retrieve 2. Next time, store 1, retrieve 3 and so on.

    This is just an example to show you how struct writing/reading can work, you'll need to adapt it to suit your own needs. Hopefully though, it will start you off.

    Code:
    #include <stdio.h>
    
    #define MAX 100
    #define FILENAME "client.dat"
    
    struct fclient
    {
    	int client_code;
    	char clientname[MAX];
    	char street_address[MAX];
    	char city[MAX];
    	char pros[MAX];
    	char country[MAX];
    	char postalorzip_code[MAX];
    	int deleted; //used as a boolean (0=false, 1=true)
    };
    
    void PrintClient(struct fclient *);
    
    int main(void)
    {
    	FILE *fp;
    	struct fclient new_client = {
    			1,
    			"C Coding Ltd", 
    			"1 Code Street", 
    			"Code City",
    			"Somewhere",
    			"Code Country",
    			"c99 s11",
    			0 };
    	struct fclient another_client;  /* use to receive data FROM the file */
    	
    	printf ("Before writing to the file:\n");
    	PrintClient(&new_client);
    	
    	/* First open file for binary write (append mode) */
    	if ((fp = fopen (FILENAME, "ab")) == NULL)
    	{
    		perror(FILENAME);
    		return (1);
    	}
    	
    	/* Now write one struct out */
    	if ((fwrite(&new_client, sizeof(struct fclient), 1, fp)) != 1)
    	{
    		perror("write");
    		return (1);
    	}
    	
    	fclose(fp);
    	
    	/* Now open the file again for binary read */
    	if ((fp = fopen (FILENAME, "rb")) == NULL)
    	{
    		perror(FILENAME);
    		return (1);
    	}
    	
    	/* Now loop, reading in all structs in the file */
    	while ((fread(&another_client, sizeof(struct fclient), 1, fp)) == 1)
    	{
    		printf ("\nAfter reading back from the file:\n");
    		/* each struct loaded gets passed to PrintClient */
    		/* but it's just as easy to use each one for whatever */
    		/* we want before the next is loaded */
    		PrintClient(&another_client);
    	}
    	
    	fclose(fp);
    	
    	return (0);
    }
    
    void PrintClient(struct fclient *client)
    {
    	printf ("Client ID:\t%03d\n", client->client_code);
    	printf ("Client street:\t%s\n", client->street_address);
    	printf ("Client city:\t%s\n", client->city);
    	printf ("Client pros:\t%s\n", client->pros);
    	printf ("Client country:\t%s\n", client->country);
    	printf ("Client zipcode:\t%s\n", client->postalorzip_code);
    	printf ("Client deleted:\t%s\n", (client->deleted)?"Deleted":"Active");
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #20
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Thank you very much for your help and example. I think it gave me a better understanding and a bit of direction but there are still a couple things I can not grasp:

    1.) In my add record func(), how can I take the users input that the program scans and save those pieces of info into one fclient address before writing it to file?i.e. &new_client or &another_client?

    2.) For my search func(), how can I search the file(client.dat) for a client code requested by the user and display all of the info associated with the requested code? I only want to display the one address that the user has requested, not everything contained in the client.dat file.

    For you to better understand what I'm asking I have attached a full copy of my current code. The func's(add_record, delete, search, print_label)don't work yet but I think once the two questions I have been solved I will be able to complete the program. I appreciate the help more than I can express. If your ever in Halifax N.S. Canada and need a place to stay, let me know, you'll be more than welcome. Thx, peace.
    Attached Files Attached Files

  6. #21
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In my add record func(), how can I take the users input that the program scans and save those pieces of info into one fclient address before writing it to file?i.e. &new_client or &another_client?
    Simple. At the moment, your code is reading into seperately named variables, all of which mimic the elements of the struct. The quickest way to get this information into a struct, is to read directly from the keyboard into a temporary struct. Like this:
    Code:
    	#include <stdio.h>
    	#include <stdlib.h>
    	..........
    	/* lots more code here */
    
    	..........
    
    	char buffer[MAX];
    	struct fclient new_client;
    
    	/*
    	 * This next bit asks the user for each piece of information,
    	 * and stores the input in the struct
    	 */
    	printf ("Enter Client Code: ");
    	fgets(buffer, MAX, stdin);
    	new_client.client_code = atoi(buffer);
    	
    	printf ("Enter Client Name: ");
    	fgets(new_client.clientname, MAX, stdin);
    	
    	printf ("Enter Address: ");
    	fgets(new_client.street_address, MAX, stdin);
    	
    	printf ("Enter City: ");
    	fgets(new_client.city, MAX, stdin);
    	
    	printf ("Enter pros: ");
    	fgets(new_client.pros, MAX, stdin);
    	
    	printf ("Enter Country: ");
    	fgets(new_client.country, MAX, stdin);
    	
    	printf ("Enter Postal Or Zip Code: ");
    	fgets(new_client.postalorzip_code, MAX, stdin);
    	
    	new_client.deleted = 0;
    	/* End of get user input */
    Note that this does little error checking (actually none really), but I do it this way to keep the code minimal to help you understand what's going on. Most of the above is self explanatory, except for the first read, where we get the client_code. Because this is numeric, we read into a temporary buffer, then use atoi() to convert the string into a number. If the string is not a valid number, the atoi() function gives us 0, so at least the program can continue. In a fully developed program, plenty of error checking would be done here.

    Now you have the information in a struct, you are free to print it, or write it to a file (or whatever you like).

    For my search func(), how can I search the file(client.dat) for a client code requested by the user and display all of the info associated with the requested code? I only want to display the one address that the user has requested, not everything contained in the client.dat file.
    Again, simple enough, I've already given you 90% of the code in my last post.
    First you need to get the client_code from the user. Create a temporary int variable to hold it, and use the same method as given above to obtain it (fgets() to a buffer, then atoi() to convert. Or if you feel more comfortable, use scanf() ).

    Now, look back at my source in previous post, find the part starting with comment
    >/* Now open the file again for binary read */
    This opens the file, as stated, then reads each struct in from client.dat one at a time. In my code, I passed every struct to function PrintClient(&another_client); To display only one struct, you must simply do a compare before calling the PrintClient() function. Something like
    Code:
    if (user_inputted_client_code == another_client.client_code)
    {
             PrintClient(&another_client);
    }
    I appreciate the help more than I can express. If your ever in Halifax N.S. Canada and need a place to stay, let me know, you'll be more than welcome. Thx, peace.
    No problem, I'm glad to help. Thanks for your offer, but it's a bit of a long way for me to go!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #22
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    Last Question I Swear!


    Your help has put me inches from the finish line. I hate to even ask you after all the help you have provided, but my progran has a few syntax errors that I can't seem to fix. I've tried for about 3 hours now and I still can't figure it out! Would you mind taking one last look at my roughly finished code and see if the errors are a simple fix, or if the problem runs deep into the code? Much appreciation and respect.
    Attached Files Attached Files

  8. #23
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Any errors in particular.... or is it all of them

    >Line 62: if (dta_file !=fclose)
    The file is already open, so forget the if statement and just fclose() the file.

    >Line 117: fgets(buffer,Max,stdin);
    Max should be MAX

    >char add_record(void)
    This function is getting a bit confusing. You're trying to do too many things within it.
    I'd suggest: Start by getting the data from the user, then writing the struct to the file (append mode). Don't worry if it's unique or not until you get this part working.
    Then, write a function to read all the structs in the file, checking for one that has a specific ID code. If you find a match, return TRUE, if not return FALSE. You can then insert a call to this new function in add_record(), just prior to writing out the data.

    In the meantime:
    >Line 119: while(dta_file != NULL)
    You haven't opened the file yet, so you can't test dta_file.

    >if (buffer != client_code)
    Not sure where you're going with this!

    >dta_file=dta_file;
    ...or this

    >if ((fwrite(&new_client, sizeof(struct fclient), client_code, dta_file))! = client_code)
    The ! and = must be together. And you should be testing against 1 not client_code, which represents the number of structs written to the file.

    >return(0);
    Is in the wrong place, it needs to be at the end of the function.

    >Missing }
    There's on missing from the end of add_record().

    There are some more, but that'll do for now, you can have a go at sorting out the rest!

    Have fun and good luck. Post again if you want more help.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #24
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    Me again!?!?!?!?


    This is eating what's left of my brain! I have made appropriate changes and cleaned up the add func(). I have one error left in my delete func(). It's highlighted in red.
    Code:
    while((fread(&another_client,sizeof(struct fclient),1,dta_file)) == 1)
    {
      if(client_code == another_client.client_code)
       {
          another_client.client_code->deleted=1 
          printf("\n\nThe client has been deleted from the inventory...");
       }
       else
       {
           printf("\n\nThat client code does not exist...");
       }
    I don't see the problem? The line should delete the called struct from inside the file. Tips? Also you asked what what this line was supposed to be:
    Code:
    if (buffer != client_code)
    Here is what I thought it meant:
    buffer would be = to the client_code entered by the user?
    client_code would be = to the client_codes already written into the file?
    I guess I am off a bit(alot).
    I also attached the newest version of code as well. Thx again.
    Attached Files Attached Files

  10. #25
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >another_client.client_code->deleted=1

    I don't see the problem? The line should delete the called struct from inside the file. Tips? Also you asked what what this line was supposed to be:
    This line won't delete anything itself, it mearly changes the value of the int variable to 1. And in your code, it only does this for the struct in memory, not on disk. To actually delete the struct from the file is another matter altogether, for which there are many answers.

    You could
    - Read each struct in, and write them all out to a new file with the exception of the one you want to delete. Then, when done, delete the client.dat file, and rename in the new file to this name.
    - Read in all structs to memory, the write them all back out to the client.dat file.
    - Just update the deleted element of the fclient struct, and rewrite the single struct in client.dat. This would leave that struct in there, but as it is now marked as deleted, you could amend the rest of your code to ignore such items.

    There's probably more ways too.


    >if (buffer != client_code)
    This line.... client_code isn't equal to all the codes in the file. Infact, at the point in which you'd used it, it wasn't equal to anything in particular. It had been defined here:
    >int client_code;
    Remember, a variable only lasts as long as the function it's created in (unless it's static, but don't worry about that for now).
    Once you enter a function, all the variables you request are created in memory, but their values are not set until you do so. If you leave the function, and then come back in again, the original values have gone, and you start over.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #26
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Hammer

    This line won't delete anything itself, it mearly changes the value of the int variable to 1.


    Is there any point in doing this then?


    You could
    - Read each struct in, and write them all out to a new file with the exception of the one you want to delete. Then, when done, delete the client.dat file, and rename in the new file to this name.
    - Read in all structs to memory, the write them all back out to the client.dat file.
    - Just update the deleted element of the fclient struct, and rewrite the single struct in client.dat. This would leave that struct in there, but as it is now marked as deleted, you could amend the rest of your code to ignore such items.

    There's probably more ways too.


    Would you happen to have an example of one of these options? Preferably (in your opinion) the easiest one. The books I have don't give any examples and the explanations are extremely brief.
    I don't know where to start?


Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on removing valid (but minor) compiler warnings
    By PaulBlay in forum C Programming
    Replies: 12
    Last Post: 04-20-2009, 01:16 PM
  2. How to solve warnings that appear when building my DLL...
    By starcatcher in forum Windows Programming
    Replies: 6
    Last Post: 12-14-2008, 11:47 AM
  3. Warnings when using vector of vector
    By Boksha in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2008, 02:54 PM
  4. Replies: 9
    Last Post: 03-14-2008, 10:55 AM
  5. Warnings from String manipulation functions.
    By Arker in forum C Programming
    Replies: 4
    Last Post: 10-15-2002, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21