从文件中读取字符并写入另一个文件。

时间:2023-02-06 08:06:36

I am quite new in c++ and programming so sorry in advance in my question repeats. I have a text file of 3 lines:

我在c++和编程方面都是新手,所以在我的问题重复出现之前很抱歉。我有一个3行的文本文件:

7

00000000000000000000000*0000

00000000000000000000000 * 0000

0 0 0 R 0

0 0 0 R 0。

What I need to do is read 2nd line and write it into an array as char. But I must not include 3rd line because it will go to a completely different matrix. My code so far :

我需要做的是读第二行,然后把它写成char。但我不应该包括第三行因为它会变成一个完全不同的矩阵。我的代码到目前为止:

ifstream input;
input.open("input1.txt");
input >> start;
char a=0;
string line;
while (getline(input, line))
{
    a=0;
    istringstream iss(line);
    int length = line.size();
    for (int i=0; i<length; i++)
    {
        iss >> a;
        A[i] = a;
        cout << A[i] << " " << i << endl;
    }
}
input.close();

However, with this code it always starts new array for 3rd line. What am I doing wrong? What is the easiest way to fix it? Thank you.

然而,使用此代码,它总是为第三行启动新的数组。我做错了什么?解决这个问题最简单的方法是什么?谢谢你!

-----------------------------Update--------------------------------------

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - update - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

I have modified the code but it still does not work properly. I am getting this kind of result : 5)-└ instead of correct one. My current code:

我已经修改了代码,但是仍然不能正常工作。我得到这样的结果:5)——└而不是正确的一个。我现在的代码:

void Read(int &numLines, int &start, vector<char>&A, char B[][5])
{
ifstream input;
input.open("input.txt");
input >> start;
input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
string line;
if(getline(input, line))
{
    for(char temp: line)
    {
        A.push_back(temp);
    }
}
input.close();

}

}

A here is a vector I want to write 2nd line to, char by char Start is just an integer in which I am storing 1st line (7)

这是一个向量我要写第二行,char开始是一个整数我存储第一行(7)

Thank you very much for advices

非常感谢你的建议。

2 个解决方案

#1


0  

Mixing >> and std::getline is non-trivial. For example, after input >> start; the end of line marker is left in the stream in case it's still needed. In your case it isn't, and it is picked off by the subsequent call to getline, resulting in a read of an empty line.

混合>>和std::getline是非平凡的。例如,输入>>开始;行标记的结束留在流中,以防它仍然需要。在您的情况中,它不是,并且它被随后的getline调用所捕获,从而导致读取空行。

This is what's complicating your read of line and forcing the while loop and test for empty lines.

这是使您的代码读起来更加复杂的原因,并迫使while循环和测试空行。

Step through your program with your development environment's debugger and you'll see what I'm talking about. Get used to using the debugger. It's possibly the best programming productivity tool you'll ever encounter.

通过开发环境的调试器逐步完成您的程序,您将看到我所谈论的内容。习惯使用调试器。它可能是您所遇到的最好的编程效率工具。

The easiest way to fix it is to place

修复它的最简单的方法是放置。

input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

after

input >> start;

