Thread: FREAD error

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    46

    FREAD error

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // struct containing recipe
    typedef struct {
     char recipeName[40];
     char recipeHowTo[1000];
     int howManyPerson;
     int level;
     int timeToEat;
    } recipe;
    
    // struct for list
    struct element {
     recipe data;
     struct element *elemPtr;   
    };
    
    // prototypes
    struct element *menu(int choice, struct element *list);
    struct element *insertRecipe(struct element *listPtr);
    struct element *editRecipe(struct element *listPtr);
    void listRecipes(struct element *listPtr);
    void saveRecipes(struct element *listPtr);
    struct element *loadRecipes(struct element *listPtr);
    
    int main() {  
      int choice = 2 ;
      struct element *list = NULL;  
      
      int status = 0;
      while( status != 1 ){
         list = loadRecipes(list); 
     }
      
      system("PAUSE");	
      return 0;
    }
    
    
    
    //LOAD RECIPES
    struct element *loadRecipes(struct element *listPtr){
    
    FILE *filePtr = fopen("recipes.txt","r");
    recipe x;
    struct element *xPtr;
    
    
       if(( filePtr = fopen("recipes.txt","rb"))== NULL){
           printf("Error in file opening");
       }
       printf("debug1");
       while( fread(&x,sizeof(recipe),1,filePtr) != NULL ){ 
           printf("debug2");
           if(listPtr == NULL){ printf("debug3");
               listPtr = (struct element *) malloc(sizeof (struct element));
               strcpy(listPtr->data.recipeName,x.recipeName);
               strcpy(listPtr->data.recipeHowTo,x.recipeHowTo);
               //strcpy(listPtr->data.howManyPerson,x.howManyPerson);
               //strcpy(listPtr->data.level,x.level);
               //strcpy(listPtr->data.timeToEat,x.t);
               listPtr->elemPtr = NULL;
           }else{ printf("debug4");
               xPtr = (struct element *) malloc(sizeof (struct element));
               strcpy(xPtr->data.recipeName,x.recipeName);
               strcpy(xPtr->data.recipeHowTo,x.recipeHowTo);
               //strcpy(xPtr->data.howManyPerson,x.howManyPerson);
               //strcpy(xPtr->data.level,x.level);
               //strcpy(xPtr->data.timeToEat,x.t);
               xPtr->elemPtr = xPtr;
               listPtr = xPtr;
           }
          }fclose(filePtr); 
       return(listPtr);
    }
    
    
    
    //SAVE TO TXT
    void saveRecipes(struct element *listPtr){
        FILE *stream = fopen("recipes.txt","w");
        
        while(listPtr != NULL) {
         fprintf(stream,"%s\n%s\n%d%d%d\n\n",listPtr->data.recipeName,listPtr->data.recipeHowTo,listPtr->data.howManyPerson,listPtr->data.level,listPtr->data.timeToEat);   
         listPtr = listPtr->elemPtr;
        }
        fflush(stream);
        fclose(stream);
    }
    i have my recipes.txt files filled
    but when i run this program it arrive to debug1
    and can't enter in the while that read the data and load them to the list

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Answer is in fread documentation.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    FILE *filePtr = fopen("recipes.txt","r");
    recipe x;
    struct element *xPtr;
    
    
       if(( filePtr = fopen("recipes.txt","rb"))== NULL){
           printf("Error in file opening");
       }
    Opening the file twice? Once as text, once as binary? Huh?

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    Quote Originally Posted by Bayint Naung View Post
    Answer is in fread documentation.
    ? where plz?

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    Quote Originally Posted by rags_to_riches View Post
    Code:
    FILE *filePtr = fopen("recipes.txt","r");
    recipe x;
    struct element *xPtr;
    
    
       if(( filePtr = fopen("recipes.txt","rb"))== NULL){
           printf("Error in file opening");
       }
    Opening the file twice? Once as text, once as binary? Huh?
    just a way to understand what was wrong in fread, removed the first fopen :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM