Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Structures and Reading from File (http://www.programmingforums.org/showthread.php?t=14564)

ccfc1986 Nov 23rd, 2007 2:35 PM

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

Sane Nov 23rd, 2007 2:43 PM

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

Salem Nov 23rd, 2007 2:43 PM

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>

DaWei Nov 23rd, 2007 3:04 PM

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.

Sane Nov 23rd, 2007 3:07 PM

Re: Structures and Reading from File
 
Oh, overloading comparison operators? Interesting. Well that's what you get from a C guy.

DaWei Nov 23rd, 2007 3:18 PM

Re: Structures and Reading from File
 
We'll keep you around, anyway. ;)

ccfc1986 Nov 23rd, 2007 4:47 PM

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!

DaWei Nov 23rd, 2007 5:11 PM

Re: Structures and Reading from File
 
Quote:

while (personInfo.last_name != end_of_file);
What in the doodingeywawah is that?

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.

ccfc1986 Nov 23rd, 2007 5:17 PM

Re: Structures and Reading from File
 
Quote:

Originally Posted by DaWei (Post 137435)
What in the doodingeywawah is that?

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;
}

And it works just fine. minus the social security number is a mess - but im still working on that - i guess ill do it by myself without help. Thanks anywayas tho.

null_ptr0 Nov 23rd, 2007 9:26 PM

Re: Structures and Reading from File
 
try calling
strcmp(personInfo[count].last_name.data(), end_of_file.data())
and using the preprocessor statement
:

  1. #include <string.h>



All times are GMT -5. The time now is 3:35 AM.

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