Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从冒号分隔的.txt文件C++;_C++_String_Fstream - Fatal编程技术网

C++ 从冒号分隔的.txt文件C++;

C++ 从冒号分隔的.txt文件C++;,c++,string,fstream,C++,String,Fstream,伙计们。我正在编写这个小测试程序,将“EXAMPLE.txt”中的文本文件读取到我的主程序中。在输出时,我用“*”来显示输出期间的数据,即我要将其提取出来并定位到数组中的数据。比如说,在这个测试程序中,我想要提取的数据是“JY9757AC”、“AZ9107AC”、“GY9Z970C”。但在那之后,我做了一次试运行,在输出方面我遇到了这个问题 EXAMPLE.txt ABC:JY9757AC HDMI:AZ9107AC SNOC:GY9Z970C MAIN.CPP main() { st

伙计们。我正在编写这个小测试程序,将“EXAMPLE.txt”中的文本文件读取到我的主程序中。在输出时,我用“*”来显示输出期间的数据,即我要将其提取出来并定位到数组中的数据。比如说,在这个测试程序中,我想要提取的数据是“JY9757AC”、“AZ9107AC”、“GY9Z970C”。但在那之后,我做了一次试运行,在输出方面我遇到了这个问题

EXAMPLE.txt

ABC:JY9757AC
HDMI:AZ9107AC
SNOC:GY9Z970C
MAIN.CPP

main()
{
    string output;
    ifstream readExample;
    readExample.open("EXAMPLE.txt");  

    while(readExample.eof())
    {
        getline(readExample,output,':');
        cout << "* " << output <<endl; 
    }
}

我不知道为什么输出上会显示“*ABC”,我的逻辑是否有问题。或者我在while循环中漏掉了什么?提前感谢您帮助解决我的代码

首先,我假设
while
循环是
while(!readExample.eof())
,否则应该没有任何输出


第二,对于您的问题,第一个
getline(readExample,output,':')
将“ABC”读入
输出
,因此在下一行它输出
*ABC
,这正是您得到的。毫不奇怪。

用于
getline
delim
参数将替换新行的默认分隔符“\n”。 您当前获得的“线”是:

您可以做的更多事情是这样的(如果您的输出(如GY9Z970C)是固定大小的:

ssize_t len = getline(readExample,output,':');
cout << "* " << (char*)(output + (len - 8)) <<endl; 
ssize\u t len=getline(readExample,output,':');

coutOutput存储Example.txt中的第一个提取,然后打印*。在第一次迭代中
output=“ABC”在第二次迭代中
output=“JY9757AC”。我在while循环中添加了一个
getline()
,用于读取行中不需要的部分。我还添加了一个
字符串[]
,将提取的值存储在中

#include <fstream>
#include <string>


using namespace std;

int main()
{
    string output, notWanted, stringArray[3];
    int i = 0;
    ifstream readExample;
    readExample.open("EXAMPLE.txt");

    while (!readExample.eof())
    {
        getline(readExample, notWanted, ':');
        getline(readExample, output);
        cout << "* " << output << endl;
        stringArray[i++] = output;
    }
    cin.get();

    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串输出,不需要,字符串数组[3];
int i=0;
ifstream示例;
readExample.open(“EXAMPLE.txt”);
而(!readExample.eof())
{
getline(readExample,notwant,“:”);
getline(readExample,output);

嗨,罗宾,在我的程序上实现了你的代码。程序的输出产生了额外的一行“GY9Z970C”。这很奇怪,它对我来说不是这样。你确定它是一样的吗?是的,我复制了你的源代码,粘贴它并再次运行。仍然是一样的。(感觉很奇怪!哈哈)EXAMPLE.txt文件中没有意外的更改?没有,它与我在问题上发布的内容相同。
ssize_t len = getline(readExample,output,':');
cout << "* " << (char*)(output + (len - 8)) <<endl; 
#include <fstream>
#include <string>


using namespace std;

int main()
{
    string output, notWanted, stringArray[3];
    int i = 0;
    ifstream readExample;
    readExample.open("EXAMPLE.txt");

    while (!readExample.eof())
    {
        getline(readExample, notWanted, ':');
        getline(readExample, output);
        cout << "* " << output << endl;
        stringArray[i++] = output;
    }
    cin.get();

    return 0;
}