我目前正在創建一個自定義檔案結構(檔案擴展名為 .ndev),以提高我在 C 中處理檔案的技能。我可以以特定方式將值保存到檔案中(見下文)
{
"username": "Nikkie",
"password": "test",
"role": "Developer",
"email": "[email protected]"
}
這與實際的 JSON 沒有任何關系,它只是像它一樣結構化。
我的問題是,我怎樣才能用 C 讀取其中一個變數的值,而不會像下面的螢屏截圖那樣出現:

我當前撰寫檔案的代碼:
void user::RegisterUser(string username, string password, string role, string email)
{
string filename = "E:\\Coding\\C\\test\\data\\" username ".ndev";
ifstream CheckFile(filename);
if (CheckFile.good())
{
printf("User already exists!");
}
else {
ofstream UserDataFile(filename);
UserDataFile << "{\n\t\"username\": \"" << username << "\",\n\t\"password\": \"" << password << "\",\n\t\"role\": \"" << role << "\",\n\t\"email\": \"" << email << "\"\n}";
UserDataFile.close();
}
CheckFile.close();
}
不要在密碼加密方面對我大發雷霆,我稍后會補充。我目前正試圖讓它在我做任何其他事情之前實際讀取這些值
我當前讀取檔案的代碼:
void user::LoginUser(string username)
{
string filename = "E:/Coding/C/test/data/" username ".ndev";
ifstream UserFile(filename, ios_base::in);
if (UserFile.good())
{
string name;
string passw;
string role;
string email;
while (UserFile >> name >> passw >> role >> email)
{
cout << name << passw << endl;
cout << role << email << endl;
}
}
else
{
printf("User doesn't exist!");
}
}
我似乎無法讓它正確顯示值,控制臺和 VS 除錯版本中也沒有列出任何錯誤。
uj5u.com熱心網友回復:
從實際的角度來看,沒有理由以這種格式存盤您的結構。有更簡單的方法。
無論如何,這是一個起點(演示):
#include <iostream>
#include <string>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;
// your structure
struct person
{
string name, pass, role, mail;
};
// the tokens your format is using
enum class token : char
{
lb = '{',
rb = '}',
sc = ':',
comma = ',',
str,
end
};
token tk; // current token
string str; // if the current token is token::str, str is its value
// get_token breaks the input stream into tokens - this is the lexer, or tokenizer, or scanner
token get_token(istream& is)
{
char c;
if (!(is >> c))
return tk = token::end;
switch (c)
{
case '{':
case '}':
case ':':
case ',':
return tk = token(c);
case '"':
is.unget();
is >> quoted(str);
return tk = token::str;
default: throw "unexpected char";
}
}
// throws if the current token is not the expected one
void expect(istream& is, token expected, const char* error)
{
if (tk != expected)
throw error;
get_token(is);
}
// throws if the current token is not a string
string expect_str(istream& is, const char* error)
{
if (tk != token::str)
throw error;
string s = str;
get_token(is);
return s;
}
// the actual parser; it extracts the tokens one by oneand compares them with the expected order.
// if the order is not what it expects, it throws an exception.
void read(istream& is, person& p)
{
get_token(is); // prepare the first token
expect(is, token::lb, "'{' expected");
map<string, string> m; // key/values storage
while (tk == token::str)
{
string k = expect_str(is, "key expected");
expect(is, token::sc, "':' expected");
string v = expect_str(is, "value expected");
if (m.find(k) == m.end())
m[k] = v;
else
throw "duplicated key";
if (tk == token::comma)
get_token(is);
else
break; // end of of key/value pairs
}
expect(is, token::rb, "'}' expected");
expect(is, token::end, "eof expected");
// check the size of m & the keys & copy from m to p
// ...
}
int main()
{
ifstream is{ "c:/temp/test.txt" };
if (!is)
return -1;
try
{
person p;
read(is, p);
}
catch (const char* e)
{
cout << e;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415358.html
標籤:
上一篇:lstat和ls權限輸出有些不同
下一篇:根據單元格值連接每列中的值
