Подскажите, что не так.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
//================================
using namespace std;
//=============================
struct log{
string login;
string passw;
};
//===============================
int main(){
const int n = 80;
int lenfile = 0, i = 0, pos = 0;
char* str = new char[];
string s1;
struct log* q = new struct log[lenfile];
ifstream f("C:\\file.txt");
if(!f){ cout << "Cannot open this file." << endl; return 0; }
while(!f.eof()){
f.getline(str,n);
lenfile++;
}
f.seekg(0); // Хочу чтобы курсор вернулся к началу файла
for(i = 0; i < lenfile; i++){
f.getline(str,n); //после этой команды в стр пустая строка
s1.assign(str); //а мне надо, чтобы там бала снова первая сторка из файла
pos = s1.find(':');
q[i].login = s1.substr(0, pos);
q[i].passw = s1.substr(pos + 1, s1.size());
}
//for(i = 0; i < lenfile; i++) cout << q[i].login << q[i].passw << endl;
return 0;
}
Здравствуйте, leg0, Вы писали:
L>Подскажите, что не так.
L>#include <iostream>
L>#include <fstream>
L>#include <string>
L>#include <iomanip>
L>//================================
L>using namespace std;
L>//=============================
L>struct log{
L> string login;
L> string passw;
L>};
L>//===============================
L>int main(){
L> const int n = 80;
L> int lenfile = 0, i = 0, pos = 0;
L> char* str = new char[];
L> string s1;
L> struct log* q = new struct log[lenfile];
L> ifstream f("C:\\file.txt");
L> if(!f){ cout << "Cannot open this file." << endl; return 0; }
L> while(!f.eof()){
L> f.getline(str,n);
L> lenfile++;
L> }
if (f.eof())
f.clear();
L> f.seekg(0); // Хочу чтобы курсор вернулся к началу файла
L> for(i = 0; i < lenfile; i++){
L> f.getline(str,n); //после этой команды в стр пустая строка
L> s1.assign(str); //а мне надо, чтобы там бала снова первая сторка из файла
L> pos = s1.find(':');
L> q[i].login = s1.substr(0, pos);
L> q[i].passw = s1.substr(pos + 1, s1.size());
L> }
L> //for(i = 0; i < lenfile; i++) cout << q[i].login << q[i].passw << endl;
L> return 0;
L>}