Thread: reading from files

  1. #16
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    sorry for reviving this thread but ive been working so havnt had much of a chance to continue with my learning of c. when i try and open the file it doesnt seem to be finding it, where should the file be placed? here is my code, there may be something wrong:


    list.h
    Code:
      
    
    /*List header file */
    
    #include <stdio.h> /* Header file includes the file stdio.h 
                        because that is where NULL is defined*/
                        
    typedef char DATA[30];  /* Creates names of types that are more 
                        suggestive of their use*/
    
    typedef struct linked_list {
        DATA d;
        struct linked_list *next;
    } Element;
    
    typedef Element* LINK;
    time_lib.h

    Code:
     
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define MAXSTRING 100
    
    typedef struct {
            clock_t   begin_clock,  save_clock;
            time_t    begin_time,   save_time;
    } time_keeper;
    
    static time_keeper  tk;  /* tk is external to the functions and is used
                             for communication between the functions in this
                             file */
    
    void start_time(void)   /* when start_time() is invoked, the values returned
                               from clock() and time() are stored in tk */
    {
         tk.begin_clock = tk.save_clock =clock();
         tk.begin_time = tk.save_time = time(NULL);
    }
    
    double prn_time(void) /* When prn_time() is invoked, new values from clock()
                          and time() are used to compute and print the elapsed user
                          time and the elapsed real time, and new values are stored
                          in tk*/
    {
           char s1[MAXSTRING], s2[MAXSTRING];
           int field_width, n1,n2;
           double clocks_per_sec = (double) CLOCKS_PER_SEC,
                  user_time, real_time;
                  
    user_time = (clock() -tk.save_clock) / clocks_per_sec;
    real_time = difftime(time(NULL),tk.save_time);
    tk.save_clock =clock();
    tk.save_time =time(NULL);
    
    sprintf(s1, "%.1f", user_time);
    n1 = strlen(s1);
    sprintf(s2, "%.1f", real_time);
    n2 =strlen(s2);
    field_width = (n1>n2)? n1 :n2;
    printf("%s%*.1f%s\n%s%*.1f%s\n\n",
       "User time: ", field_width, user_time, "seconds",
       "Real time: ", field_width, real_time, "seconds");
       return user_time;
    }
    list.c

    Code:
    #include "list.h"
    #include "time_lib.h"
    #include <stdlib.h>
    #include <string.h>
    
    
    /*Function to create a list recursivly*/
    
    LINK create( DATA h, Element *t )
    {
        LINK y = calloc( 1, sizeof( Element ) );
        strcpy( y->d, h );
        if ( t ) t->next = y; /* Only do this if there is a previous element. */
        return y;
    }
    
    /* Function to read data from file*/
    
    FILE *readdata(void)
    {
      FILE *ifp;    /*infile pointer*/
      ifp=fopen("birthday.txt","r");   /*Open file birthday for reading only*/
      if(ifp==NULL)
      {
          printf("File Cannot Be Opened\n");
      }
      return ifp;
    
    }
    
    /* Function to insert data into the list*/    
    
    LINK insert(FILE *ifp)
    {
        LINK head = NULL;
        LINK tail = NULL;
        DATA b;
        while( fscanf( ifp, "%s", b ) == 1 ){
               tail = create( b, tail );
               if( !head ) { head = tail; }
        }
        printf("\n");
       return head;  
    } 
    
    /* Function to count the number of elements in the list recursively */
    
    int count(LINK head)
    {
        if(head==NULL) /* If the list is empty , 0 is returned , otherwise
                          the number of elements in the list is returned */
             return 0;
        else           
             return (1+count(head->next));
    } 
    
    /*Function to print list*/
    
    void print_list(LINK head)
    {
        /* This 'for' loop executes while head !=0 */
        LINK next;
        for( ; head ; head = next ) {
        printf( "%s\n", head->d );
        next = head->next;
        }
    }    
             
            
    int main(void)
    {
        LINK head = NULL;
        FILE *ifp;
        start_time(); /* function call to start time in time_lib.h */
        readdata();
        head=insert(stdin);
        prn_time(); /* function call to print time in time_lib.h */
        count(head);
        print_list(head);
        fclose(ifp);
        return 0;
    }
    Last edited by recluse; 12-26-2004 at 08:56 AM.

  2. #17
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    From what I can tell from the rest of the thread, you're trying to open a file called, "birthday.txt", but you only refer to it as, "birthday" in your program. Include the type. Unless you specify other directories (and I can't find anywhere that you do), then you just need to keep all your program files in one folder.

  3. #18
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    thanks sean , its now opening the file but not doing anything else, is it in another endless loop somewhere?

    i get 1 compiler warning : local variable 'ifp' used without having been intialized. is this causing my program not to work?

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by recluse
    thanks sean , its now opening the file but not doing anything else, is it in another endless loop somewhere?

    i get 1 compiler warning : local variable 'ifp' used without having been intialized. is this causing my program not to work?
    Code:
    FILE *ifp;
    start_time(); /* function call to start time in time_lib.h */
    readdata();
    ...
    fclose(ifp);
    See the problem?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    no sorry quzah , i dont see the problem

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >no sorry quzah , i dont see the problem
    You're trying to fclose a file pointer that was never initialized.
    My best code is written with the delete key.

  7. #22
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    so in my main function do i have to initialise ifp to NULL ifp=NULL; , or ifp=fopen("birthday.txt","r"); . if i did the latter wouldnt that just mean i'm repeating code?

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Why not just delete it since it's not being used?
    My best code is written with the delete key.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    FILE *readdata(void)
    {
      FILE *ifp;    /*infile pointer*/
      ifp=fopen("birthday.txt","r");   /*Open file birthday for reading only*/
      if(ifp==NULL)
      {
          printf("File Cannot Be Opened\n");
      }
      return ifp;
    
    }
    Combine with the previously mentioned:
    Code:
    FILE *ifp;
    start_time(); /* function call to start time in time_lib.h */
    readdata();
    ...
    fclose(ifp);
    How about now? Now do you see the problem? Here's a hint: What happens to the return value of readdata?

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    sorry i'm still confused can i not just do what prelude said and remove FILE *ifp from main , as it has been declared in my readdata function?
    Last edited by recluse; 12-26-2004 at 10:30 AM.

  11. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're not inserting correctly:
    Code:
    LINK insert(FILE *ifp)
    {
        LINK head = NULL;
        LINK tail = NULL;
        DATA b;
        while( fscanf( ifp, "%s", b ) == 1 ){
               tail = create( b, tail );
               if( !head ) { head = tail; }
        }
        printf("\n");
       return head;  
    }
    What happens if head is not null? What do you do with tail? (Hint: Nothing at all!)

    [edit]
    No, I correct myself: The problem is in the function create. You aren't linking nodes correctly:
    Code:
    LINK create( DATA h, Element *t )
    {
        LINK y = calloc( 1, sizeof( Element ) );
        strcpy( y->d, h );
        if ( t ) t->next = y; /* Only do this if there is a previous element. */
        return y;
    }
    What you should be doing is making y->next equal t. Not the other way around.
    [/edit]

    Quzah.
    Last edited by quzah; 12-26-2004 at 10:36 AM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM