簡體   English   中英

如何正確使用ifstream從輸入文件讀取數據?

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

我已經在這個問題上進行了多次搜索。 我只是回到編程,我試圖創建一個簡單的代碼來開始。

我目前正在使用g ++編譯我的文件。 我正在使用gedit鍵入我的代碼和輸入文件。

基本上,我想從我的輸入文件(單獨的整數列表)中讀取數據,在文本文件中找到整數的總數,然后將它們保存到數組中。

以下是在我的主文件中使用的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;


}

主文件

#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;
}

輸入文件

1 
2
5
9

我認為的問題是線

inputfile >> n;

這是提供給其他人的解決方案,這是我在課堂上使用的解決方案,但是由於某種原因,文本文件無法正確讀取,因此我必須做錯了什么。

當我使用以下代碼時,我設置了一個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

如您所見,它僅計數1個條目,然后當我檢查以查看存儲在數組中的數字時,它將存儲2 ,這是文本文件中的第二個數字。 它只是完全跳過了第一個條目。

如果您想知道,實際的out_2.txt文件包含

There are (1) numbers in the input file

我也試過了

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

但這又帶來了其他問題,該問題總共計算了9個條目,並且數組中存儲的數字是大整數,這是不正確的。

任何幫助表示贊賞。 謝謝

INPUTREAD函數采用char *參數。 您正在傳遞文字字符串“ in_2.txt”。 文字字符串是const char * ,這是編譯器的警告告訴您的內容。

如您所見,它僅計入1個條目

文件中的第一個數字是1,所以

inputfile >> n;

將“ 1”讀入n

然后,當我檢查它存儲在數組中的數字時,它存儲了2,這是文本文件中的第二個數字。

正確,那是文件中的下一個數字。 在上面的語句讀取“ 1”之后,代碼期望在文件中看到一個數字,然后開始讀取。 下一個,也是代碼唯一讀取的數字是“ 2”,這是您看到的內容。

它只是完全跳過了第一個條目。

不,不是。 它被讀過

inputfile >> n;

您需要添加

4

到文件開頭,表示緊隨其后的是四個數字。 該代碼讀取文件中有多少個數字的計數,然后繼續讀取這些數字。 這就是您編碼程序要執行的操作,而這正是它在執行的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM