Beginners Help

This is a discussion on Beginners Help within the C Programming forums, part of the General Programming Boards category; Hi guys i've just started to learn the c programming language in computing class so im a real noob. This ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    24

    Beginners Help

    Hi guys i've just started to learn the c programming language in computing class so im a real noob. This code is writing seven records to a binary file succesfully but the text its outputing is all over the place and not very neat when i open it in notepad. Is there any way to control the way it appears when i open it in notepad? I also just read in another post that you should never use fflush(stdin) but my teacher uses it so i think ill probably need to include that if you's dont mind.

    Any help would be much appreciated.

    Thanks
    Code:
     
    void add_book()
    {
    	record_file = fopen("C:\\bookfile.bin","wb");
        
        system("cls");
        
        printf( "\n\nAdd book details");
        
        for(num_records=0;num_records<=7;num_records++)
             {
               printf( "\n\nISBN: ");
               fflush(stdin);
               gets(record_array[num_records].ISBN);
               printf( "\n\nAuthor: ");
               fflush(stdin);
               gets(record_array[num_records].Author);
               printf( "\n\nTitle: ");
               fflush(stdin);
               gets(record_array[num_records].Title);
               printf( "------------------------------------" );
               
               fwrite(&record_array[num_records],sizeof(record_structure),1,record_file);
              } 
              
               fclose(record_file);
        
        screen_pause();
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hard to tell without seeing the output it produces or your struct definition. But I notice that you use gets(). It's dangerous since a string longer than what you have room for will over write memory outside of your declaration. Use fgets() instead called like this:

    Code:
    fgets(char *restrict s, int n, FILE *restrict stream);
    Where n is the maximum characters that will be read -1, to account for the terminating \0 character. So, in your case something like this:

    Code:
    fgets(record_array[num_records].ISBN, size, stdin);
    Last edited by Subsonics; 05-20-2010 at 02:50 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    24
    Thanks for your reply subsonics ive made the changes you suggested but im getting an error message now ((warning return type of 'main' is not int)) so i thought id post all the code. This was a sample menu program given to us by our teacher, i have only edited void_book() so far all the rest is mostly untouched.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    /* global variables */			
    typedef struct
    {
      char ISBN[20];
      char Author[50];
      char Title[50];
    }record_structure;  
    
    FILE *record_file;
    int choice;
    record_structure record_array[8];
    int num_records;
    
    /* function prototypes */
    void intro();
    void menu();
    void close();
    void add_book();
    void option2();
    void option3();
    void option4();
    void screen_pause();
    
    /*******************************************************************************************/
    
    void main()
    {
    	intro();
    	do
    	{
    		menu();
    	}
    	while (choice!=5);
    	close();
    }/* end of main function */
    
    /*******************************************************************************************/
    
    void intro()
    {
    	system("cls");
    	printf("\n\n\n\n\n\n\tWelcome to Forth Valley's Collection of Technical Books");
    	screen_pause();
    }/*end of intro function*/
    
    /*******************************************************************************************/
    
    void add_book()
    {
    	record_file = fopen("C:\\bookfile.bin","wb");
        
        system("cls");
        
        printf( "\n\nAdd book details");
        
        for(num_records=0;num_records<=7;num_records++)
             {
               printf( "\n\nISBN: ");
               fflush(stdin);
               fgets(record_array[num_records].ISBN, size, stdin);
               printf( "\n\nAuthor: ");
               fflush(stdin);
               fgets(record_array[num_records].Author, size, stdin);
               printf( "\n\nTitle: ");
               fflush(stdin);
               fgets(record_array[num_records].Title, size, stdin);
               printf( "------------------------------------" );
               
               fwrite(&record_array[num_records],sizeof(record_structure),1,record_file);
              } 
              
               fclose(record_file);
        
        screen_pause();
    }/* end of dummy menu option 1 function */
    
    /*******************************************************************************************/
    
    void option2()
    {
    	system("cls");
    	printf("Search for ISBN ");
    	screen_pause();
    }/* end of dummy menu option 2 function */
    
    /*******************************************************************************************/
    
    void option3()
    {
    	system("cls");
    	printf("Search for a book by title ");
    	screen_pause();
    }/* end of dummy menu option 3 function */
    
    /*******************************************************************************************/
    
    void option4()
    {
    	system("cls");
    	printf("Show details of all books ");
    	screen_pause();
    }/* end of dummy menu option 4 function */
    
    /*******************************************************************************************/
    
    void close()
    {
    	system("cls");
    	printf("\n\n\n\n\n\n\tThank you for using this program");
    	screen_pause();
    }/* end of close function */
    
    /*******************************************************************************************/
    
    void menu()
    {
    	system("cls");
    	printf("\n\nMENU");
    	printf("\n\n1 - Add new entry: ");
    	printf("\n2 - Book search by ISBN: ");
    	printf("\n3 - Book search by title: ");
    	printf("\n4 - Show details off all books stored: ");
    	printf("\n5 - Exit Program:");
    	
        printf("\n\nEnter your choice ");
        scanf("%i", &choice);
        while((choice<1)||(choice>5))
        {
         printf("\n\nYou eneterd an invalid number please enter a valid number");
         printf("\n\n\tEnter your choice: ");
         scanf("%i", &choice);
         }
           
    	switch(choice)
    	{
    		case 1 : add_book();break;
    		case 2 : option2(); break;
    		case 3 : option3(); break;
    		case 4 : option4() ; break;
    		case 5 :  break;
    	}
    }/* end of menu function */
    
    /*******************************************************************************************/
    
    void screen_pause()
    {
    	printf("\n\n\tPress any key to continue.....");
    	getch();
    }/* end of screen_pause function */

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You don't have a return type in main, as you declare it as "void main", if you do:

    Code:
    int main()
    {
            //your code..
    
            return 0;
    }
    Code:
    typedef struct
    {
      char ISBN[20];
      char Author[50];
      char Title[50];
    }record_structure;
    I meant for the "size" argument to be replaced with the size you had declared, not size typed literarily.

    So in you case, size would be 20, 50, 50 in your three fgets calls. But I propose that you define size as a constant and use it in both the declaration and in your fgets calls, easier to change and comprehend and also less likely for mistakes to happen. So for example:

    Code:
    #define SIZE 50
    
    
    typedef struct
    {
      char ISBN[SIZE];
      char Author[SIZE];
      char Title[SIZE];
    }record_structure;

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Yeah but when you're writing the structs to a "binary" file, of course it won't look good when opening with a text editor like notepad. The elements of the structure are not always filled to their capacity.... leaving perhaps garbage characters after the terminating null. Also no one placed new-lines between the elements or records so that notepad would display things in some sort of list.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, writing the entire structure to the file will likely result in some garbage (unused bytes) and probably pad bytes and so on. But the newline should be part of the strings, entered at stdin.
    Last edited by Subsonics; 05-20-2010 at 04:06 PM.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Wait: you want to make a binary file readable in Notepad? Binary files in case you don't know are for storing things in a binary format as efficiently as possible (without compression et al). Text files are easy to read from and write to with something like Notepad but are not as efficient to read and write to from a program....if you MUST look at the files in binary, its useful to make a little program that does nothing but print out the contents of the file in text format. If you feel you need to edit it, I would suggest something like writing to a CSV file which would look like:
    Code:
    "field1","field2","fieldN"
    But I don't think you are ever going to get a binary file readable and worse, I doubt you will ever be able to edit (much of) a binary file with Notepad before it becomes unusable..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners C Programming Challenge
    By UCnLA in forum C Programming
    Replies: 23
    Last Post: 04-01-2008, 07:46 PM
  2. Beginners C Programming Challenge
    By UCnLA in forum C Programming
    Replies: 2
    Last Post: 03-18-2008, 12:15 PM
  3. Exercises for Beginners!
    By alokrsx in forum C++ Programming
    Replies: 11
    Last Post: 10-02-2006, 11:59 AM
  4. for beginner's or anyone interested
    By Iconoklast in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-25-2004, 01:45 PM
  5. What is a good beginners' C++ book?
    By GrNxxDaY in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2002, 09:50 AM

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