![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Posts: 4
Rep Power: 0
![]() |
Structures and Reading from File
Hey folks. I have worked on an assignment for class and i would like a little help. As always i have shown work to try to get the thing working - but as always i have come up short. So here i am...
The idea of my code is to read from a text file that contains the following information: last name (string up to 39 characters, plus room for null) first name (string up to 39 characters, plus room for null) SSN (string up to 9 chars, plus room for null) salaray (a double) years employed (an integar) I need to read the following - and store them into a structure. The exact wording is as follows: This program will read input, fill in a structure, and write the structure to disk - repeatedly until there is no more input. ....It will then read the first set of lines representing one student, build the struct, and then write it to disk My question occures involving how to set up the structre per say. I know how its done - but should i declare an array of the structure? Like: Employee personInfo[20]; And then do: personInfo[count].lastName; so then it would be something like this for read file: inFile >> personInfo[count].lastName; ??? Here is my code as of now - notice i have not set it to be written to disk - im unsure how you go about writing a structure to disk - it is not in any notes and i cannot find definite information while searching through forums and tutorials. Any helps on this would also be nice. #include <iostream>
#include <fstream>
using namespace std;
//Structure
struct Employee
{
string last_name;
string first_name;
string ss_num;
double salary;
int years_employed;
};
//Function Prototypes
//Main function
int main()
{
Employee personInfo[20];
string end_of_file = "eof";
int count = 0;
ifstream inFile;
inFile.open("empinfo.txt");
if (inFile.fail())
{
cout << "Unable to open empinfo.txt file\n";
exit(1);
}
inFile >> personInfo[count].last_name;
while (strcmp(personInfo[count].last_name, end_of_file) != 0)
{
inFile >> personInfo[count].first_name;
inFile >> personInfo[count].salary;
inFile >> personInfo[count].ss_num;
inFile >> personInfo[count].years_employed;
count++;
inFile >> personInfo[count].last_name;
}
inFile.close();
system("PAUSE");
return 0;
}(error message for the my strcmp line in bold above) 47 C:\Documents and Settings\Tony\My Documents\Fall 2007\CSCI 240\Assign11\assign11a.cpp no matching function for call to `strcmp(std::string&, std::string&)' I would like help the mainly the bolded area or anything else you guys notice along with a general guideline how i could write out the structures to file so that they can be read by a second program and printed out later. Thanks |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
Re: Structures and Reading from File
Since strcmp is a C function, you need to
include <string.h>. But perhaps you should include <string> instead, and use the C++ equivilent, compare().Read here |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Nov 2007
Posts: 33
Rep Power: 0
![]() |
Re: Structures and Reading from File
std::string overloads a lot of operators, so you can do
while ( personInfo[count].last_name != end_of_file )You also need to have #include <string>
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Structures and Reading from File
To explain your error message,
strcmp(std::string&, std::string&) is not a form which strcmp supports. Did you even look at the prototypes for strcmp?As suggested above, the string class overloads the "==" operator for comparison purposes. No need for a "compare" method. If you wanted to use strcmp, you'd have to play by its rules and pass C strings. Those can be obtained from a string-class string with the .c_str () method.
__________________
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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
Re: Structures and Reading from File
Oh, overloading comparison operators? Interesting. Well that's what you get from a C guy.
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Structures and Reading from File
We'll keep you around, anyway.
![]()
__________________
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 |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Nov 2007
Posts: 4
Rep Power: 0
![]() |
Re: Structures and Reading from File
Thank you for the replies folks. I have used your suggestions to great use and have gotten a lot further than i thought would be possible! My new problem arises because for whatever reason i need to figure out how to write the structure to a file called employees. I have done this using the following:
ifstream inFile;
ofstream outFile;
inFile.open("empinfo.txt");
outFile.open("employees");
if (inFile.fail())
{
cout << "Unable to open empinfo.txt file\n";
exit(1);
}
//Start read of data from file
inFile >> personInfo.last_name;
do
{
inFile >> personInfo.first_name;
inFile >> personInfo.salary;
inFile >> personInfo.ss_num;
inFile >> personInfo.years_employed;
// Writes out to employees file
outFile << "\n" << personInfo.last_name << " ";
outFile << personInfo.first_name << endl;
outFile << personInfo.salary << endl;
outFile << personInfo.ss_num << endl;
outFile << personInfo.years_employed << endl;
//Restart over from next user in file
inFile >> personInfo.last_name;
}
while (personInfo.last_name != end_of_file);Is this the way to write out structures to file correctly? Because the next thing i have to do it write a program that will read from this file of structs and store them into arrays of structs. My problem is ... in doing this... why would you because the first assignment is obsolete if you do this - you could just read from the orginal text file and use a count to make the members of the structure increase such as : personInfo.first_name[count]; etc etc count++; So is there a direct way to write out the entire struct to a file... like: outFile << personInfo; I tried this and got errors galore before. Thanks folks! |
|
|
|
|
|
#8 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Structures and Reading from File
Quote:
There is this about structures: they made be padded in different ways with different compilers or systems. If you're worried about portability, write out the members individually. If you aren't, use the file write function to write out sizeof struct bytes. In any event, you need to plan your file so you know how to read it back in. Sit down with documentation on file transfer methods. Learn how each parses the input. Learn how they indicate failure. Learn how they indicate end-of-file. Design your program from a base of knowledge rather than just sitting down at the keyboard and banging out trash based on guesses.
__________________
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: Nov 2007
Posts: 4
Rep Power: 0
![]() |
Re: Structures and Reading from File
This line takes the end of file (there is a line in the file being read that says just 'eof'). I set a string to be just that - and then used this line to check weather or not the last.name is equal to it, if it is, it will not use it and will exit the loop. So it it not trash.
Thanks for your help tho. Much appreciated. I changed my code to be this now anyways: /*****************************************************************************
CSCI 240 - Assignment 11a - Fall 2007
Programmer: Anthony Hogan
Z-ID: z109079
Section: 4
Date Due: 11/28/07
Purpose: Creates a struct by reading from file and then writes struct to disk
for future use by program aab.
******************************************************************************/
#include <iostream>
#include <fstream>
#include <ostream>
#include <string>
using namespace std;
//Structure
struct Employee
{
char last_name[39];
char first_name[39];
char ss_num[9];
double salary;
int years_employed;
};
//Function Prototypes
//Main function
int main()
{
Employee personInfo;
string end_of_file = "eof";
ifstream inFile;
ofstream outFile;
inFile.open("empinfo.txt");
outFile.open("employees");
if (inFile.fail())
{
cout << "Unable to open empinfo.txt file\n";
exit(1);
}
//Start read of data from file
inFile >> personInfo.last_name;
do
{
inFile >> personInfo.first_name;
inFile >> personInfo.salary;
inFile >> personInfo.ss_num;
inFile >> personInfo.years_employed;
// Writes out to employees file
outFile << "\n" << personInfo.last_name << " ";
outFile << personInfo.first_name << endl;
outFile << personInfo.salary << endl;
outFile << personInfo.ss_num << endl;
outFile << personInfo.years_employed << endl;
//Restart over from next user in file
inFile >> personInfo.last_name;
}
while (personInfo.last_name != end_of_file);
inFile.close();
outFile.close();
system("PAUSE");
return 0;
} |
|
|
|
|
|
#10 |
|
12 years old
Join Date: Nov 2007
Posts: 93
Rep Power: 1
![]() |
Re: Structures and Reading from File
try calling
strcmp(personInfo[count].last_name.data(), end_of_file.data()) and using the preprocessor statement c++ Syntax (Toggle Plain Text)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Changing Array structures to Linked Lists? | kalulu | C | 4 | Nov 29th, 2005 6:33 AM |
| C files and structures | Princeck | C++ | 2 | Feb 24th, 2005 2:09 PM |