简体   繁体   English

如何正确使用ifstream从输入文件读取数据?

[英]How do I read data from an input file using ifstream correctly?

I've already done multiple searches on this issue. 我已经在这个问题上进行了多次搜索。 I'm just getting back to programming and I'm trying to create a simple code just to get started. 我只是回到编程,我试图创建一个简单的代码来开始。

I'm currently using g++ to compile my files. 我目前正在使用g ++编译我的文件。 I am using gedit to type my code and input file. 我正在使用gedit键入我的代码和输入文件。

Basically I want to read data from my input file (list of separate integers), find the total number of integers in the text file, then save them into an array. 基本上,我想从我的输入文件(单独的整数列表)中读取数据,在文本文件中找到整数的总数,然后将它们保存到数组中。

The following is a HEADER file to be used in my main file. 以下是在我的主文件中使用的HEADER文件。

//Make program to read data from in_2.txt then add them and print to output file out_2.txt

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <iomanip>

std::ofstream outputfile("out_2.txt", std::ios::out);

class TEST_ADD{
    public:
        TEST_ADD(void); //constructor. Will use an unknown input file so nothing to do

        void INPUTREAD(char *); // Read the data from the input file. Ex.                       //b.INPUTREAD ("in.2")

        void ADD(void); //Will perform the actual summation and store value into int add


        void PRINT(void); // Print the numbers being summed, along with it's sum into out.2

    private:
        int add; //will store the sum of the numbers
        int n; //will keep track of total number of entries
        int A[20]; //used to store the number from the input file into an array

};

TEST_ADD::TEST_ADD (void)

{
    n=0;
}

void TEST_ADD::INPUTREAD (char * in_name) //will record the users input file (written in main.cpp)
                       //and store it as in_name

{
    int i;
    std::ifstream inputfile(in_name, std::ios::in);
    std::cout << "Number of n before input file read : "<< n << std::endl;

    inputfile >> n;

    /*while(!inputfile.eof()) 
    {
            inputfile >> n;
        //std::cout << std::setw(10) << n;
    }*/

    std::cout << "Number of n after input file read : " << n << std::endl;

    std::cout <<  "There are ("<< n << ") numbers in the input file" << std::endl; //check

    for(i=0;i<n;i++)
    {
        inputfile >> A[i];
        std::cout << "A[" << i << "] = " << A[i]<< std::endl;
    }

    outputfile << "There are ("<< n << ") numbers in the input file" << std::endl;


}

Main File 主文件

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "Project2.h"

int main()
{
    TEST_ADD b;
    b.INPUTREAD("in_2.txt");

    return 0;
}

Input File 输入文件

1 
2
5
9

The issue I think is the line 我认为的问题是线

inputfile >> n;

This was the solution given to others, and this is what I used to use in class, but for some reason the text file isn't being read correctly so I must be doing something wrong. 这是提供给其他人的解决方案,这是我在课堂上使用的解决方案,但是由于某种原因,文本文件无法正确读取,因此我必须做错了什么。

When I use the following, I set up a cout statement to test my code and this is what I got 当我使用以下代码时,我设置了一个cout语句来测试我的代码,这就是我得到的

jason@jason-Satellite-S55-B:~/C++/Second_program$ g++ main2.cpp -o project2
main2.cpp: In function ‘int main()’:
main2.cpp:10:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
  b.INPUTREAD("in_2.txt");
                        ^
jason@jason-Satellite-S55-B:~/C++/Second_program$ ./project2
Number of n before input file read : 0
Number of n after input file read : 1
There are (1) numbers in the input file
A[0] = 2

As you can see it is only counting 1 entry and then when I check to see what number it stored in the array, it stores 2 which is the second number in the text file. 如您所见,它仅计数1个条目,然后当我检查以查看存储在数组中的数字时,它将存储2 ,这是文本文件中的第二个数字。 It just skipped the first entry completely. 它只是完全跳过了第一个条目。

In case you were wondering, the actual out_2.txt file has 如果您想知道,实际的out_2.txt文件包含

There are (1) numbers in the input file

I also tried the line 我也试过了

while(!inputfile.eof()) 
    {
            inputfile >> n;
        //std::cout << std::setw(10) << n;
    }

But that just brought up other problems where it counted 9 entries total, and the numbers stored in the array were large integers which aren't correct. 但这又带来了其他问题,该问题总共计算了9个条目,并且数组中存储的数字是大整数,这是不正确的。

Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

The INPUTREAD function takes a char * parameter. INPUTREAD函数采用char *参数。 You are passing "in_2.txt", a literal string. 您正在传递文字字符串“ in_2.txt”。 Literal string are const char * , which is what the compiler's warning is telling you. 文字字符串是const char * ,这是编译器的警告告诉您的内容。

As you can see it is only counting 1 entry 如您所见,它仅计入1个条目

The first number in the file is 1, so 文件中的第一个数字是1,所以

inputfile >> n;

Read "1" into n . 将“ 1”读入n

and then when I check to see what number it stored in the array, it stores 2 which is the second number in the text file. 然后,当我检查它存储在数组中的数字时,它存储了2,这是文本文件中的第二个数字。

Correct, that's the next number in the file. 正确,那是文件中的下一个数字。 After the above statement read the "1", the code expects to see one number in the file, and starts reading. 在上面的语句读取“ 1”之后,代码期望在文件中看到一个数字,然后开始读取。 The next, and the only number the code reads is "2", which is what you see. 下一个,也是代码唯一读取的数字是“ 2”,这是您看到的内容。

It just skipped the first entry completely. 它只是完全跳过了第一个条目。

No, it didn't. 不,不是。 It was read by 它被读过

inputfile >> n;

You need to add 您需要添加

4

to the beginning of the file, indicating that four numbers follow. 到文件开头,表示紧随其后的是四个数字。 The code reads the count of how many numbers are in the file, then proceeds to read those numbers. 该代码读取文件中有多少个数字的计数,然后继续读取这些数字。 That's what you coded your program to do, and that's exactly what it's doing, here. 这就是您编码程序要执行的操作,而这正是它在执行的操作。

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

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