View Full Version : file handling
3li773
08-21-2003, 08:44 PM
Hey
I am making a program that is similar to an inventory program
and i was wondering if someone could help me or give a link on how i could, open a file to write in, and save that file as the part number, and put the amount of items inside the file for future viewing
thanks for all the help
DNAunion2000
08-21-2003, 09:04 PM
/*DNAunion*/ Here's something I posted in another thread here that should get you started.
struct Person
{
char Name[40];
int Age;
char Gender;
char County[30];
};
...
Person people[20];
strcpy(people[0].Name, "Robert Jones");
people[0].Age = 25;
people[0].Gender = 'M';
strcpy(people[0].County, "Macon");
...
strcpy(people[19].Name, "Lauren Mathers");
people[19].Age = 15;
people[19].Gender = 'F';
strcpy(people[19].County, "Fair");
ofstream outfile("SomePeople.dat", ios::binary);
for (int Lcv = 0; Lcv < 20; ++Lcv)
{
outfile.write(reinterpret_cast<char*>people[Lcv], sizeof(Person));
}
...
Person peeps[20];
ifstream infile("SomePeople.dat", ios::binary);
for (int Lcv = 0; Lcv < 20; ++Lcv)
{
infile.read(reinterpret_cast<char*>peeps[Lcv], sizeof(Person));
}
3li773
08-21-2003, 09:10 PM
well when i was compiling it, it said:
ISO C++ forbids
sans-hubris
08-22-2003, 03:15 AM
forbids what?
3li773
08-22-2003, 10:17 AM
strcpy(people[0].Name, "Robert Jones");
stuka
08-22-2003, 11:03 AM
<rant>WHEN will people stop using char arrays in C++?????</rant> Seriously, the standard library provides std::string, which is easier, more robust, etc. It does make the file I/O a little more tricky (since std::string isn't fixed size), but I'd rather have complexity in ONE function rather than throughout my code!
DNAunion2000
08-22-2003, 02:21 PM
/*DNAunion*/ Stuka, I originally posted the code in the C thread (where I made the C++ code mostly compatible with C).
DNAunion2000
08-22-2003, 02:33 PM
3li773: well when i was compiling it, it said:
ISO C++ forbids [strcpy(people[0].Name, "Robert Jones");]
/*DNAunion*/ I just compiled the following code - clean compile and proper output.
#include <shlwapi.h>
#include <iostream>
using namespace std;
struct Person
{
char Name[40];
int Age;
char Gender;
char County[30];
};
int main()
{
Person people[20];
strcpy(people[0].Name, "Robert Jones");
people[0].Age = 25;
people[0].Gender = 'M';
strcpy(people[0].County, "Macon");
cout << people[0].Name << endl;
cout << people[0].Age << endl;
cout << people[0].Gender << endl;
cout << people[0].County << endl;
return 0;
}
sans-hubris
08-22-2003, 02:51 PM
Originally posted by Stuka
<rant>WHEN will people stop using char arrays in C++?????</rant> Seriously, the standard library provides std::string, which is easier, more robust, etc. It does make the file I/O a little more tricky (since std::string isn't fixed size), but I'd rather have complexity in ONE function rather than throughout my code! Personally, I think that std::string makes even file IO pretty easy.
stuka
08-22-2003, 03:00 PM
sans-hubris: well, you can't use handy sizeof() tricks, though with code I saw that's being considered for inclusion in the Boost library (dynamic_any), it would become trivial.
3li773
08-23-2003, 05:54 PM
well since we're all talking here i would like to ask one more thing
i wanted to know how to put a variable in a system command like this.
int main
{
char cmd;
cin>>cmd;
system(cmd);
return 0;
}
but that does not work how could i make the variable determin the outcome
stuka
08-25-2003, 10:21 AM
Your code doesn't work as posted because system() expects a string (char*), not a char.
3li773
08-25-2003, 11:41 PM
Hey thanks alot
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.