简体   繁体   English

C ++在带有数字的文本文件中读取

[英]C++ reading in a text file with numbers

I have a txt file with 16 lines of integers. 我有一个包含16行整数的txt文件。 Each line contains 5 integers, here are the first 4 lines of the file, 每行包含5个整数,这里是文件的前4行,

0 0 0 0 0 0 0 0 0 0

1 2 3 4 5 1 2 3 4 5

5 4 3 2 1 5 4 3 2 1

5 100 1000 10000 10500 5 100 1000 10000 10500

I need to read one line at a time, store all 5 ints in an array, then find the max value. 我需要一次读取一行,将所有5个整数存储在一个数组中,然后找到最大值。 I can do this on my own just fine. 我可以自己做就好了。 What I am having trouble with is going to the next line in the file after I finish reading the previous line. 我读完前一行后,我遇到的问题是进入文件的下一行。

  1. Read first line from text file 从文本文件中读取第一行

  2. store numbers in an array 将数字存储在数组中

  3. send array to a function that returns the largest integer in the array 将数组发送到返回数组中最大整数的函数

  4. place that returned number in a new array that will eventually contain the largest numbers from all 16 lines of the file 将数字返回到新数组中的位置,该数组最终将包含文件的所有16行中的最大数字

  5. read the next line of the file 阅读文件的下一行

  6. write the array with the 16 int array to a file 将带有16个int数组的数组写入文件

I am having trouble with step 5. Each time I run my code, it reads line 1 every time. 我在第5步遇到问题。每次运行我的代码时,它每次都会读取第1行。

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int n,i,j;
    int temp[5];
    //ask user
    cout<<"How many lines are in the file\n";
    cin>>n;
    int *array = new int[n];
    //open file
    ifstream inFile;
    inFile.open("input.txt");

    for(j=0;j<n;++j)
    {
        for(i=0;i<5;++i)
        {
            inFile >> temp[i];
            cout<<temp[i]<<" ";
        }
        cout<<"\n";
    }
    return 0;
}

As you can see, I don't have the find max value function yet, I want to fix how I am reading the file before I move on to analyzing the actual data. 正如您所看到的,我还没有找到最大值函数,我想在继续分析实际数据之前修复我如何读取文件。

And yes this is a homework project, due in a few days, but my teacher did not even explain reading in files and told us to read the explanation in the textbook (which didn't help for what we were doing). 是的,这是一个家庭作业项目,几天后,但我的老师甚至没有解释阅读文件,并告诉我们阅读教科书中的解释(这对我们正在做的事情没有帮助)。 I am looking to learn not just pass the homework, so a brief explanation/example of what I'm missing is fine. 我希望学习的不仅仅是通过作业,所以我错过了一个简短的解释/例子。

I would grab the entire line at once using using istream::getline() and then parse the resultant string. 我将使用istream :: getline()立即获取整行,然后解析结果字符串。 Then call getline() again, etc. As it is you're probably not reading in the endline character although why you're getting the exact behavior you are isn't quite clear to me. 然后再次调用getline(),等等。因为它是你可能没有阅读结束字符,虽然为什么你得到你的确切行为对我来说不是很清楚。

Your code works fine for me. 你的代码对我来说很好。 I believe there is a problem with opening a file, so it keeps outputting the temp[] elements as they were initialized. 我相信打开一个文件有一个问题,所以它在初始化时不断输出temp []元素。 Try to get the inFile state while reading. 尝试在阅读时获得inFile状态。

Well, I don't think you need an array. 好吧,我认为你不需要阵列。 Just read in line by line, tokenize it, static cast each to int (look at ascii values within the number range 0-9 for beingextra cautious, check for negatives etc.) and directly store the max. 只需逐行读取,对其进行标记,将每个静态转换为int(查看数字范围0-9内的ascii值,以便beingextra谨慎,检查否定等)并直接存储最大值。 Repeat till EOF and you would possibly have the most efficient solution. 重复直到EOF,您可能会有最有效的解决方案。

So the steps would be 所以步骤将是

1) Declare a temporary string. 1)声明一个临时字符串。

2) Read in a line 2)读一行

3) Tokenize on the space 3)对空间进行标记

4) Assume the first is the max. 4)假设第一个是最大值。 Continue reading and comparing. 继续阅读和比较。 Swap the values if you find a larger one 如果找到较大的值,请交换值

5) Repeat till the end 5)重复直到结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM