Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,228 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,959 people online right now. Registration is fast and FREE... Join Now!




file to function

 
Reply to this topicStart new topic

file to function, file not working in function

gamodg
14 Mar, 2007 - 11:23 AM
Post #1

New D.I.C Head
*

Joined: 14 Mar, 2007
Posts: 10


My Contributions
CODE

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
class router
{
    char str[20],*p;
    public:void pkt_type(fstream &file) :angry:
           {
         file.seekg(0);
         file.getline(str,20);[b][color=#FF0000]//str is NULL why [/b][/color]
         p=strchr(str,'0');
            cout<<endl<<endl<<"\t" <<str<<endl;
            if(p)
             {
              cout<<endl<<" Data Packet Found ";
             }
             else
             {
              cout<<endl<<" Marking Packet Found ";
             }
        }
  };
void main()
{
fstream file;
file.open("maza",ios::in|ios::out|ios::trunc);
char src[20],des[20],data[100],temp[100];
clrscr();
cout<<" Enter the source address ";
cin.getline(src,20);
cout<<"\n Enter the destination address ";
cin.getline(des,20);
cout<<"\n Enter the data ";
cin.getline(data,100);
file<<" DF:0\n";
file<<" Uflag :0\n";
file<<" Source :"<<src<<"\n";
file<<" Destination :"<<des<<"\n";
file<<" Data :"<<data;
file.seekg(0);
cout<<endl<<endl;
    while(!file.eof())
        {
           file.getline(temp,100);
           cout<<temp<<endl;
        }

router r;
r.pkt_type(file);
file.close();
getch();
   }

file variable received in router class function pkt_type shows no value i tried to print the contents of file using while like in main but no result
User is offlineProfile CardPM
+Quote Post

horace
RE: File To Function
14 Mar, 2007 - 12:01 PM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
in your loop in main() you read the file until file.eof() is true - before you can read again (in your function pkt_type()) you must call file.clear() to clear the condition, i.e.
CODE

    while(!file.eof())
        {
           file.getline(temp,100);
           cout<<temp<<endl;
        }
file.clear();     // clear eof() condition
router r;
r.pkt_type(file);

otherwise when you attempt to read the file in function pkt_type() eof() is still true
User is offlineProfile CardPM
+Quote Post

gamodg
RE: File To Function
17 Mar, 2007 - 09:52 AM
Post #3

New D.I.C Head
*

Joined: 14 Mar, 2007
Posts: 10


My Contributions
thanks a lot for your help its working thnx again biggrin.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 02:51PM

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