Thread: Why won't the file contents clear??

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Why won't the file contents clear??

    Ok, changed the fstream to ofstream in the code below, the file is created ok. The file is initialized with a struct of 2 char[15] and 1 int[4] array. The first char[], lastName, has the value "unassigned". I can write data to the file (last name, first name, age) ok, except that if the last name is shorter than "unassigned", the remaining letters in "unassigned" are left in the file. For example, if I enter "Smith", that prints with a space (for null character I guess), but "gned" is left in the field. I'm trying to reinitialize it with blank, but that doesn't work. What's going on? Thanks.
    Code:
    ofstream personOut("nameAge.txt");
    	if (!personOut) {cout << "Error!";}
    	person id = {"unassigned", "", 0};
    	person blank = {"             ", "", 0};
    	for (int i = 0; i < 100; i++)
    		personOut.write((char *)&id, sizeof(person));
    
    	// 14.11.b	Enter records sequentially
    	int numOfRec;
    	cout << "\nEnter number of records to enter: ";
    	cin >> numOfRec;
    	for (int j = 0; j < numOfRec; j++)
    	{     
    		personOut.seekp(j * sizeof(person));
    		personOut.write((char *)&blank, sizeof(person));
    		cout << "\nEnter last name, first name and age: ";
    		cin >> id.lastName >> id.firstName >> id.age;
    		personOut.seekp(j * sizeof(person));
    		personOut.write((char *)&id, sizeof(person));
    	}

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this. Change this:

    ofstream personOut("nameAge.txt");

    to this:

    ofstream personOut("nameAge.txt",ios::binary);

    Do the same thing if you open it for reading later.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Question What's going on?

    Tried it, didn't work. There was an anomaly though. If I look at the nameAge file in Notepad, I see the "gned" remnant of "unassigned". When I write code to output the file to screen, the file prints out w/o the "gned". But then it messes up the output - skipping some lines and not printing the last values entered. That part I'll worry about later. Maybe the "gned" is throwing the values off? As far as I can tell, I'm following the text examples pretty closely. What's the problem? Thanks.
    Last edited by Brown Drake; 11-21-2001 at 08:19 AM.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    That is, I can read the file with an ifstream variable and it doesn't print the "gned", but doesn't print correctly either.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you look at the nameAge file in Notepad, and see the "gned" remnant, this is not surprising. When the record is written, it writes the whole char[15], not stopping at the string terminator. But this if fine, because when you read it back in:

    personOut.read((char *)&id, sizeof(person));

    it will again read 15 bytes for the char[15], and this will include the string terminator. So when you print it out, it will stop at the string terminator.

    You mention you have an int[4] array in the structure, but it appears as if age is just an integer from your "cin" statement.
    typedef struct person {
    char firstName[15];
    char lastName[15];
    int age;
    };

    One other wild guess is maybe seekp() isn't going back correctly, so in your for-loop before the 2nd write do this:

    personOut.seekp(0);
    personOut.seekp(j * sizeof(person));
    Last edited by swoopy; 11-21-2001 at 02:19 PM.

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You'll have to memset the struct if you want it to be readable by humans(in which case it probably shouldn't be written to using fstream::write()) as unintialised arrays on the stack will contain junk and close the write file before reading. This might work -

    Code:
    struct person
    {
    	char lastName[15];
    	char firstName[15];
    	int age;
    };
    
    int main(int argc, char* argv[])
    {
    	ofstream personOut("nameAge.txt",ios::binary);
    	if (!personOut) {cout << "Error!";}
    	person id = {"unassigned", "", 0};
    	person blank = {"             ", "", 0};
    	for (int i = 0; i < 100; i++)
    		personOut.write((char *)&id, sizeof(person));
    
    	// 14.11.b	Enter records sequentially
    	int numOfRec;
    	cout << "\nEnter number of records to enter: ";
    	cin >> numOfRec;
    	for (int j = 0; j < numOfRec; j++)
    	{     
    		memset(&id,0,sizeof(person));
    		cout << "\nEnter last name, first name and age: ";
    		cin >> id.lastName >> id.firstName >> id.age;
    		personOut.seekp(j * sizeof(person));
    		personOut.write((char *)&id, sizeof(person));
    	}
    
    	personOut.close();
    
    
    	ifstream personIn("nameAge.txt",ios::binary);
    	for(int k =0;k<numOfRec;k++)
    	{
    		personIn.seekg(k * sizeof(person));
    		personIn.read((char *)&id, sizeof(person));
    		cout << id.lastName  <<  " "<< id.firstName << " "<< id.age << endl;
    		
    	}
    
    
    	return 0;
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 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. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Swap the file contents
    By jayfriend in forum C Programming
    Replies: 5
    Last Post: 01-16-2007, 10:36 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM