czytanie z pliku binarnego

0

Siema wszystkim mam problem z operacjami wejścia/wyjścia na plikach.
Mianowicie próbuje odczytane z pliku binarnego dane w rzucić do tablicy ułamków ale mi nic w niej nie zapisuje. Mógł by mi ktoś podpowiedzieć co robię źle oto kod:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
#include "ulamek.h"
#include<fstream.h>
#include<cctype.h>
#include<string.h>
int main(int argc, char* argv[])
{
int n;
cout<<"podaj n: \n";
cin>>n;
//1
ulamek *t = new ulamek [n];
//2
for (int i = 0; i < n; i++) {
	ulamek tmp;
	cin>>tmp;
	t[i] = tmp;
}
//3
ofstream output("c:\\plik1.txt");
for (int i = 0; i < n; i++) {
	output<<t[i];
	output<<" ";
}
output.close();
output.open("c:\\plik2.bin",ios::binary);
for (int i = 0; i < n; i++) {
	output.write((char*) &t[i],sizeof(ulamek));
}
output.close();
char s;
ifstream input;
input.open("c:\\plik1.txt");
if (!input.is_open()) {
	 cerr << "\aBlad otwarcia pliku\nDo widzenia...";
	 return EXIT_FAILURE;
  }
	while (input.good()){
		input.get(s);
		cout<<s<<endl;
	}
input.close();
//4
delete []t;
//5
ulamek *t1 = new ulamek[n];
input.open("c:\\plik2.bin");
for (int i = 0; i < n; i++) {
	input.read((char*)&t1[i],sizeof(ulamek));
	cout<<t1[i]<<endl;
}

system("pause");
exit(1);
}
//---------------------------------------------------------------------------

Z góry dzięki

0

Hmm, nie wiem co tam konkretnie u ciebie jest zle, albowiem nie mam pojecie co to ten 'ulamek' i skad wytrzasnales 'ulamek.h' (mogles chociaz podac zawartosc). Nie wiem tez, po jaka cholere wtryniales tam vcl, przeciez nie korzystasz.
W kazdym razie:

  • Dane binarne mozna zapisywac i czytac calymi tablicami.
  • Mozna wczytywac i wypisywac dane z elementow tablic bezposrednio do i z strumieni.
  • (tak na marginesie) Nazwy naglowow w C++ pisze sie bez rozszezen
    Caly ten balagan z kilogramami zmiennych tymczasowych zaslania ci kod, w tym i zapewne jakis blad jakikolwiek by on nie byl. Proponuje posprawdzac hexedytorem ten plik co on tam tworzy (albo chociaz sprawdzic rozmiar), moze blad jest w zapisie.

Dzialajacy kod moze wygladac np. tak:

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    int n;
    cout << "podaj n: ";
    cin >> n;
    float* f = new float[n];

    for(int i=0; i<n; i++) cin >> f[i];

    ofstream ofs("foo.bin", ios::binary);
    ofs.write((char*)f, n*sizeof(float));
    ofs.close();

    delete[] f;

    f = new float[n];
    ifstream ifs("foo.bin", ios::binary);
    ifs.read((char*)f, n*sizeof(float));
    ifs.close();

    for(int i=0; i<n; i++) cout << f[i] << " ";
    cout << endl;

    delete[] f;

    system("PAUSE");
    return 0;
}

Popodstawiaj elementy ktore masz u siebie i wtedy zobacz, czy dziala.

0

Podstawiłem do tego kodu mój ulamek i oto odpowiedz linkera:

