Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 23rd, 2007, 1:35 PM   #1
ccfc1986
Newbie
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0 ccfc1986 is on a distinguished road
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
ccfc1986 is offline   Reply With Quote
Old Nov 23rd, 2007, 1:43 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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
Sane is offline   Reply With Quote
Old Nov 23rd, 2007, 1:43 PM   #3
Salem
Programmer
 
Join Date: Nov 2007
Posts: 33
Rep Power: 0 Salem is on a distinguished road
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.
Salem is offline   Reply With Quote
Old Nov 23rd, 2007, 2:04 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 23rd, 2007, 2:07 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Structures and Reading from File

Oh, overloading comparison operators? Interesting. Well that's what you get from a C guy.
Sane is offline   Reply With Quote
Old Nov 23rd, 2007, 2:18 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 23rd, 2007, 3:47 PM   #7
ccfc1986
Newbie
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0 ccfc1986 is on a distinguished road
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!
ccfc1986 is offline   Reply With Quote
Old Nov 23rd, 2007, 4:11 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
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
DaWei is offline   Reply With Quote
Old Nov 23rd, 2007, 4:17 PM   #9
ccfc1986
Newbie
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0 ccfc1986 is on a distinguished road
Re: Structures and Reading from File

Quote:
Originally Posted by DaWei View Post
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.
ccfc1986 is offline   Reply With Quote
Old Nov 23rd, 2007, 8:26 PM   #10
null_ptr0
12 years old
 
Join Date: Nov 2007
Posts: 93
Rep Power: 1 null_ptr0 is on a distinguished road
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)
  1. #include <string.h>
null_ptr0 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:48 AM.

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