mes,
| Код | void SelectTable::ReadFileToArray(string FileName) {
ShowMessage(FileName.c_str()); // - Нет ошибки
ShowMessage(StrCount(FileName.c_str())); // - Нет ошибки
StrCount(FileName.c_str()); // - Нет ошибки
n = StrCount(FileName.c_str()); // - ЕСТЬ ОШИБКА !
} |
странно, что ошибку выдает только при присвоении !!
Сегодня еще поковырял свой код:
то, что было:
| Код |
//---------------------------------------------------------------------------
#include <string.h> #include <fstream.h> #include <windows.h>
#include <vcl.h> #pragma hdrstop
#pragma argsused
//---------------------------------------------------------------------------
class SelectTable {
public:
int StrCount (string FileName);
void ReadFileToArray(string FileName);
SelectTable (void);
~SelectTable (void);
private:
int n;
string *array;
};
//---------------------------------------------------------------------------
int SelectTable::StrCount (string FileName) {
string str;
int String_Counter = 0;
ifstream File(FileName.c_str());
while( !File.eof() ) { getline(File, str); ++String_Counter; };
return String_Counter; };
//---------------------------------------------------------------------------
void SelectTable::ReadFileToArray(string FileName) {
n=StrCount(FileName);
array = new string[n];
if (n > 0) {
string str; ifstream File(FileName.c_str());
int i=0;
while( !File.eof() ) { getline(File, str); array[++i]=str; }; }
} //---------------------------------------------------------------------------
SelectTable::~SelectTable(void) {
if(n) delete[] array;
}
//---------------------------------------------------------------------------
SelectTable::SelectTable(void):n(0),array(NULL) { }
//---------------------------------------------------------------------------
int main(int argc, char* argv[]) {
SelectTable *My;
My->ReadFileToArray("C:\\file.TXT");
My->~SelectTable();
return 0; }
//---------------------------------------------------------------------------
|
Поменял всего в одной строке:
И заработало !
| Код |
//---------------------------------------------------------------------------
#include <string.h> #include <fstream.h> #include <windows.h>
#include <vcl.h> #pragma hdrstop
#pragma argsused
//---------------------------------------------------------------------------
class SelectTable {
public:
int StrCount (string FileName);
void ReadFileToArray(string FileName);
void SaveToFile(string FileName);
SelectTable (void);
~SelectTable (void);
private:
int n;
string *array;
};
//---------------------------------------------------------------------------
int SelectTable::StrCount (string FileName) {
string str;
int String_Counter = 0;
ifstream File(FileName.c_str());
while( !File.eof() ) { getline(File, str); ++String_Counter; };
return String_Counter; };
//---------------------------------------------------------------------------
void SelectTable::ReadFileToArray(string FileName) {
n=StrCount(FileName);
array = new string[n];
if (n > 0) {
string str; ifstream File(FileName.c_str());
int i=0;
while( !File.eof() ) { getline(File, str); array[i++]=str; }; }
} //---------------------------------------------------------------------------
SelectTable::~SelectTable(void) {
if(n) delete[] array;
}
//---------------------------------------------------------------------------
SelectTable::SelectTable(void):n(0),array(NULL) { }
//---------------------------------------------------------------------------
void SelectTable::SaveToFile(string FileName) { std::ofstream file(FileName.c_str()); for(unsigned int i=0; i<n; ++i) file << array[i] << '\n'; }
//---------------------------------------------------------------------------
int main(int argc, char* argv[]) {
SelectTable *My = new SelectTable;
My->ReadFileToArray("C:\\file");
My->SaveToFile("C:\\file 2");
return 0; }
//---------------------------------------------------------------------------
|
Добавлено через 3 минуты и 6 секунд Вот где была ошибка:
| Код | SelectTable *My= new SelectTable; |
надо было дописать:
забыл Теперь и сам доехал Всем спасибо 
Еще метод добавил SaveToFile - чтобы проверить считывает ли в массив, нормально считал и записал в файл 
|