Thread: .read function

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    .read function

    hi i need some help with this program, i need to have it read a file and have the program output whatever is in the file
    this is whats in the file that the program is reading from

    john 1234567
    mary ann 6851230
    peter 1112222


    need to have the output be like this

    john 1234567
    john
    1234567

    mary ann 6851230
    mary ann
    6851230

    peter 1112222
    peter
    1112222


    this is what i have right now..

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <fstream.h>
    
    
    struct rec
    { 
    	char name[10];
    	char phone[7];
    	char cr_lf[2];
    };
    
    
    void main()
    
    {
    	rec record;
    	ifstream infile;
    
    
    	infile.open("6afile.txt",ios::out);
    	infile.read((char *) &record, sizeof(record));
    
    	cout << record.name<< " " << record.phone << endl;
    	infile.close();
    
    
    }
    all it does is output a line of werid symbols

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Is binary file I/O necessary? It seems much more logical with text mode reading into std::strings.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    struct rec
    { 
    	string name;
    	string phone;
    };
    
    
    istream& operator>>(istream& i, rec& record)
    {
    	i	>> record.name 
    		>> record.phone;
    
    	return i;
    }
    ostream& operator<<(ostream& o, rec& record)
    {
    	o	<< record.name << " " << record.phone 
    		<< "\n" << record.name 
    		<< "\n" << record.phone << "\n";
    
    	return o;
    }
    
    
    int main()
    
    {
    	rec record1,
    		record2,
    		record3;
    	ifstream infile;
    
    
    	infile.open("C:\\a.txt", ios::in);
    
    	infile >> record1 >> record2 >> record3;
    
    	cout 
    		<< record1 << "\n" 
    		<< record2 << "\n" 
    		<< record3 << "\n";
    	infile.close();
    }

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    i need to use the .read fuction

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well that's really illogical in this case, but okay. The fundamental design problem of your current program being that each record entry is not the same size, so you cannot just read sizeof(rec). Your file for these data entries would probably need to be changed into more hunk-of-data things instead.

    Code:
    struct rec
    { 
    	char name[10];
    	char phone[7];
    };
    
    int main()
    {
    	rec record;
    	strcpy(record.name, "John");
    	strcpy(record.phone, "53265");
    
    	// More people like that
    
    	ostream oS("a.dat", ios::out | ios::binary);
    	oS.write((char*) &record, sizeof(record));
    
    	// More people like that
    }

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    why does it keep on outputting werid symbols that doesn't make any sense?

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    10 + 7 + 2 == 19 == strlen("john 1234567\r\nmary a")

    Then you print it name out, it's not null-terminated. Then you print out the part of that as a phone number. You'll note, that because it is binary, you read 10 chars into the name variable instance, when clearly "john" is not 10 bytes. This is why I thought text would be better. I think relevent reading for this would include the C++ FAQ-Lite's Serialization/Unserialization

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the input file was in binary, then your original code would be fine for reading in binary with read() except that you need to fix your file stream flags. You must specify binary mode when you open the file by using ios::binary, and as an input file you shouldn't be using ios::out.

    Of course, that assumes that you have a binary input file, which it looks like you don't have. Whoever told you that you must use read() should have provided an input file in binary for you. If your actual input file does look a little different when you open it in notepad (weird characters and stuff) then try making that simple change to ios::binary and it might work.


    BTW, the headers you are using are outdated and non-standard. If an instructor, book or tutorial is teaching you those headers, you might consider switching to something more modern.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM