Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Write for Binary file gives error -858993460 for output

$
0
0
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();
	}
} 


Viewing all articles
Browse latest Browse all 51036

Trending Articles