Thread: Help with Structure and I/O

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    54

    Help with Structure and I/O

    I'm trying to write a program that can read in customer info from a text file.

    The text file is formatted as follows:
    Last_name, First_name
    Transaction#
    Address

    Each customer should take one structure, and I'd like to be able to search by either name or transaction number.

    I'm not really sure how to read in the data and store it into the structures. I've been reading through a few books for the past couple of days and each one seems to skip around and use different syntax, and trying to search online seems that everyone wants to write from their struct to a file, not from the file to a struct. I understand that once you open the file you can handle the data similarly to like using cin, but I guess I'm just not too comfortable with structures to know what I'm doing.

    Here's some basic things I've started with, can you give me a little help to get the ball rolling?
    Code:
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        
        struct info{
              char name[30];
              int ID;
              string address[50];
        };
        info person[10]; //starting with 10 people to just see if it works
         
        ifstream in("custo.txt"); // Open file custo.txt
        if (not in)
           perror("custo.txt");
        else
        {
            
            for (int i = 0; i < 10; i++)
            {
                         //read in name, id, address and store in that customers struct
    
            }
           
        in.close();    
        }
            
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    19
    Start by reading each line of the file into a string variable. From that variable, it's up to you to decide how you want to split it up, though it really depends what you've covered in class. I'd probably go with stringstream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 12:40 PM
  2. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  3. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM