需要代码帮助以从 C++ 文件中读取特定字符串值并打印该值
Need help in code to read a particular string value from a file in C++ and print that value
我是 C++ 编程的新手,我需要编写代码来从文件中读取特定值。
例如,如果输入服务器名,则它必须显示 ABCDE-1。我正在尝试使用字符串函数,但无法获得结果。有没有人帮忙写下逻辑。
文件数据
[ServerName]
ABCDE-1;
[DBLcation]
\ABCDE-1\dat-520;
[ServerAPPsharename]
LR520APP;
[DerTablePath]
\ABCDE-1\dat-520\CDM\derd;
[PTPWait]
9995;
第一部分是文件数据
谢谢
读取字符串数组中的文件数据。
string str;
string Database[10];
if (myfile.is_open())
{
int i=0;
while (myfile)
{
getline(myfile, str);
// here I am trying to store each word in array of string
//Database[i]=str;
i++;
}
}
else
{
cout << "Error in opening file\n";
}
打印所选选项。
cout<<"Enter what you want "<<endl;
cout<<"1.-ServerName \n2.-DBLcation \n3.-ServerAPPsharename \n4.-DerTablePath \n5.-PTPWait"<<endl;
我需要编写代码来打印文件中所选选项的值。
我们可以看到您的源数据遵循特定模式。
括号[]
中有一个“键”,后面是一个“值”,后面跟一个分号;
如果我们有这样一个清晰的模式,那么我们就可以使用简单的机制。在“形式语言”理论中,根据乔姆斯基层次结构定义,我们称其为“Type-3 文法”或“正则文法”。请阅读 here。
优点是:您不需要解析器。在这种情况下,您可以使用所谓的正则表达式生存。
并且,C++ 支持带有 regex library 的正则表达式。
我将使用以下正则表达式来匹配上述模式:\[([^\]]+)\]\s+([^; ]+);
如果您将其粘贴到像 regex101 这样的正则表达式在线工具中,那么您就可以对其进行测试。描述是:
[([^]]+)]\s+([^; ]+);
\[([^\]]+)\]\s+([^; ]+);
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
1st Capturing Group ([^\]]+)
Match a single character not present in the list below [^\]]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group ([^; ]+)
Match a single character not present in the list below [^; ]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
;
matches a single character in the list ; (case sensitive)
; matches the character ; with index 5910 (3B16 or 738) literally (case sensitive)
下一个:
我们将使用std::map
来存储数据。我们可以使用它的 map 运算符来存储 和 以访问键的值。
如果你想存储一个新值,那么你可以使用例如config["ServerAPPsharename"] = "LR520APP"
。而且,如果你想通过键访问一个值,那么你可以写 std::cout << config["ServerAPPsharename"];
我们可以简单地遍历所有匹配项(只要有)并将数据存储在 std::map
中。我们将为此使用 regex_search。
然后我们可以轻松访问所有数据。
请将下面的代码作为许多可能的解决方案之一。
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <map>
#include <iomanip>
std::regex re{R"(\[([^\]]+)\]\s+([^; ]+);)"};
const std::string configFileName{ "test.txt" };
int main() {
// Open the source file with config data and check, if it could be opened successfully
if (std::ifstream configFileStream{ configFileName }; configFileStream) {
// Load the complete content of the file in this string
std::string configData(std::istreambuf_iterator<char>(configFileStream), {});
// We will use an associative container to store the key and its value.
std::map<std::string, std::string> config{};
// Now iterate over all pattern matches and store the data in our map
for (std::smatch sm; std::regex_search(configData, sm, re); configData = sm.suffix())
config[sm[1]] = sm[2];
// Example: show value for ServerAPPsharename
std::cout << "ServerAPPsharename: " << config["ServerAPPsharename"] << "\n\n";
// show all config data
std::cout << "\nAll data:\n\n";
for (const auto& [key, value] : config) std::cout << std::setw(20) << key << ": " << value << '\n';
}
else { // Error, could not open source file. Show message
std::cerr << "\nError: Could not open '" << configFileName << "'\n\n";
}
return 0;
}
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void read_file();
int main()
{
read_file();
return 0;
}
void read_file()
{
ifstream myfile;
myfile.open("config.txt");
string str;
if (myfile.is_open())
{
cout << "Enter the name :" << endl;
string in_val;
cin >> in_val;
while (getline(myfile, str))
{
if (str == "[" + in_val + "]")
{
getline(myfile, str);
cout << str << endl;
}
}
}
else
cout << "Error in opening file\n";
}
我是 C++ 编程的新手,我需要编写代码来从文件中读取特定值。 例如,如果输入服务器名,则它必须显示 ABCDE-1。我正在尝试使用字符串函数,但无法获得结果。有没有人帮忙写下逻辑。
文件数据
[ServerName]
ABCDE-1;
[DBLcation]
\ABCDE-1\dat-520;
[ServerAPPsharename]
LR520APP;
[DerTablePath]
\ABCDE-1\dat-520\CDM\derd;
[PTPWait]
9995;
第一部分是文件数据 谢谢
读取字符串数组中的文件数据。
string str;
string Database[10];
if (myfile.is_open())
{
int i=0;
while (myfile)
{
getline(myfile, str);
// here I am trying to store each word in array of string
//Database[i]=str;
i++;
}
}
else
{
cout << "Error in opening file\n";
}
打印所选选项。
cout<<"Enter what you want "<<endl;
cout<<"1.-ServerName \n2.-DBLcation \n3.-ServerAPPsharename \n4.-DerTablePath \n5.-PTPWait"<<endl;
我需要编写代码来打印文件中所选选项的值。
我们可以看到您的源数据遵循特定模式。
括号[]
中有一个“键”,后面是一个“值”,后面跟一个分号;
如果我们有这样一个清晰的模式,那么我们就可以使用简单的机制。在“形式语言”理论中,根据乔姆斯基层次结构定义,我们称其为“Type-3 文法”或“正则文法”。请阅读 here。
优点是:您不需要解析器。在这种情况下,您可以使用所谓的正则表达式生存。
并且,C++ 支持带有 regex library 的正则表达式。
我将使用以下正则表达式来匹配上述模式:\[([^\]]+)\]\s+([^; ]+);
如果您将其粘贴到像 regex101 这样的正则表达式在线工具中,那么您就可以对其进行测试。描述是:
[([^]]+)]\s+([^; ]+);
\[([^\]]+)\]\s+([^; ]+);
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
1st Capturing Group ([^\]]+)
Match a single character not present in the list below [^\]]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group ([^; ]+)
Match a single character not present in the list below [^; ]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
;
matches a single character in the list ; (case sensitive)
; matches the character ; with index 5910 (3B16 or 738) literally (case sensitive)
下一个:
我们将使用std::map
来存储数据。我们可以使用它的 map 运算符来存储 和 以访问键的值。
如果你想存储一个新值,那么你可以使用例如config["ServerAPPsharename"] = "LR520APP"
。而且,如果你想通过键访问一个值,那么你可以写 std::cout << config["ServerAPPsharename"];
我们可以简单地遍历所有匹配项(只要有)并将数据存储在 std::map
中。我们将为此使用 regex_search。
然后我们可以轻松访问所有数据。
请将下面的代码作为许多可能的解决方案之一。
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <map>
#include <iomanip>
std::regex re{R"(\[([^\]]+)\]\s+([^; ]+);)"};
const std::string configFileName{ "test.txt" };
int main() {
// Open the source file with config data and check, if it could be opened successfully
if (std::ifstream configFileStream{ configFileName }; configFileStream) {
// Load the complete content of the file in this string
std::string configData(std::istreambuf_iterator<char>(configFileStream), {});
// We will use an associative container to store the key and its value.
std::map<std::string, std::string> config{};
// Now iterate over all pattern matches and store the data in our map
for (std::smatch sm; std::regex_search(configData, sm, re); configData = sm.suffix())
config[sm[1]] = sm[2];
// Example: show value for ServerAPPsharename
std::cout << "ServerAPPsharename: " << config["ServerAPPsharename"] << "\n\n";
// show all config data
std::cout << "\nAll data:\n\n";
for (const auto& [key, value] : config) std::cout << std::setw(20) << key << ": " << value << '\n';
}
else { // Error, could not open source file. Show message
std::cerr << "\nError: Could not open '" << configFileName << "'\n\n";
}
return 0;
}
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void read_file();
int main()
{
read_file();
return 0;
}
void read_file()
{
ifstream myfile;
myfile.open("config.txt");
string str;
if (myfile.is_open())
{
cout << "Enter the name :" << endl;
string in_val;
cin >> in_val;
while (getline(myfile, str))
{
if (str == "[" + in_val + "]")
{
getline(myfile, str);
cout << str << endl;
}
}
}
else
cout << "Error in opening file\n";
}