Mar 10, 2013 at 1:49am UTC
I'm trying to put file data into members of a class. Remember to type in the file name you want to open. Cool feature right? I just had Dbase.txt so I chose that.
EDIT: Fixed stuff in the .txt. Now I need to figure out why it only does 1 set and then ends.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
class INFO
{
public :
char FirstName[256];
char LastName[256];
char Job[256];
char Gender; //M or F
char Hash[256];
double Income;
int Age;
};
int main(int nNumberofArgs,char * pszArgs[])
{
INFO person[256];
char filename[256];
cout << "Enter the name of file you want to open:" ;
cin.getline(filename,256);
for (int s = 0;s != '\n' ;s++)
{
if (filename[s] == NULL)
{
filename[s] = '.' ;
filename[s + 1] = 't' ;
filename[s + 2] = 'x' ;
filename[s + 3] = 't' ;
filename[s + 4] = NULL;
break ;
}
}
cout << "Opening and reading contents of '" << filename << "'" << endl;
ifstream File(filename);
int I = 0;
for (int i = 1;;i++)
{
char object[256];
char gender;
int age;
double income;
File.getline(object,256);
for (int a = 0;a < 256;a++)
{
person[i].FirstName[a] = object[a];
}
File.getline(object,256);
for (int a2 = 0;a2 < 256;a2++)
{
person[i].LastName[a2] = object[a2];
}
File.getline(object,256);
for (int a3 = 0;a3 < 256;a3++)
{
person[i].Job[a3] = object[a3];
}
File >> gender;
person[i].Gender = gender;
File >> income;
person[i].Income = income;
File >> age;
person[i].Age = age;
File.getline(object,256);
for (int a4 = 0;a4 < 256;a4++)
{
person[i].Hash[a4] = object[a4]; //A line in the .txt file which is "-----------------"
}
if (File.fail())
{
cout << "File read end..." << endl << endl;
break ;
}
I++;
cout << "Data Sets Complete: " << I << endl;
}
for (int i2 = 1;i2 < I + 1;i2++)
{
cout << "First name:\t" << person[i2].FirstName << endl;
cout << "Last name:\t" << person[i2].LastName << endl;
cout << "Job:\t\t" << person[i2].Job << endl;
cout << "Gender:\t\t" << person[i2].Gender << endl;
cout << "Income:\t\t" << person[i2].Income << endl;
cout << "Age:\t\t" << person[i2].Age << endl;
cout << "----------------------------------" << endl;
}
system("PAUSE" );
return 0;
}
Dbase.txt:
Bob
Guy
Programmer
M
9999.99
40
------------------
Little
Guy
Little Brother
M
0.0
3
------------------
Last edited on Mar 10, 2013 at 2:09am UTC
Mar 10, 2013 at 10:17pm UTC
Bump... :( ....nothing.... Hello? ....
Mar 10, 2013 at 10:22pm UTC
Um, then how would I transfer from file to string?
Mar 10, 2013 at 10:34pm UTC
But can you tell me what the problem is in my program though?
Mar 10, 2013 at 10:36pm UTC
wow... ive never seen code go out the box like that lol. dont know why but that makes me happy. anyways, no. i tried reading through it but i cant read code well when its a mish mash of begginner c with beginner c++
Mar 10, 2013 at 10:43pm UTC
Um, it's not a mish mash, there are worse programs than that and I've seen you read them and everyone's style of writing c++ is different.
Mar 10, 2013 at 10:46pm UTC
1) it is a mish mash.
2) didnt say it was bad i said its not a style im good at reading ie i can read well defined c/c++ code.
3) not gonna lie i feel seriously stalked
Mar 10, 2013 at 10:51pm UTC
Stalked? we've only crossed paths a few times.
And i'm using the format which is the standard, ANSI. So it is not a mish mash because i'm not using whatever organization you use.
Mar 10, 2013 at 10:54pm UTC
yes its a mish mash. i understand its ANSI. that doesnt mean its not illegel. and im not saying its bad im saying its a style i cant read
Mar 10, 2013 at 11:01pm UTC
Is it because of the "{}"?
Mar 10, 2013 at 11:02pm UTC
no it is the fact that you are using c strings with c++ classes. i could care less for ur style. its not how you write the code its the code you write
Mar 10, 2013 at 11:04pm UTC
OMG I FIGURED IT OUT, it was stupid solution though, I had to put the
file.fail()
after I incremented "I"