![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 7
Rep Power: 0
![]() |
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; } |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Sep 2005
Posts: 7
Rep Power: 0
![]() |
I don't know what's up with the smiley faces in the code... sorry
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
Professional Programmer
|
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;} |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Sep 2005
Posts: 7
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Sep 2005
Posts: 7
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Jun 2005
Posts: 86
Rep Power: 4
![]() |
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;
}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;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|