Search code examples
c++objective-cclassifstreamreadfile

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. I am using gedit to type my code and input file.

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.

//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

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. It just skipped the first entry completely.

In case you were wondering, the actual out_2.txt file has

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.

Any help is appreciated. Thanks


Solution

  • The INPUTREAD function takes a char * parameter. You are passing "in_2.txt", a literal string. Literal string are const char *, which is what the compiler's warning is telling you.

    As you can see it is only counting 1 entry

    The first number in the file is 1, so

    inputfile >> n;
    

    Read "1" into 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.

    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. The next, and the only number the code reads is "2", which is what you see.

    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.