Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,148 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,059 people online right now. Registration is fast and FREE... Join Now!




Reading from a file

 
Reply to this topicStart new topic

Reading from a file

kp362001
12 Apr, 2007 - 05:44 AM
Post #1

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 31


My Contributions
could someone take a look at this and tell me whats missing it doesnt work correctly

CODE

#include <iostream>
#include <fstream>

using namespace std;

struct Terminal
{
    char TermType[5];
    char Building[3];
    int Rate;
    char Access;
    int mon, day, year;
}CompTerm;


int main(int argc, char* argv[])
{
    int TermNum;
    int ans = 1;

    fstream FileTerminal;

    while (1)
    {
        cout << "\n-----------------------------------------------------------------------\n";
        cout << "\n\t\tMain Menu";
        cout << "\n\n1) Enter new terminal Information?(new data will be added to existing data file)";
        cout << "\n2) Display information of specific terminal number? ";
        cout << "\n3) Display all stored information? ";
        cout << "\n4) Exit.";
        cout << "\n\nyour choice : ";
        cin >> ans;
        cout << "\n-----------------------------------------------------------------------\n";
        
        switch(ans)
        {
        case 1 : FileTerminal.open("c:/terminal.txt",ios::out|ios::binary|ios::app);
                if ( !FileTerminal )
                {
                    cout << "Error...cannot open file for writing...";
                    break;
                }

                cout << "\nTerminal Type : "; cin >> CompTerm.TermType;
                cout << "\nBuilding : "; cin >> CompTerm.Building;
                cout << "\nTransmission Rate : "; cin >> CompTerm.Rate;
                cout << "\nAccess Code : "; cin >> CompTerm.Access;
                cout << "\nDate of last acsess : ";
                cout << "\n\tmonth : ";cin >> CompTerm.mon;    
                cout << "\n\tday : "; cin >> CompTerm.day;
                cout << "\n\tyear : "; cin >> CompTerm.year;

                FileTerminal.write(reinterpret_cast<char *>(&CompTerm), sizeof(CompTerm));
                
                cout << "\n-----------------------------------------------------------------------\n";


                FileTerminal.close();

                break;

        case 2 : FileTerminal.open("c:/terminal.txt",ios::binary|ios::in);
                
                
                cout << "\nEnter the terminal number : ";
                cin >> TermNum;

                if ( TermNum < 1 || TermNum > 10)
                {
                    cout << "...error... wrong terminal number...";
                    break;
                }

                FileTerminal.seekg((TermNum-1)*sizeof(CompTerm));

                if(!FileTerminal.good( ) )
                {
                    if (!FileTerminal.eof( ))
                    {
                        cout << "\nError reading file.";
                        break;
                    }
                }

                FileTerminal.read(reinterpret_cast<char *>(&CompTerm), sizeof(CompTerm));
                cout <<"\nInformation for Terminal No "<< TermNum << "\n";
                cout << "\nTerminal Type : " << CompTerm.TermType
                     << "\nBuilding : " << CompTerm.Building
                     << "\nTransmission Rate : " << CompTerm.Rate
                     << "\nAccess Code : " << CompTerm.Access
                     << "\nDate of Last access (mm/dd/yyyy) : " << CompTerm.mon << "/" << CompTerm.day << "/" << CompTerm.year << "\n\n";
                    
                cout << "\n-----------------------------------------------------------------------\n";

                FileTerminal.close();
                system("Pause");
                break;

        case 3 : FileTerminal.open("c:/terminal.txt",ios::binary|ios::in);
                
                
                TermNum = 1;
                cout <<"\nTermNo Type TransRate Building AccessCode Date(mm/dd/yyyy)\n\n";
                while( TermNum <= 10)
                {
                    if(!FileTerminal.good( ) )
                    {
                        if (!FileTerminal.eof( ))
                        {
                            cout << "\nError reading file.";
                            break;
                        }
                        
                    }
                    FileTerminal.read(reinterpret_cast<char *>(&CompTerm), sizeof(CompTerm));
                    cout << TermNum << "\t" << CompTerm.TermType << "\t"<< CompTerm.Building << "\t"
                         << CompTerm.Rate << "\t" << CompTerm.Access << "\t"
                         << CompTerm.mon << "/" << CompTerm.day << "/" << CompTerm.year << "\n";

                    TermNum++;
                }

                cout << "\n-----------------------------------------------------------------------\n";

                FileTerminal.close();
                system("Pause");

                break;

        case 4 : exit(1);

        default : cout << "\n...wrong choice...choose again..."; continue;
        }
    }


    return 0;
}



