Help with dynamic memory allocation

This is a discussion on Help with dynamic memory allocation within the C Programming forums, part of the General Programming Boards category; Code: #include <cstdio> #include <cstdlib> #include <iomanip> using namespace std; const int MaxHolidays = 25; const int MaxDescLength = 40; ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    7

    Help with dynamic memory allocation

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    const int MaxHolidays = 25;
    const int MaxDescLength = 40;
    const char HOLIDAY_FILE[] = "holidays.txt";
    
    struct holidayStruct {
        int holidayId;
        char description[MaxDescLength+1];
        int month;
        int day;
        double cost;
    };
    
    holidayStruct *
    readHoliday(FILE* holidayfile)
    {
        int id;
        holidayStruct *holidayPtr;
        
        holidayfile = fopen ("holidayFile.txt", "r");
        if (holidayfile!=NULL) {
            holidayPtr = new holidayStruct;
            if (!holidayPtr) {
                printf("Critical Error: Failure to allocate memory in \n");
                printf("readHoliday() function.\n");
                printf("Contact your holiday management program team to resolve\n");
                printf("this problem."); 
                exit(1);
            } 
            holidayPtr->holidayId = id;
            
            fscanf(holidayfile, "%d %d %f" , holidayPtr->month, holidayPtr->day, holidayPtr->cost);
            //fclose(inFile);
            fgets(holidayPtr->description, MaxDescLength+1, holidayfile);
            return (holidayPtr);
        } else return NULL;
    }
    
    int
    main()
    {
        holidayStruct *items[MaxHolidays];
        
    
        int i;                // general loop counter
        int itemCount;       // current number of items
        FILE* inputFile;
    
        inputFile = fopen(HOLIDAY_FILE, "r");
        if (inputFile==NULL)
        {
             printf("Holiday file %s %s",(HOLIDAY_FILE) , " could not be opened."  );
                 
            getchar();
            exit(1);
        }
    
        //reading from the file
        itemCount = 0;
        while (itemCount < MaxHolidays
             &&(items[itemCount] = readHoliday(inputFile)))
            
            ++itemCount;
        printf("%d %s" ,itemCount, " holidays read in.\n");
    This compiles, but doesn't go through the while loop. The print statement is as follows:
    o holidays read in.
    here is a sample input file:
    20 5 10 10.50 Tony's Birthday
    11 2 10 50.00 Happy Day
    6 2 16 5.00 Washington's Birthday
    8 3 2 25.45 Skinned knee day
    9 12 10 20.01 Last day of school

    Please help me!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    remove
    holidayfile = fopen ("holidayFile.txt", "r");
    from the readHoliday function

    also fscanf awaits the pointers to variables, add & where needed
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    That looks like a cross between a C and a C++ program . . . .

    Also, closing a file once you're done with it via fclose() is a good idea.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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