这段代码的问题是它没有正确读取.txt文件,我提供了.txt文件的图像,同时还提供了它给我的当前输出。任何帮助都将受到欢迎。
#include <fstream>
#include <iostream>
using namespace std;
const int MAX_CHARS = 10;
const int MAX_STUDENTS = 1;
class File
{
public:
void openFile()
{
ifstream input_file("UserPass.txt", ios::binary);
if (input_file.fail())
{
cout << "Could not open file" << endl;
}
else
{
if (!input_file.read((char*)&studLoaded, sizeof(studLoaded)))
{
cout << "Could not read file" << endl;
}
else
{
streamsize bytesRead = input_file.gcount();
if (bytesRead != sizeof(studLoaded))
{
cout << "Could not read expected number of bytes" << endl;
}
else
{
input_file.read((char*)&studLoaded, sizeof(studLoaded));
input_file.close();
}
}
}
};
void displayFile()
{
for (size_t i = 0; i < MAX_STUDENTS; i++)
{
cout << "Username: " << studLoaded[i].username << endl;
cout << "Password: " << studLoaded[i].password << endl;
cout << "Verf ID:" << studLoaded[i].verfID << endl;
}
}
private:
typedef struct
{
char username[MAX_CHARS];
char password[MAX_CHARS];
int verfID;
}student_t;
student_t studLoaded[MAX_STUDENTS];
};
主要只是调用这些函数
File f;
f.openFile();
f.displayFile();
这是我当前的输出。我尝试了很多事情,但似乎无法正常工作。这是我得到的当前输出。
[从上面我的评论继续,鉴于您显示的输入文件是TEXT文件,您不想将其读为ios::binary
。为什么?读取二进制输入时,所有文本格式字符都没有特殊含义。您只需读取数据字节,并且'\n'
(值:0xa
)只是流中的另一个字节。阅读文本时,您想使用文本文件中的格式字符来告诉您何时阅读了一行或一个单词。
此外,如要以二进制形式读取@sheff所注释的,则取决于您事先知道要读取多少个字节到username
或password
中,以及verfID
int在其中的位置。流。他提供的链接很好地说明了过程C++ FAQ: Serialization and Unserialization。对于写入二进制数据,尤其是当数据位于struct
中时,除非进行序列化,否则无法保证编译器之间的可移植性,这是因为可能在结构中插入了填充位。]
因此,除非您有读写二进制文件的要求,否则最好将文本文件读取为文本。
通过重载<<
和>>
运算符可以一次从您的输入流中以文本形式读取学生的大量数据,从而使学生数据的读取和输出更加简单。例如,要使<<
运算符重载以读取student_t
数据,只需将成员函数添加到类中即可:
/* overload >> to read username, password, verfID from input stream */ friend std::istream& operator >> (std::istream& is, passfile& pf) { student_t s {}; /* temporary struct student */ /* attempt read of all 3 values (username, password, verfID) */ if (is >> s.username >> s.password >> s.verfID) { /* handle storage of s here */ } return is; /* return stream state */ }
使用重载运算符的好处不仅减少了必须编写的自定义输入函数,而且将大大减少
main()
。例如:
,而应避免使用STL提供的诸如std::string,std::vector之类的东西(对于您的int main (int argc, char **argv) { if (argc < 2) { /* verify at least 1 argument for filename */ std::cerr << "error: password filename required.\n"; return 1; } passfile pf (argv[1]); /* declare instance of class, with filename */ std::cout << pf; /* output all student data */ }
要把班级的各个部分放在一起,请避免使用诸如
char[CONST]
之类的基本类型
student_t
集合而不是对于类,您将使用一个额外的容器来强制使用唯一的verfID
。您可以自己编写一个函数,以便在插入新学生之前每次都扫描收集到的所有student_t
,也可以使用std::unordered_set为您提供更高效的方法。因此,使用STL容器,您只需要std::vector<student_t>
来存储学生信息(而不是数组),就可以使用std::unordered_set<int>
来散列verfID
并强制唯一性。您的班级private:
数据成员可能类似于:
class passfile { struct student_t { std::string username {}, password {}; /* user std:string istead */ int verfID; }; std::unordered_set<int> verfID; /* require unique verfID before add */ std::vector<student_t> students {}; /* use vector of struct for storage */ ...
对于
public:
成员,可以使用构造函数,该构造函数以要读取的文件名作为参数,然后,除了重载的<<
和>>
运算符外,您将仅需要一个辅助函数。帮助函数仅使用重载的>>
运算符循环输入,直到到达文件末尾。
您的构造函数实际上不需要只不过是:
public: passfile() {} passfile (std::string fname) { readpwfile (fname); } ...
可以反复使用
>>
运算符的帮助功能可以是:
void readpwfile (std::string fname) /* read all students from filename */ { std::ifstream f (fname); do f >> *this; /* use overloaded >> for read */ while (f); } ...
剩余的详细信息由重载的
<<
和>>
运算符处理。从<<
的重载开始,您实际上不需要做任何事情,只需遍历所有学生并以您喜欢的格式输出数据,例如
在类中的声明中使用的/* overload << to output all student data */ friend std::ostream& operator << (std::ostream& os, const passfile& pf) { for (auto s : pf.students) os << "Username: " << s.username << '\n' << "Password: " << s.password << '\n' << "Verf ID : " << s.verfID << "\n\n"; return os; }
((note:
friend
关键字,如果在其他位置定义了该函数,则在定义之前将省略friend
)您的>>
重载是大部分工作发生的地方,尽管逻辑很简单。您声明一个临时student_t
以从流中读取值。如果成功,则在unordered_set中进行快速查找以查看verfID
是否已经存在。如果不是,则将verfID
添加到您的unordered_set中,并将您的临时student_t
添加到您的向量中,然后完成。如果verfID
是重复的,则可以发出警告或错误,例如
/* overload >> to read username, password, verfID from input stream */ friend std::istream& operator >> (std::istream& is, passfile& pf) { student_t s {}; /* temporary struct student */ /* attempt read of all 3 values (username, password, verfID) */ if (is >> s.username >> s.password >> s.verfID) { /* if verfID not already in verfID unordered_set */ if (pf.verfID.find (s.verfID) == pf.verfID.end()) { pf.verfID.insert (s.verfID); /* add verfID to unordered_set */ pf.students.push_back (s); /* add temp student to vector */ } else /* warn on duplicate verfID */ std::cerr << "error: duplicate verfID " << s.verfID << ".\n"; } return is; /* return stream state */ }
在一个简短的示例中将其完全放入(基本上只是添加标题并关闭上面的信息的类),您将拥有:
#include <iostream> #include <fstream> #include <string> #include <vector> #include <utility> #include <unordered_set> class passfile { struct student_t { std::string username {}, password {}; /* user std:string istead */ int verfID; }; std::unordered_set<int> verfID; /* require unique verfID before add */ std::vector<student_t> students {}; /* use vector of struct for storage */ public: passfile() {} passfile (std::string fname) { readpwfile (fname); } void readpwfile (std::string fname) /* read all students from filename */ { std::ifstream f (fname); do f >> *this; /* use overloaded >> for read */ while (f); } /* overload >> to read username, password, verfID from input stream */ friend std::istream& operator >> (std::istream& is, passfile& pf) { student_t s {}; /* temporary struct student */ /* attempt read of all 3 values (username, password, verfID) */ if (is >> s.username >> s.password >> s.verfID) { /* if verfID not already in verfID unordered_set */ if (pf.verfID.find (s.verfID) == pf.verfID.end()) { pf.verfID.insert (s.verfID); /* add verfID to unordered_set */ pf.students.push_back (s); /* add temp student to vector */ } else /* warn on duplicate verfID */ std::cerr << "error: duplicate verfID " << s.verfID << ".\n"; } return is; /* return stream state */ } /* overload << to output all student data */ friend std::ostream& operator << (std::ostream& os, const passfile& pf) { for (auto s : pf.students) os << "Username: " << s.username << '\n' << "Password: " << s.password << '\n' << "Verf ID : " << s.verfID << "\n\n"; return os; } }; int main (int argc, char **argv) { if (argc < 2) { /* verify at least 1 argument for filename */ std::cerr << "error: password filename required.\n"; return 1; } passfile pf (argv[1]); /* declare instance of class, with filename */ std::cout << pf; /* output all student data */ }
示例输入文件
将上面的输入文件用作TEXT文件:
$ cat dat/userpass.txt Adam Pass121 1 Jamie abc1 2
示例使用/输出
运行程序并将您的输入文件作为第一个参数将导致:
$ ./bin/passwdfile dat/userpass.txt Username: Adam Password: Pass121 Verf ID : 1 Username: Jamie Password: abc1 Verf ID : 2
如果您需要通过提示用户输入信息来增加更多的学生,则只需:
std::cout << "enter user pass verfID: "; std::cin >> pf;
或通过适当的错误检查将是:
std::cout << "enter user pass verfID: "; if (std::cin >> pf) std::cout << "addition succeeded.\n"; else std::cerr << "addition failed.\n";
((尝试一下,并尝试添加重复的
verfID
...]] >>[仔细检查,如果您还有其他问题,请告诉我。到目前为止,使用STL提供的容器是更好的方法,而不是自己尝试重新发明轮子(这样可以避免很多错误...)