Code:
#include <iostream>
#include <fstream>
#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(istream &holidayFile)
{
int id;
holidayStruct *holidayPtr;
holidayFile >> id;
if (!holidayFile.eof()) {
holidayPtr = new holidayStruct;
if (!holidayPtr) {
cerr << "Critical Error: Failure to allocate memory in "
<< "readHoliday() function.\n"
<< "Contact your holiday management program team to resolve\n"
<< "this problem." << endl;
exit(1);
}
holidayPtr->holidayId = id;
holidayFile >> holidayPtr->month >> holidayPtr->day >> holidayPtr->cost;
holidayFile.ignore();
holidayFile.getline(holidayPtr->description, MaxDescLength+1);
return (holidayPtr);
} else return NULL;
}
int
holidays()
{
holidayStruct *items[MaxHolidays];
holidayStruct *itemPtr = NULL;
int i; // general loop counter
int itemCount; // current number of items
ifstream inputFile;
inputFile.open(HOLIDAY_FILE, ios::in);
if (inputFile.fail())
{
cerr << "Holiday file " << HOLIDAY_FILE
<< " could not be opened." << endl;
cin.get();
exit(1);
}
//reading from the file
itemCount = 0;
while (itemCount < MaxHolidays
&& (items[itemCount] = readHoliday(inputFile)) )
++itemCount;
cout << itemCount << " holidays read in.\n";
cout << setiosflags(ios::fixed);
cout.precision(2);
if (itemCount)
cout << setw(10) << "id" << setw(10) << "Month"
<< setw(10) << "Day" << setw(12) << "Cost"
<< " Description\n\n";
for(i=0; i<itemCount; i++)
cout << setw(10) << items[i]->holidayId
<< setw(10) << items[i]->month
<< setw(10) << items[i]->day
<< setw(12) << items[i]->cost
<< " " << items[i]->description << endl;
return 0;
}
I'm trying to convert this to C language. Here is what I've done:
Code:
#include <cstdio>
#include <cstdlib>
#include <iomanip>
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 *inFile)
{
int id;
holidayStruct *holidayPtr;
inFile = fopen ("holidayFile.txt", "r");
if (inFile!=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\n");
exit(1);
}
holidayPtr->holidayId = id;
fscanf(inFile, "%d %d %f" , holidayPtr->month, holidayPtr->day, holidayPtr->cost);
fclose(inFile);
fgets(holidayPtr->description, MaxDescLength+1, inFile);
return (holidayPtr);
} else return NULL;
}
int
holidays()
{
holidayStruct *items[MaxHolidays];
holidayStruct *itemPtr = NULL;
int i; // general loop counter
int itemCount; // current number of items
FILE *inputFile;
inputFile = fopen(HOLIDAY_FILE, "r");
if (inputFile==NULL)
{
cerr << "Holiday file " << HOLIDAY_FILE
<< " could not be opened." << endl;
cin.get();
exit(1);
}
//reading from the file
itemCount = 0;
while (itemCount < MaxHolidays
&& (items[itemCount] = readHoliday(inputFile)) )
++itemCount;
cout << itemCount << " holidays read in.\n";
cout << setiosflags(ios::fixed);
cout.precision(2);
if (itemCount)
cout << setw(10) << "id" << setw(10) << "Month"
<< setw(10) << "Day" << setw(12) << "Cost"
<< " Description\n\n";
for(i=0; i<itemCount; i++)
cout << setw(10) << items[i]->holidayId
<< setw(10) << items[i]->month
<< setw(10) << items[i]->day
<< setw(12) << items[i]->cost
<< " " << items[i]->description << endl;
return 0;
}
Would you guys take a look at this and let me know if something is wrong??
Also what functions can I substitute for "holidayFile.ignore" and holidayFile.eof() to make it work in C?
I really appreciate your help.