Thread: text file i/o

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    8

    text file i/o

    okay so idk why this isnt working but im having trouble reading from the text file. i know it looks rough im only showing the basic parts of what i really have to do for the project. can someone please help me?

    this is the text file:
    (id, lastname, firstname, position, age, salary)
    Code:
    11169 King, Leroy manager 60 45000.00
    65765 Guilfour, Theresa clerk 32 24000.00
    51432 Ieyoub, Kalil salesman 48 37000.00
    12768 Cooper, Scott salesman 50 20800.00
    19180 Newman, Hank manager 56 40000.00
    31216 Fields, Lori maintenance 40 18000.00
    32589 Andrews, Dana accountant 45 24000.00
    43786 Palmer, Tom stockman 48 19000
    48971 Baker, Kelly secretary 42 25000.00
    56342 Beard, John salesman 55 43000.00
    68754 Shelton, Bob manager 60 46000.00
    74753 Gaston, Chad stockman 60 19000.00
    85154 Adams, Harry salesman 55 41000.00
    71654 Bonvillion, Cheri secretary 40 23000.00
    69876 Grimes, Gary carpenter 80 25000.00

    it compiles fine but when i a.out it gives me this:
    Code:
    11169
    K@«ªª*
    i9¡ÿ
    32622
    2.31779e-310
    and nothing else.

    this is the codeps: ignore the "Binary" parameters....i have to make it binary later but i need to read it in correctly before i can convert to binary....also this is a very simplified version of my code the actual code is about 700 lines but i blocked it all as comments so i left most of the code out to make it more readable)
    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<cstdlib>
    using namespace std;
    
    struct PERSON
    {
        int EmployeeID;
        char LastName[12];
        char FirstName[8];
        char EmployeeTitle[12];
        int EmployeeAge;
        double EmployeeSalary;
    };
    
    //prototypes
    
     int FillData(PERSON person[MAX], ifstream & inFile, fstream &Binary);
    
    
    int main()
    {
       PERSON person[MAX];               //struct
       ifstream inFile;             //input file
    
       inFile.open("text.data");
       if(inFile.fail())
       {
           cout<<"\n\nError: cannot open text file"<<endl<<endl;
           exit(1);
       }
    
    
       //PrintWelcome();
    
       //Fill Data from File
       int ct = FillData(person, inFile, Binary);
    
      inFile.close();
    return 0;
    }
    //**************************************************************
    
    int FillData(PERSON person[MAX], ifstream & inFile, fstream & Binary)
    {
       int count=0;
    
       while(inFile >> person[count].EmployeeID)
       {
           cout<<person[count].EmployeeID<<endl;
    
           inFile>> person[count].LastName[12];
           cout<< person[count].LastName;
    
           inFile>> person[count].FirstName[8];
           cout<< person[count].FirstName<<endl;
    
           inFile>> person[count].EmployeeTitle[12];
           cout<< person[count].EmployeeTitle<<endl;
    
           inFile>> person[count].EmployeeAge;
           cout<<person[count].EmployeeAge<<endl;
    
           inFile>> person[count].EmployeeSalary;
           cout<<person[count].EmployeeSalary<<endl<<endl;
    
       count ++;
       }
       return count;
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    oops forgot this part in the post:
    after using namespace std;

    //global variables
    const int MAX = 30;

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    You aren't using the streaming operator properly. The ifstream object doesn't know that you want to read in 12 characters for the last name. It only sees that LastName is of type char and it reads one char in, so the rest of the string is garbage, but you got the first K. On the same note, it doesn't know that you want to read in 8 chars for FirstName and the file pointer is pointing at the 'i' in 'King' because it was only moved one position from when you tried to read LastName. Again, it only reads one char and then the rest is gibberish.

    Not related, but if you are not passing in data to be used by the function, then there is no need for the PERSON parameter in the FillData function. You are not passing by reference, so the data will not exist when the function exits, and it doesn't provide any info to the function. You should just declare a PERSON object in your function if that is the only place it will exist.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > inFile>> person[count].LastName[12];
    Here, you're just writing one character, which is outside the bounds of the array.

    The way to read a string is
    Code:
    inFile>> person[count].LastName;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why not to use std::string instead of C-string?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding word frequency in a text file.
    By aeolusaether in forum C Programming
    Replies: 15
    Last Post: 04-04-2010, 09:59 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM