ниже приведённая программа (малость переделанные, оставшиеся со школы исходники) прекрасно работает под linux, но при кросскомпиляции с помощью mingw32-g++ (Gentoo 4.4.2 p1.0) 4.4.2 или сборке на виртуальной машине с использованием msvs6, программа работает некорректно, а именно не происходит запись\чтение в_файл\из_файла. пожалуйста помогите найти проблему.
PHP код:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct Book {
char author[45];
char name[25];
char genre[15];
int year;
int id;
};
int enterChoice(void);
void newBook(fstream &);
void updateBook(fstream &);
void deleteBook(fstream &);
void searchBook(fstream &);
void printBook(fstream &);
int main() {
fstream libraryFile("lib.dat", ios::out | ios::in);
if (!libraryFile) {
cerr << "Can't open file" << endl;
exit(1);
}
int choice;
while ((choice = enterChoice()) != 6) {
switch (choice) {
case 1:
newBook(libraryFile);
break;
case 2:
updateBook(libraryFile);
break;
case 3:
deleteBook(libraryFile);
break;
case 4:
searchBook(libraryFile);
break;
case 5:
printBook(libraryFile);
break;
default:
cerr << "Incorrect choice" << endl;
break;
}
}
return 0;
}
int enterChoice(void) {
cout << " 1 - new book" << endl
<< " 2 - edit book" << endl
<< " 3 - remove book" << endl
<< " 4 - search in books" << endl
<< " 5 - print book" << endl
<< " 6 - exit" << endl;
int choice;
cout << "Enter your choise: ";
cin >> choice;
cout << endl;
return choice;
}
void newBook(fstream & insertInFile) {
Book book;
book.id = 0;
cout << "Adding book" << endl << "Enter ID of book (1 -- 100): ";
int ID;
cin >> ID;
insertInFile.seekg((ID - 1) * sizeof(Book));
insertInFile.read((char *)&book, sizeof(Book));
if (book.id == 0) {
cout << " Author: ";
cin >> book.author;
cout << " Name: ";
cin >> book.name;
cout << " Genre: ";
cin >> book.genre;
cout << " Year: ";
cin >> book.year;
book.id = ID;
insertInFile.seekp((ID - 1) * sizeof(Book));
insertInFile.write((char *)&book, sizeof(Book));
cout << "Book with ID " << ID << " created" << endl;
}
else
cerr << "Book with ID " << ID << " was created" << endl;
}
void deleteBook(fstream & deleteFromFile) {
Book book;
cout << "Deleting book" << endl << "Enter ID of book (1 -- 100): ";
int ID;
cin >> ID;
deleteFromFile.seekg((ID - 1) * sizeof(book));
deleteFromFile.read((char *)&book, sizeof(book));
if (book.id != 0) {
Book blankBook = {" " , " ", " ", 0, 0};
deleteFromFile.seekp((ID - 1) * sizeof(book));
deleteFromFile.write((char *)&blankBook, sizeof(book));
cout << "Book with ID " << ID << " deleted" << endl;
}
else {
cerr << "Book with ID " << ID << " is impety" << endl;
}
}
void updateBook(fstream & updateFile) {
cout << "Updating book" << endl << "Enter ID of book (1 -- 100): ";
int ID;
cin >> ID;
Book book;
book.id = 0;
updateFile.seekg((ID - 1) * sizeof(book));
updateFile.read((char *)&book, sizeof(book));
if (book.id != 0) {
cout << " Author: ";
cin >> book.author;
cout << " Name: ";
cin >> book.name;
cout << " Genre: ";
cin >> book.genre;
cout << " Year: ";
cin >> book.year;
updateFile.seekp((ID - 1) * sizeof(book));
updateFile.write((char *)&book, sizeof(book));
}
else
cerr << "Book with ID " << ID << " not created" << endl;
}
void searchBook(fstream & searchFile) {
Book book;
cout << "Searching book" << endl << "Enter a string for find: ";
char buff[45];
cin >> buff;
cout << endl;
for (int i = 0; i < 100; i++) {
searchFile.seekg(i * sizeof(book));
searchFile.read((char *)&book, sizeof(book));
if ((strstr((char *)&book, buff)) != 0) {
cout << " Author: " << book.author << endl
<< " Name: " << book.name << endl
<< " Genre: " << book.genre << endl
<< " Year: " << book.year << endl
<< " ID: " << book.id << endl << endl;
}
}
}
Орфаграфические ошибки пренадлижат WWWКорпарейшен.exe Copyright © 00 - 2050.