Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 20th, 2005, 1:59 PM   #1
Dan
Newbie
 
Join Date: Sep 2005
Posts: 7
Rep Power: 0 Dan is on a distinguished road
File stream problems

My previous post didn't post for some reason, I'll try again

The code below does the following - A Class named My_class has a function that asks to enter names until 'n' is entered. The names are saved to a file. The file is opened again and all the names saved in the file are printed to the screen.

The problem is only the last name entered is printed to the screen when I use Dev c++, why is this? The program doesn't even run in Visual C++ How do I get all the names to print to screen and what am I doing wrong? Hope you can help


#include <conio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>

class My_class
{
private:
char name[20];

public:
void enternames();
void printnames();
};

void My_class::enternames()
{
cout<<"\nEnter a name : ";
gets(name);
}

void My_class::printnames()
{
cout<<"\nName = "<<name;
}

int main(void)
{
fstream namefile;

My_class X;

namefile.open("myfile.dat",ios::out);

do{
X.enternames();
namefile.write((char*)&X,sizeof(My_class));
cout<<"\nWould you like to enter another name? y/n ";
}while(getch()!='n');
namefile.close();

namefile.open("myfile.dat",ios::in);

while(namefile.read((char*)&X,sizeof(My_class)));
{
X.printnames();
}
namefile.close();

getch();
return 0;
}
Dan is offline   Reply With Quote
Old Sep 20th, 2005, 2:01 PM   #2
Dan
Newbie
 
Join Date: Sep 2005
Posts: 7
Rep Power: 0 Dan is on a distinguished road
I don't know what's up with the smiley faces in the code... sorry
Dan is offline   Reply With Quote
Old Sep 20th, 2005, 2:08 PM   #3
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quickly hit the 'edit' button under your post, and put code tags around your code. There is a code-tags icon at the top of your editor window. It will retain formatting (spaces/tabs) which makes your code MUCH easier to read. (you can use the PHP tags if you want color highlighting as well )

you can only edit your post 30 minutes, after which it will be locked.

What are the contents of the file? Were all the names concatenated, or is only the last name present?

* a wild guess, maybe you should use ios :: app?
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty

Last edited by stevengs; Sep 20th, 2005 at 2:31 PM.
stevengs is offline   Reply With Quote
Old Sep 20th, 2005, 2:09 PM   #4
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
You must use code tags. It's in the rules, no less.
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old Sep 20th, 2005, 2:33 PM   #5
Dan
Newbie
 
Join Date: Sep 2005
Posts: 7
Rep Power: 0 Dan is on a distinguished road
My previous post didn't post for some reason, I'll try again

The code below does the following - A Class named My_class has a function that asks to enter names until 'n' is entered. The names are saved to a file. The file is opened again and all the names saved in the file are printed to the screen.

The problem is only the last name entered is printed to the screen when I use Dev c++, why is this? The program doesn't even run in Visual C++ How do I get all the names to print to screen and what am I doing wrong? Hope you can help


#include <conio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>

class My_class
{
      private:
      char name[20];
      
      public:
      void enternames();
      void printnames();
};

void My_class::enternames()
{
     cout<<"\nEnter a name : ";
     gets(name);
}

void My_class::printnames()
{
     cout<<"\nName = "<<name;
}

int main(void)
{
    fstream namefile;
    
    My_class X;  
    
    namefile.open("myfile.dat",ios::out);
    
    do{
       X.enternames();
       namefile.write((char*)&X,sizeof(My_class));
       cout<<"\nWould you like to enter another name? y/n ";
       }while(getch()!='n');
    namefile.close();
    
    namefile.open("myfile.dat",ios::in);
    
    while(namefile.read((char*)&X,sizeof(My_class)));
    {
     X.printnames();
    }
    namefile.close();
    
    getch();
    return 0;
}

Sorry about the tags guys, hope this works... I'd just like to see all the names I entered during each "run" It doesnt matter what happens to the file after I close.....sorry, I'm not familiar with the correct terms.
Dan is offline   Reply With Quote
Old Sep 20th, 2005, 2:43 PM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
You may want to change OUT to APP... otherwise, you are overwriting your file.
namefile.open("myfile.dat",ios::out);


call this function to read the file after all of your names are entered:

void ReadFile (void)
{
ifile.open("myfile.dat");
string x;
 
while(ifile >> x)
	 {
		cout << x;
		cout << endl;
	 }
	 ifile.close();
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 20th, 2005, 3:25 PM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Disregard my code above...

Keep in mind that using OUT on your data file, will overwrite anything that's in it each time the file is opened for output. You will need to use APP to append data in cases like this.

Here's how I would have went about it... you will need to turn it into OOP, etc.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
 
using namespace std;
 
void WriteToFile (string data);
void ReadFile (void);
ofstream ofile;
ifstream ifile;
 
int main (void)
{
char name[256];
 
while (strcmp(name,"n") != 0)
{
cout << "Enter name: ";
cin.getline(name,200,'\n');
if (strlen(name) > 2)
WriteToFile (name);
}
 
 
ReadFile();
system("PAUSE");
 
return 0;
}
 
void WriteToFile (string data)
{
ofile.open("myfile.dat",ios::app);		//open a file
	ofile << data << endl;	//write to it
	ofile.close();				//close it
}
 
void ReadFile (void)
{
char ch;
ifile.open("myfile.dat");
 
while(!ifile.eof())
	 {
char temp[256];
		ifile.getline(temp,256);
		cout << temp << endl;
	 }
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 20th, 2005, 3:26 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
If you use ios::app, be sure and use ios::binary, too, or it won't work as you expect.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 20th, 2005, 7:46 PM   #9
Dan
Newbie
 
Join Date: Sep 2005
Posts: 7
Rep Power: 0 Dan is on a distinguished road
I'm stumped but first thanks to all of you for taking the time to reply.

Infinite recursion, a special thanks for writing out the code for me...your's worked like a charm and I'm going to save it.

Following DaWei and your example I tossed the ios::out and added ios::binary and ios::app. My program still doesn't print out all of the names, only the last one entered.

Sorry for being so insistant with getting my version to work but it's the format we're commenting with in class so I'd like to get it to work and understand why. Would it be possible to modify it only slightly and have it to work?

..here's the new code with app and binary....same result as before.

#include <conio.h>
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>

class My_class
{
      private:
      char name[20];
      
      public:
      void enternames();
      void printnames();
};

void My_class::enternames()
{
     cout<<"\nEnter a name : ";
     cin>>name;
}

void My_class::printnames()
{
     cout<<"\nName = "<<name;
     cout<<flush;
}

int main(void)
{
    fstream namefile;
    
    My_class X;  
    
    namefile.open("myfile.dat",ios::binary|ios::app);
    
    do{
       X.enternames();
       namefile.write((char*)&X,sizeof(My_class));
       cout<<"\nWould you like to enter another name? y/n ";
       }while(getch()!='n');
    namefile.close();
    
    namefile.open("myfile.dat",ios::binary|ios::in);
    
    namefile.seekg(0);
    while(namefile.read((char*)&X,sizeof(My_class)));
    {
     X.printnames();
    }
    namefile.close();
    
    getch();
    return 0;
}
Dan is offline   Reply With Quote
Old Sep 20th, 2005, 8:14 PM   #10
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
This is why you should always put some error checking in your code. If you'd checked namefile.good() after the first namefile.open(), you would have realized that the file wasn't opened for write properly at all. You really need to open it with:
namefile.open("myfile.dat",ios::binary|ios::app|ios::out);
if (!namefile.good()) {
   cerr << "Open failed" << endl;
   return -1;
}
Note the extra ios parameter I added. If you don't have it, your open() will fail.
With that said, you haven't handled reading it back correctly either. Here's how to really do it:
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;

class My_class
{
      private:
      char name[20];

      public:
      void enternames();
      void printnames();
};

void My_class::enternames()
{
     cout<<"\nEnter a name : ";
     cin>>name;
}

void My_class::printnames()
{
     cout<<"\nName = "<<name;
     cout<<flush;
}

int main(void)
{
    fstream namefile;

    My_class X;

    namefile.open("myfile.dat",ios::binary|ios::app|ios::out);
    if (!namefile.good()) {
      cerr << "Open failed" << endl;
      return -1;
    }

    do{
       X.enternames();
       namefile.write((char*)&X,sizeof(My_class));
       cin.ignore(1);
       cout<<"\nWould you like to enter another name? y/n ";
       }while(getchar()!='n');
    namefile.close();

    namefile.open("myfile.dat",ios::binary|ios::in);
    if (!namefile.good()) {
      cerr << "Open failed" << endl;
      return -1;
    }

    namefile.seekg(0);
    while(!namefile.eof())
    {
      namefile.read((char*)&X,sizeof(My_class));
     X.printnames();
    }
    namefile.close();

    getchar();
    return 0;
}
Scorpions4ever is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC