6 Replies - 1625 Views - Last Post: 15 December 2012 - 10:55 AM Rate Topic: -----

#1 imnewtoC++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-December 12

Write for Binary file gives error -858993460 for output

Posted 14 December 2012 - 07:36 PM

This program is supposed to read a binary file called "Read.dat" sort the data and calculate average. After it finishes it should write the new information into a new file called "Write.dat.

The problem I'm having is that when it writes the new data to the binary file the grades are coming out as "-858993460"



I believe the problem lies in the sort function but I have tried everything I can think of..

The files I uploaded should be .dat files but I'm not allowed to upload them.


#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <stdio.h>
#include "windows.h"
#include <fstream>
using namespace std;

void write_file();
void sort (int n);
void swap(int *p1, int *p2); 
void swap2( string *a1, string *a2);

void Sortedarray();
void get_array(); 
void Sortedarray2();
void average(int grades[5]);
const int length = 5;
 int grades[length];
string names[length];

int main()
{
	get_array();

	Sortedarray();//for testing only

	sort(5);

	Sortedarray2();

	average(grades);

	write_file();

		system("pause");
	return 0;
}
	
	


void get_array() 
{
	char filename[MAX_PATH + 1];
	int n = 0;
	char name[20];
	int grade=0;
	//int age = 0;
	int recsize = sizeof(name) + sizeof(int);
	//int recsize = sizeof(names) + sizeof(int);
	cout << "Please enter the file name you wish to access: ";
	cin.getline(filename, MAX_PATH);
	// Open file for binary read-write access.
	fstream fbin(filename, ios::binary | ios::in | ios::out);
	if (!fbin) 
	{
		cout << "Could not open " << filename << endl;
		cout<< "re-enter a valid file: ";
		cin.getline(filename, MAX_PATH);
	}
	// Get record number and go to record.
	cout<< "this is the information on file: " << endl;
	cout<< left << setw(20)<< "name" << setw(4) << "grade" << endl;
	int i=0;
	while(! fbin.eof())
	{
		fbin.seekp(n * recsize);
		// Read data from the file.
		fbin.read(name, sizeof(name) - 1);
				fbin.read(reinterpret_cast <char *> (grades[i]), sizeof(int));
		cout << endl << left<< setw(20)<< name << setw(4)<< grades[i] << endl;
		grades[i] = grade;
		names[i] = name;
		n++;
		i++;
	}
	fbin.close();
}

void Sortedarray()
{
	cout << "Data from original file: ";
	for(int i=0; i<0; i++)
	{
		cout<< endl << left<< setw(20) << names[i] << setw(4) << grades << endl;
	}
}
void Sortedarray2()
{
	cout << "Data from original file after being sorted: ";
	for(int i=0; i<5; i++)
	{
		cout<< endl << left<< setw(20) << names[i] << setw(4) << grades[i] << endl;
	}
}


void sort(int x) {
	int i,j, tempnumbers;
	string tempn;

	for (i=0; i<=length; i++)
	{
		for (j=i+1; j<length; j++)
		{
			if (grades[i] > grades[j])
			{ 
				//tempnumbers = grades[j];
				//grades[j] = grades[i];
				//grades[i] = tempnumbers; 

				//tempn = names[j];
				//names[j] = names[i];
				//names[i] = tempn;
			}
		}

	}
}


void average(int grades[5])
{
	int sum=0;
	for (int i=0;i<=5;i++)
	{
		sum = sum + grades[i];
	}
	int avg= sum/5;
	cout<< "The average grade is... : ";
	cout<< avg <<endl;
	
}






void  write_file()
{
	char filename[MAX_PATH + 1];
	int n = 0;
	char name[20];
	int grade=0;
	
	int recsize = sizeof(name) + sizeof(int);
	cout<< "enter the name of the file for destination: "<< endl;
	cin.getline(filename,MAX_PATH);
	fstream fbin (filename, ios::binary | ios::out );
for (int i=0;i<length;i++)
	{
		
    
		fbin.seekp(n * recsize);
		grade= grades[i];
		 fbin.write(names[i].c_str(), names[i].size() );
		 		fbin.write(name,sizeof(names[i])-1);
					fbin.write (reinterpret_cast <const char *> (&grades[i]), sizeof (grades));	
		
		n++;
	
	fbin.close();
	}
} 

Attached File(s)

  • Attached File  write.txt (131bytes)
    Number of downloads: 19
  • Attached File  read.txt (95bytes)
    Number of downloads: 19


Is This A Good Question/Topic? 0
  • +

Replies To: Write for Binary file gives error -858993460 for output

#2 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: Write for Binary file gives error -858993460 for output

Posted 14 December 2012 - 08:07 PM

I really cant try to run your code and test if my theory is correct because i can't see your .dat file but this might have something to do with it.

	while(! fbin.eof())
	{
		fbin.seekp(n * recsize);
		// Read data from the file.
		fbin.read(name, sizeof(name) - 1);
        fbin.read((char*)grades[i], sizeof(int));
        
		cout << endl << left<< setw(20)<< name << setw(4)<< grades[i] << endl;
		grades[i] = grade;
		names[i] = name;
		n++;
		i++;
	}


Looking at the code above, with the var name being char name[20] you are going to read 19 bytes of data with this call
fbin.read(name, sizeof(name) - 1);


Even if the name is only 5 bytes long, you will still read 19 bytes of data. So you could end up getting the name, grade and part of the next name all in one buffer. I would replace all of the istream::read() calls to istream::getline() (fbin.read() to fbin.getline());

Using this documentation: http://www.cplusplus...stream/getline/
You find that not only can you set the max amount of bytes to read, but also tell it to stop when it gets to a certain character by using the delim argument. for example, if your .dat file looked like the following

fred a
jerry c
tom d
imnewtoC++ d
UG_Cyber a

Then you could use this
char name[20];
fbin.getline(name, 19, ' '); //That tells the function to stop reading when it gets to a ' ' character
//Which is what seperates the names from the grades


to make sure that you only get the name.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4217
  • View blog
  • Posts: 13,225
  • Joined: 25-December 09

Re: Write for Binary file gives error -858993460 for output

Posted 14 December 2012 - 08:16 PM

You are trying to read and write a std::string with the read()/write() function, this is a problem. In order to properly read your std::string you need to know exactly how many characters were written, and since std::strings are of varying size you will need to write this information to your file, before the string so you can read this information before you try to read the actual string.

Also you should stop using the global variables and learn how to properly pass these variables to and from your functions.


Quote

Looking at the code above, with the var name being char name[20] you are going to read 19 bytes of data with this call
	fbin.read(name, sizeof(name) - 1);


Even if the name is only 5 bytes long, you will still read 19 bytes of data. So you could end up getting the name, grade and part of the next name all in one buffer. I would replace all of the istream::read() calls to istream::getline() (fbin.read() to fbin.getline());


First "name" is a std::string not a C-string. Second the files are being written and read in binary mode, getline() is not how you read files in binary mode.


Also this program would probably be easier if a structure was being used instead of trying to write the two separate values.

Jim

This post has been edited by jimblumberg: 14 December 2012 - 09:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 imnewtoC++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-December 12

Re: Write for Binary file gives error -858993460 for output

Posted 14 December 2012 - 08:33 PM

I realize that I have many global variables because I was testing a different method. Is structure the best route to go or is there a way to fix my array to stop outputting negative numbers? . Is there any good books on structuring binary files my current book doesn't really help me, I'm been following the read/write code from it.

this is how my files are supposed to look like.

The records in the original file:
A. Smith 89
T. Phillip 95
S. Long 76

(this part of my function works fine)

The new file sorted by scores:
S. Long 76
A. Smith 89
T. Phillip 95

(This part displays :)
S. Long -858993460
A. Smith -858993460
T. Phillip -858993460
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4217
  • View blog
  • Posts: 13,225
  • Joined: 25-December 09

Re: Write for Binary file gives error -858993460 for output

Posted 14 December 2012 - 09:15 PM

Quote

Is structure the best route to go or is there a way to fix my array to stop outputting negative numbers?

You are not properly reading your file. It looks like your name should be a C-string with a size of 19 and then the integer, with a '\0' following this integer, except the last record.
Here is a hexdump of your read.txt file:
00000000   41 2E 53 6D  69 74 68 00  CC CC CC CC  A.Smith.....
0000000C   CC CC CC CC  CC CC CC 59  00 00 00 00  .......Y....
00000018   54 2E 20 50  68 69 6C 6C  69 70 00 CC  T. Phillip..
00000024   CC CC CC CC  CC CC CC 5F  00 00 00 00  ......._....
00000030   53 2E 20 4C  6F 6E 67 00  69 70 00 CC  S. Long.ip..
0000003C   CC CC CC CC  CC CC CC 4C  00 00 00 00  .......L....
00000048   4A 2E 20 57  68 69 74 65  00 70 00 CC  J. White.p..
00000054   CC CC CC CC  CC CC CC 64  00 00 00     .......d...



As you can see this file has 4 records and the integer values are 89,95,76, and 100.


Here is a copy of your write.txt file:
00000000   53 2E 20 4C  6F 6E 67 00  CC CC CC CC  S. Long.....
0000000C   CC CC CC CC  CC CC CC CC  CC CC CC CC  ............
00000018   41 2E 53 6D  69 74 68 00  CC CC CC CC  A.Smith.....
00000024   CC CC CC CC  CC CC CC CC  CC CC CC CC  ............
00000030   54 2E 20 50  68 69 6C 6C  69 70 00 CC  T. Phillip..
0000003C   CC CC CC CC  CC CC CC CC  CC CC CC CC  ............
00000048   4A 2E 20 57  68 69 74 65  00 70 00 CC  J. White.p..
00000054   CC CC CC CC  CC CC CC CC  CC CC CC CC  ............
00000060   4A 2E 20 57  68 69 74 65  00 70 00 CC  J. White.p..
0000006C   CC CC CC CC  CC CC CC CC  CC CC CC CC  ............
00000078   CC CC CC CC  04 00 00 00  00 00 00     ...........




Was your read.txt file a file supplied by your instructor or did you create this file?

As for the structure it would probably make writing and reading the record easier.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1390
  • View blog
  • Posts: 4,847
  • Joined: 19-February 09

Re: Write for Binary file gives error -858993460 for output

Posted 14 December 2012 - 09:49 PM

In write_file the binary file appears to be closed in the for loop.

166	    fbin.close();


Was This Post Helpful? 1
  • +
  • -

#7 imnewtoC++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-December 12

Re: Write for Binary file gives error -858993460 for output

Posted 15 December 2012 - 10:55 AM

The read file was supplied by my instructor.
I fixed the "fbin.close" part earlier but I'm redoing the whole project using struct atm .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1