[ILINK32 Error] Error: Unresolved external 'ulamek::~ulamek()' referenced from C:\DOCUMENTS AND SETTINGS\MASTODONT\MOJE DOKUMENTY\RAD STUDIO\PROJECTS\DEBUG\FILE2.OBJ
[ILINK32 Error] Error: Unresolved external 'operator >>(std::basic_istream<char, std::char_traits<char> >&, ulamek&)' referenced from C:\DOCUMENTS AND SETTINGS\MASTODONT\MOJE DOKUMENTY\RAD STUDIO\PROJECTS\DEBUG\FILE2.OBJ
[ILINK32 Error] Error: Unresolved external 'ulamek::ulamek(int, int)' referenced from C:\DOCUMENTS AND SETTINGS\MASTODONT\MOJE DOKUMENTY\RAD STUDIO\PROJECTS\DEBUG\FILE2.OBJ
[ILINK32 Error] Error: Unresolved external 'operator <<(std::basic_ostream<char, std::char_traits<char> >&, const ulamek&)' referenced from C:\DOCUMENTS AND SETTINGS\MASTODONT\MOJE DOKUMENTY\RAD STUDIO\PROJECTS\DEBUG\FILE2.OBJ

a to kod ulamka. Dodam że sam ulamek mi działa i nie potrafię rozgryźć czemu przy operacjach na plikach się sypie.

#include<math.h>
#include<iostream.h>
class ulamek{
	private:
	int l,m;
	friend ostream& operator<< (ostream&, const ulamek&);
	friend istream& operator>> (istream&, ulamek&);

	public:
	ulamek(int = 0,int = 1);
	ulamek(const ulamek&);
	~ulamek();
	void set(int,int);
	float oblicz();

	ulamek operator+(const ulamek &);
	ulamek operator-(const ulamek &);
	ulamek operator*(const ulamek &);
	ulamek operator/(const ulamek &);
	ulamek operator=(const ulamek &);
	void operator+=(const ulamek &);
	void operator-=(const ulamek &);
	void operator*=(const ulamek &);
	void operator/=(const ulamek &);

	ulamek operator()(int, int);
	int operator==(const ulamek &);
	operator float(){
    	return (float)l/m;
	}

};  

#include "ulamek.hcode>`#include "ulamek.h"
ulamek::ulamek(int a, int b):l(a),m(b){}
ulamek::~ulamek(){}
ulamek::ulamek(const ulamek &other){
l=other.l;
m=other.m;
}
void ulamek::set(int a, int b){
l = a;
m = b;
}
float ulamek::oblicz(){
if (m==0) {
cout<<"Mianownik rowny (0) - wyjscie z programu"<<endl;
system("pause");
exit(1);
}
float tmp = (float)l/(float)m;
return tmp;
}
ulamek ulamek::operator+(const ulamek &other){
l*=other.m;
int tmp = other.lm;
m
=other.m;
l+=tmp;
return (this);
}
ulamek ulamek::operator-(const ulamek &other){
l
=other.m;
int tmp = other.lm;
m
=other.m;
l-=tmp;
return (this);
}
ulamek ulamek::operator
(const ulamek &other){
l*=other.l;
m*=other.m;
return (this);
}
ulamek ulamek::operator/(const ulamek &other){
l
=other.m;
m*=other.l;
return (this);
}
ulamek ulamek::operator=(const ulamek &other){
l=other.l;
m=other.m;
return (this);
}
void ulamek::operator+=(const ulamek &other){
l
=other.m;
int tmp = other.l
m;
m*=other.m;
l+=tmp;
}
void ulamek::operator-=(const ulamek &other){
l*=other.m;
int tmp = other.lm;
m
=other.m;
l-=tmp;
}
void ulamek::operator*=(const ulamek &other){
l*=other.l;
m*=other.m;
}
void ulamek::operator/=(const ulamek &other){
l*=other.m;
m*=other.l;
}

ulamek ulamek::operator()(int a, int b){
	l=a;
	m=b;
	return *this;
}
int ulamek::operator==(const ulamek &other){
	if ((l/m)==(other.l/other.m)) {
	return 1;
	}
	else return 0;
}
ostream& operator<< (ostream& output, const ulamek& g){
	output<<g.l<<"/"<<g.m;
	return output;
}
istream& operator>> (istream& input, ulamek& g){
	input>>g.l;
	input.ignore();
	input>>g.m;
	return input;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1