C++从文件中读取

发布于 2024-12-28 05:55:59 字数 376 浏览 4 评论 0原文

如果我知道第一个值后面有多少个 int 值,但我不知道它们后面有多少个值,我应该如何读取此文件。我的问题是当我必须读取包含 2 个值的行时。

编辑:这是我的文件

5
27
15
42
17
35
20 1
28 2
43 3

这是我尝试过的:

fin >> n;
for (i=1; i<=n; i++)
    fin >> part[i];

while(!fin.eof())
{
    nrT++;
    fin >> tric[nrT][0];
    fin >> tric[nrT][1];
}

How should I read this file if I know how many int values are after the first value but I don't know how many values are after them. My problem is when I have to read the lines that contains 2 values.

Edit: This is my file

5
27
15
42
17
35
20 1
28 2
43 3

Here is what I tried:

fin >> n;
for (i=1; i<=n; i++)
    fin >> part[i];

while(!fin.eof())
{
    nrT++;
    fin >> tric[nrT][0];
    fin >> tric[nrT][1];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我做我的改变 2025-01-04 05:55:59
for (int a,b; fin >> a >> b; nrT++)
{
    tric[nrT][0] = a;
    tric[nrT][1] = b;
}
for (int a,b; fin >> a >> b; nrT++)
{
    tric[nrT][0] = a;
    tric[nrT][1] = b;
}
动次打次papapa 2025-01-04 05:55:59

像对待空格字符一样对待行尾字符,并尝试解析不属于其中任何一个的所有内容。如果您使用Unicode 感知正则表达式,那么已经有一个小组可以执行此操作。

Treat an end of line character the same as you would a space character, and try to parse everything that's not either of those. If you use Unicode aware regular expressions there's already a group which does this.

终止放荡 2025-01-04 05:55:59

查看字符串流。 (我不知道我脑子里的代码)。它们类似于 cin,但您可以使用文件中的数据来初始化它们。然后您可以使用>>运算符从中提取数据,并且它知道如何提取整数值。

Look into string streams. (I don't know the code off the top of my head). They are like cin, but you can initialize them with data from a file. Then you can use the >> operator to extract data from it and it knows how to pull out the integer value.

圈圈圆圆圈圈 2025-01-04 05:55:59

首先,不要使用 fin.eof()。该功能只有在阅读后才有用
已经失败了。但是,类似以下内容应该可以工作:

fin >> n;
if ( !fin || n > size( part ) ) {
    //  Error...
}
for ( i = 0; i != n; ++ i ) {
    fin >> part[i];
    if ( !fin ) {
        //  Error...
    }
}
while ( nrT < size( tric ) && fin >> tric[nrT][0] >> tric[nrT][1] ) {
    ++ nrT;
}

为了更好地验证,您可能需要逐行读取文件,并且
从该行中提取一个或两个元素,具体取决于您所在的位置
文件。另一个改进是使用 std::vector
容器和推回——这样,您就不需要任何边界
检查。

First, don't use fin.eof(). That function is only useful after a read
has failed. Something like the following should work, however:

fin >> n;
if ( !fin || n > size( part ) ) {
    //  Error...
}
for ( i = 0; i != n; ++ i ) {
    fin >> part[i];
    if ( !fin ) {
        //  Error...
    }
}
while ( nrT < size( tric ) && fin >> tric[nrT][0] >> tric[nrT][1] ) {
    ++ nrT;
}

For better validation, you might want to read the file line by line, and
extract one or two elements from the line, depending on where you are in
the file. Another improvement would be to use std::vector for the
containers, and push-back—that way, you don't need any bounds
checking.

走过海棠暮 2025-01-04 05:55:59

逐行阅读,寻找行中的空格。如果有,则使用方法子字符串。最后将得到的子串转换为整数。互联网上有大量示例:

substring

字符串到整数转换

希望这有帮助!

Read line by line, the look for spaces in the line. If there are, use the method substring. Finally, convert the substrings obtained to integers. There are tons of examples on the internet:

substring

String to integer conversion

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文