Help File handling

This is a discussion on Help File handling within the C Programming forums, part of the General Programming Boards category; Code: /* Function Protypes*/ void saveToFile(FILE*); void getFromFile(FILE*); void saveToFile(FILE*fp){ fp = fopen("c:BOOKS.dat","wb"); int i; for(i=0;i<SIZE;i++) { fwrite(&books[i],sizeof(BOOK),1,fp); fclose(fp); } ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Help File handling

    Code:
    /* Function Protypes*/
    void saveToFile(FILE*);
    void getFromFile(FILE*);
    
    
         
    void saveToFile(FILE*fp){
         fp = fopen("c:BOOKS.dat","wb");
         int i;
         
         for(i=0;i<SIZE;i++)
         {
          fwrite(&books[i],sizeof(BOOK),1,fp);
          fclose(fp);
                           }
         }
         
    void getFromFile(FILE*fp){
         int i =0;
         
         printf("Retrieving Books from file");
         
         while(fread(&books[i],sizeof(BOOK),1,fp)!= NULL)
         i++;
         fclose(fp);
         
         } 
    
    do{
            FILE*fp;
      
      
      if((fp=fopen("c:BOOKS.DAT","wb")) ==NULL){
    
      printf("there is no file saved");
                 } else
                 getFromFile(fp);
     
      runMainMenu();
      scanf("%d",&numSelected) ;
      switch(numSelected){
      
            case 1: addBook(); break;
            case 2: deleteBook(); break;
            case 3: viewBooks(); break;
            case 4: viewABook(); break;
            case 5: printf(" "); break;
    
            }

    ok when i try to compile this code(this is only a small amount of the code)

    i get an error
    $(CC) -c main.c -o main.o $(CFLAGS)

    no idea why???? help??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    What's the error?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Where is main()? Forgot about it perhaps?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    thats it

    main.o: main.c
    $(CC) -c main.c -o main.o $(CFLAGS)

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Code:
    int main(){
        char delay;
        int numSelected,i;
        
    
     do{
            FILE*fp;
      
      
      if((fp=fopen("c:BOOKS.DAT","wb")) ==NULL){
    
      printf("there is no file saved");
                 } else
                 getFromFile(fp);
     
      runMainMenu();
      scanf("%d",&numSelected) ;
      switch(numSelected){
      
            case 1: addBook(); break;
            case 2: deleteBook(); break;
            case 3: viewBooks(); break;
            case 4: viewABook(); break;
            case 5: printf(" "); break;
    
            }
               saveToFile(fp);
       }while(numSelected != 5);
    
      
        return 0;
    }

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by molleman View Post
    i get an error
    $(CC) -c main.c -o main.o $(CFLAGS)

    no idea why???? help??
    Do you mean that this is the "error" you get? do you have a Makefile? Post the makefile contents.
    Inside [code] tags.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    If you have a makefile there should be exactly one TAB character on the lines that are like this:
    Wrong:
    Code:
    main.o: main.c
    $(CC) -c main.c -o main.o $(CFLAGS)
    Right:
    Code:
    main.o: main.c
    	$(CC) -c main.c -o main.o $(CFLAGS)
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Code:
    # Project: AlgorithmsTest
    # Makefile created by Dev-C++ 4.9.9.2
    
    CPP  = g++.exe
    CC   = gcc.exe
    WINDRES = windres.exe
    RES  = 
    OBJ  = main.o main.o $(RES)
    LINKOBJ  = main.o main.o $(RES)
    LIBS =  -L"C:/Dev-Cpp/lib"  
    INCS =  -I"C:/Dev-Cpp/include" 
    CXXINCS =  -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include" 
    BIN  = AlgorithmsTest.exe
    CXXFLAGS = $(CXXINCS)  
    CFLAGS = $(INCS)  
    RM = rm -f
    
    .PHONY: all all-before all-after clean clean-custom
    
    all: all-before AlgorithmsTest.exe all-after
    
    
    clean: clean-custom
    	${RM} $(OBJ) $(BIN)
    
    $(BIN): $(OBJ)
    	$(CC) $(LINKOBJ) -o "AlgorithmsTest.exe" $(LIBS)
    
    main.o: main.c
    	$(CC) -c main.c -o main.o $(CFLAGS)
    
    main.o: main.c
    	$(CC) -c main.c -o main.o $(CFLAGS)

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    I am not on Windows atm, but i recall that when a new file is added to a Dev-Cpp project, it might be marked by the IDE as C++. You should go to Project - Options.
    From there i think you have a Files tab.
    Check on that, choose the files of your project and verify that they are targetting C, and not C++.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    C:\Users\molleman\Desktop\C Programming\Makefile.win [Build Error] [main1.o] Error 1

    i got this error.....

  11. #11
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    It looks like a linker error. Your object files (xxx.o) might have been compiled as C++ code, and the linker is failing to bind them.
    Do a clean, check and fix the files in your project so that they are taged by Dev-Cpp as C, and not C++, and try Build All again.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    this code should be compliable..........

    can someone try compile it!!!! please...it wont work for me




    Code:
    /*Here i must include the standard input output libary for input and output from the keyboard 
    i must include the string library so i can use the some string functions to compare and copy character strings
    i define a variable called SIZE that will be the size of my array of books*/
    #include <stadio.h>
    #include <string.h>
    #define SIZE 2
    
    /* Book Struct. this is the structure that we were told to create. A Struct book has a serial, title,author and a year
    of publication*/
    
    struct book{
           long serial;
           char title[20];
           char author[20];
           int yearOfPublication;
           };
           
           
    /* Type Defintion for struct book. I find it easier to have a type def as i must only use word when referring to a struct*/
    
    typedef struct book BOOK;
    
    /* Function Protypes*/
    
    void addBook();
    void deleteBook();
    void viewBooks();
    void viewABook();
    void delayMessage();
    void saveToFile(FILE*);
    void getFromFile(FILE*);
    
     
    
    /* Global Variable, books is my array of struct book which has a limit to the size of it as defined above by SIZE. Count
    is the number of books that are in the array at any given time*/
    
    BOOK books[SIZE];
    int count= 0;
    char quit[]= "quit";
    
    
    /* Functions */
    void addBook(){              
      
         printf("Please enter in the serial of the book in position %d\n",count+1);
         scanf("%ld",&books[count].serial);
         fflush(stdin);
         
      // the reason for gets is that if a book has spaces in the name it can be taken in by this function. unlike a scanf function.
             
             printf("Please enter in the title of the book in position %d\n",count+1);
             gets(books[count].title);
             printf("Please enter in the author of the book in position %d\n",count+1);
             gets(books[count].author);
             printf("Please enter in the year Of Publication of the book in position %d\n",count+1);
             scanf("%d",&books[count].yearOfPublication);
         //Error checking for  year of publication if it is after 2000 it must re-enterd
                 if(books[count].yearOfPublication > 2000){
                                                   printf("The Year of publication must be before than 2000, please enter in the correct value\n");
                                                   scanf("%d",&books[count].yearOfPublication);
                                                   }
                                                   count++;
                                                   printf("Adding Book...");
                                                   delayMessage();     
              }
         
    void viewBooks(){
     printf("Displaying all books...\n");
     for(int i = 0; i< count;i++){
         
        // printf("%d. Bookname %s \n Book Serial %ld\n",i+1,books[i].title,&books[i].serial);
      printf("Book name : %s \n",books[i].title );
      printf("Book Serial: %ld \n",books[i].serial);
      printf("Authors name : %s \n",books[i].author );
      printf("Book year %d\n",books[i].yearOfPublication);
      }
      delayMessage();
         }
    
    void viewABook(){
     char searchingName[20];
     int found;
    
      fflush(stdin);
    
     printf("please enter in the title of the book you are searching for:\n");
     gets(searchingName);
    
     for (int x = 0; x <count;x++){
     found = strcmp(searchingName, books[x].title );
     if(found == 0){
      printf("book found \n \n");
      printf("Book name : %s \n",books[x].title );
      printf("Book Serial %ld\n",&books[x].serial);
      printf("Authors name : %s \n",books[x].author );
      printf("Book year %d\n",&books[x].yearOfPublication);
     }else
          printf("Book could not be found");
     }
     delayMessage();
     
    }
    
    void deleteBook(){
     long searchingSerial;
     int found;
     int check = 0;
     fflush(stdin);
    
     printf("please enter in the serial of the book you wish to delete:\n");
     scanf("%ld",&searchingSerial);
    
     for (int x = 0; x <count;x++){
         if(searchingSerial == books[x].serial){
         // overwrites the postion of the book
          books[x]= books[x+1];
           check = 1;
           count--;
           printf("Deleting book...");
           }else if(check == 0)
           printf("The book could not be found");
           } 
           delayMessage();
    }
    
    void runMainMenu(){
         printf("1. Add a Book\n");
         printf("2. Delete a Book\n");
         printf("3. View all Books\n");
         printf("4. View a Specific Book\n");
         printf("5. Exit the System\n");
         printf("Press number for option\n");
         }
         
    void delayMessage(){
         char delay;
         
         printf("\n Please press enter to return to the main menu\n");
         fflush(stdin);
         scanf("%c",&delay);
         }
         
    void saveToFile(FILE*fp){
         fp = fopen("C:BOOKS.dat","wb");
         int i;
         
         for(i=0;i<SIZE;i++)
         {
          fwrite(&books[i],sizeof(BOOK),1,fp);
          fclose(fp);
                           }
         }
         
    void getFromFile(FILE*fp){
         int i =0;
         
         printf("Retrieving Books from file");
         
         while(fread(&books[i],sizeof(BOOK),1,fp)!= NULL)
         i++;
         fclose(fp);
         
         }     
         
       
    
    /* my main method*/
    int main(){
        char delay;
        int numSelected,i;
        
    
     do{
            FILE*fp;
      
      
      if((fp=fopen("C:BOOKS.DAT","wb")) ==NULL){
    
      printf("there is no file saved");
                 } else
                 getFromFile(fp);
     
      runMainMenu();
      scanf("%d",&numSelected) ;
      switch(numSelected){
      
            case 1: addBook(); break;
            case 2: deleteBook(); break;
            case 3: viewBooks(); break;
            case 4: viewABook(); break;
            case 5: printf(" "); break;
    
            }
               saveToFile(fp);
       }while(numSelected != 5);
    
      
        return 0;
    }

  13. #13
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Your code compiles on my box, after the following fix:
    Code:
    #include <stadio.h>
    to
    Code:
    #include <stdio.h>
    > gcc -std=iso9899:1999 -o file file.c
    Code:
     for(int i = 0; i< count;i++){
         
        // printf("&#37;d. Bookname %s \n Book Serial %ld\n",i+1,books[i].title,&books[i].serial);
      printf("Book name : %s \n",books[i].title );
      printf("Book Serial: %ld \n",books[i].serial);
      printf("Authors name : %s \n",books[i].author );
      printf("Book year %d\n",books[i].yearOfPublication);
      }
    The declaration is C99 and it would be better if it was moved out of the for, also for the following piece.
    Code:
     for (int x = 0; x <count;x++){
         if(searchingSerial == books[x].serial){
         // overwrites the postion of the book
          books[x]= books[x+1];
           check = 1;
           count--;
           printf("Deleting book...");
           }else if(check == 0)
           printf("The book could not be found");
           }
    And gets is generally better off forgotten. Read the forum FAQ.
    Last edited by xuftugulus; 03-10-2008 at 02:47 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  14. #14
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Your program has some logical errors.
    The saveToFile function should use the variable 'count' instead of the constant SIZE, or the program crashes for me at least.
    Also the getFromFile should update the value of 'count' to match the number of BOOK items read.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    1. fflush(stdin); - undefined, read FAQ

    2. gets(books[count].title); - do not use gets - read FAQ

    3. and do not use globals

    4. check return value of scanf - you can get some garbage in your vars overwise

    5. read about the return value of fread and fix your == NULL crap

    6. check your array bounds

    7. saveToFile - get the file close out of cycle

    8. fix the indentation

    9 check the return value of fwrite as well
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 05:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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