file
terminal.txt
C109 L1 155 R 3 1 2006
B208 L2 145 W 2 1 2006
A307 L3 135 A 1 1 2007
C406 L4 125 R 3 1 2007
B505 L5 115 W 2 2 2007
A604 L6 105 A 1 1 2007
C703 L7 125 R 12 1 2006
B802 L8 135 W 11 1 2006
A901 L9 150 A 10 1 2006
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Reading From A File
12 Apr, 2007 - 06:17 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
QUOTE
...tell me whats missing...
Well the code tags were missing, but I added them for you... this time... smile.gif

Is there a spacific problem you are having with it?
User is offlineProfile CardPM
+Quote Post

kp362001
RE: Reading From A File
12 Apr, 2007 - 06:29 AM
Post #3

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 31


My Contributions
QUOTE(NickDMax @ 12 Apr, 2007 - 07:17 AM) *

QUOTE
...tell me whats missing...
Well the code tags were missing, but I added them for you... this time... smile.gif

Is there a spacific problem you are having with it?




I just wasn't getting the right informaton no errors or anything , thanks
User is offlineProfile CardPM
+Quote Post

kp362001
RE: Reading From A File
12 Apr, 2007 - 06:36 AM
Post #4

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 31


My Contributions
QUOTE(NickDMax @ 12 Apr, 2007 - 07:17 AM) *

QUOTE
...tell me whats missing...
Well the code tags were missing, but I added them for you... this time... smile.gif

Is there a spacific problem you are having with it?



yod added them ? where
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Reading From A File
12 Apr, 2007 - 07:37 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
QUOTE
yod added them ? where

Ah, see why we are supposed to help, and not DO.

By adding tags [Code ] your code here [/Code ] (without the extra space at the end) arround your code it makes it much easier for us to read. And therefor easier for us to help you.

As for you code, it helps to know what the program is doing wrong when we go looking for some kind of logic error. Your code compiles fine. So there must be a logic error, but without knowing what you MEANT to do...

I suppose I could figure it out, but who has the time...

Help me, help you.
User is offlineProfile CardPM
+Quote Post

kp362001
RE: Reading From A File
12 Apr, 2007 - 08:00 AM
Post #6

New D.I.C Head
*

Joined: 28 Jan, 2007
Posts: 31


My Contributions
[quote name='NickDMax' date='12 Apr, 2007 - 08:37 AM' post='216397']
[quote]yod added them ? where[/quote]
Ah, see why we are supposed to help, and not DO.

By adding tags [Code ] your code here [/Code ] (without the extra space at the end) arround your code it makes it much easier for us to read. And therefor easier for us to help you.

As for you code, it helps to know what the program is doing wrong when we go looking for some kind of logic error. Your code compiles fine. So there must be a logic error, but without knowing what you MEANT to do...

I suppose I could figure it out, but who has the time...

Help me, help you.


what ever I'll look else where for help you people think you help people here all I've gotten is frustrated "maybe I'll tell you and maybe I won't" give me a break . YOUR ALL SO HIGH AND MIGHTY. Kick me out of your little group I don't really care



User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Reading From A File
12 Apr, 2007 - 08:37 AM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
owch... I am sorry that my post offended you. I didn't mean it to. If you are unhappy with the help here I am sorry, you can always post elsewhere.

I only meant that it is hard to help if I don't know what is wrong. "my program does not work right" is not very helpful. I am more than willing to help, but I am a very busy guy and really didn't want to go though all the trouble of editing your program to work on my system, creating the files, running it, and guessing what it is supposed to do, and not supposed to do.

All I was asking for is a discription of the problem.

please remeber that we are not getting paid to help you. Make it easy for us.


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:57PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month