to eat up the end of the line (and anything else that might be on that line. This needs the addition of #include<limits> to get std::numeric_limits<std::streamsize>::max.

把线的末端吃完(还有其他任何可能出现在这条线上的东西)。这需要添加#include <限制> 来获得std::numeric_limit <:streamsize> ::max。

Then you can remove the while loop and replace it with

然后您可以删除while循环并替换它。

if (getline(input, line))

No loop, not chance of consuming multiple lines from the file. And the logic for reading and processing the third line can follow.

没有循环,不可能从文件中使用多行。阅读和处理第三行的逻辑也可以遵循。

Side note: instead of that for loop, consider

附注:考虑一下,而不是循环。

int i = 0;
while (iss >> a)
{
    A[i] = a;
    cout << A[i] << " " << i << endl;
    i++;
}

This will march through iss until it hits the end of the line. You can also throw iss out entirely and just read the characters directly out of line.

这将穿过国际空间站直到它到达终点。你也可以把iss完全扔出去,然后直接从直线上读取字符。

int i = 0;
for(char temp: line)
{
    A[i] = temp;
}

And A should probably be a vector if it isn't already to reduce the chances of buffer overruns.

如果它还没有减少缓冲区溢出的机会,那么A应该是一个向量。

for(char temp: line)
{
    A.push_back(temp);
}

#2


0  

I would go with something like this:

我会这样做:

std::string start;
std::string Astring;
ifstream input;
input.open("input.txt");
input >> start;
input >> Astring;

// If you really want a char array
char * A = new char[Astring.size()];
for (unsigned int i = 0; i < Astring.size(); i++) {
    A[i] = Astring[i];
}
// Don't forget to delete after use
delete[] A;

Moreover, if you just need the char array as an input to something else later, you can call Astring.c_str() instead of that for loop, which returns a C-style char array.

此外,如果您只是需要将char数组作为其他内容的输入,您可以调用astr. c_str()而不是for循环,它返回一个c样式的char数组。

#1


0  

Mixing >> and std::getline is non-trivial. For example, after input >> start; the end of line marker is left in the stream in case it's still needed. In your case it isn't, and it is picked off by the subsequent call to getline, resulting in a read of an empty line.

混合>>和std::getline是非平凡的。例如,输入>>开始;行标记的结束留在流中,以防它仍然需要。在您的情况中,它不是,并且它被随后的getline调用所捕获,从而导致读取空行。

This is what's complicating your read of line and forcing the while loop and test for empty lines.

这是使您的代码读起来更加复杂的原因,并迫使while循环和测试空行。

Step through your program with your development environment's debugger and you'll see what I'm talking about. Get used to using the debugger. It's possibly the best programming productivity tool you'll ever encounter.

通过开发环境的调试器逐步完成您的程序,您将看到我所谈论的内容。习惯使用调试器。它可能是您所遇到的最好的编程效率工具。

The easiest way to fix it is to place

修复它的最简单的方法是放置。

input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

after

input >> start;

to eat up the end of the line (and anything else that might be on that line. This needs the addition of #include<limits> to get std::numeric_limits<std::streamsize>::max.

把线的末端吃完(还有其他任何可能出现在这条线上的东西)。这需要添加#include <限制> 来获得std::numeric_limit <:streamsize> ::max。

Then you can remove the while loop and replace it with

然后您可以删除while循环并替换它。

if (getline(input, line))

No loop, not chance of consuming multiple lines from the file. And the logic for reading and processing the third line can follow.

没有循环,不可能从文件中使用多行。阅读和处理第三行的逻辑也可以遵循。

Side note: instead of that for loop, consider

附注:考虑一下,而不是循环。

int i = 0;
while (iss >> a)
{
    A[i] = a;
    cout << A[i] << " " << i << endl;
    i++;
}

This will march through iss until it hits the end of the line. You can also throw iss out entirely and just read the characters directly out of line.

这将穿过国际空间站直到它到达终点。你也可以把iss完全扔出去,然后直接从直线上读取字符。

int i = 0;
for(char temp: line)
{
    A[i] = temp;
}

And A should probably be a vector if it isn't already to reduce the chances of buffer overruns.

如果它还没有减少缓冲区溢出的机会,那么A应该是一个向量。

for(char temp: line)
{
    A.push_back(temp);
}

#2


0  

I would go with something like this:

我会这样做:

std::string start;
std::string Astring;
ifstream input;
input.open("input.txt");
input >> start;
input >> Astring;

// If you really want a char array
char * A = new char[Astring.size()];
for (unsigned int i = 0; i < Astring.size(); i++) {
    A[i] = Astring[i];
}
// Don't forget to delete after use
delete[] A;

Moreover, if you just need the char array as an input to something else later, you can call Astring.c_str() instead of that for loop, which returns a C-style char array.

此外,如果您只是需要将char数组作为其他内容的输入,您可以调用astr. c_str()而不是for循环,它返回一个c样式的char数组。