I/O stream files

This is a discussion on I/O stream files within the C Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #include <iomanip> using namespace std; const int MaxHolidays = 25; const int MaxDescLength = 40; ...

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

    I/O stream files

    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.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    holidayPtr = new holidayStruct;
    and all the cout <<
    just from looking at it quick

    I don't see the 2 things your asking about, the .ignore and .eof(), maybe I'm not looking hard enough

  3. #3
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    well you could use #define for those constants. you never really initialized id. you did holidayPtr->holidayId = id; but I think the compiler will complain. I would suggest flippin it to id=holidayPtr->holidayId; that should work. And (somebody correct me if I'm wrong) you need a & on holidayPtr->cost. If it's a string it doesn't need it, but if it's not....use &. Change all of this:
    Code:
     cerr << "Holiday file " << HOLIDAY_FILE
                 << " could not be opened." << endl;
            cin.get();
    And then just switch all your couts and you should be good!!! Hope that helps and if I'm wrong....it happens often....but I think I'm pretty right on.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    First, sorry for my oops in moving threads, I didn't read it closely enough on the first go-round. I left it with the C++ redirect to help garnish further interest.

    Quote Originally Posted by malooch
    Also what functions can I substitute for "holidayFile.ignore" and holidayFile.eof() to make it work in C?
    Even in C++ you generally don't want to use .eof(), or .ignore(), really. [And in C++ consider taking input with std::string.]

    Instead, choose to check for successful input (rather than lack of a particular failure), and be aware of issues with string input from streams. The FAQ has a few more comments on these.

    I again apologize for not spending much time with this, but could you post (or attach) a small example of the input file?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Washington
    Posts
    18
    Quote Originally Posted by malooch
    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.
    Is it me, or did anyone else notice that there is no corresponding call to "delete" in the C++ version, or "free" in the C version? (The C version still uses "new")


    :davis:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question: i/o data form .txt or .xls files ??
    By camelman in forum C++ Programming
    Replies: 21
    Last Post: 11-17-2007, 02:48 PM
  2. Question about file I/O for configuration files.
    By sup_stephen in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 11:57 PM
  3. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 08:35 AM
  4. i/o stream
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 12:55 AM
  5. Looping and I/O Stream
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2001, 10:20 AM

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