Ebrahim Ghasemi
Ebrahim Ghasemi

Reputation: 6126

Reading integer from a file in c++

I have a text file named in.txt that has an integer in each line. The first line contains the number of lines minus 1 (=the number of integers). I want to read this integers into an array. So I wrote the below program :

#include <iostream>
#include <fstream>
int main(int argc, char * argv[])
{
    std::fstream myfile("in.txt");

    int number;
    int counter=0;
    myfile >> number;
    int inputArray[number];

    for(counter=0;counter=number;counter++)
    {
        myfile>>inputArray[counter];
    }

    for(counter=0;counter=number;counter++){
        printf("%i",inputArray[counter]);
    }
    return 0;
}

Q1 : When I run it, nothing happens! A black screen appears and remains empty. What is wrong?

Q2 : If the input file was really big (Contains about 2^27 integers), is this program a good idea, or I must do something else?

Upvotes: 2

Views: 5873

Answers (3)

Baum mit Augen
Baum mit Augen

Reputation: 50111

There are several things wrong with your code:

First of all, you fail to check if your input operations succeed. Do something like

if (! (myfile >> number)) {
       std::cerr << "Reading number failed!\n";
       return -1;
}

for every input operation. Else, you might use garbage values which might lead to strange and hard to find bugs.

Next, there are no variable sized arrays in C++, use std::vector<int> inputArray(number); instead. This will initialize the whole array to 0 which might cost some time, you could change your code to

std::vector<int> inputArray;
inputArray.reserve(number);
for(int64_t counter=0;counter<number;counter++)
{
    int temp;
    myfile>>temp;
    inputArray.push_back(temp);
}

to avoid that.

Last, your loop conditions are wrong. They should be

for(counter=0;counter<number;counter++)

If the number of numbers might be large, you should use a fixed sized integer like int64_t for you counters and numberinstead.

Is this a good idea? Well, it is an rather efficient way to read the numbers, but reading 2^27 numbers will always take a lot of time. If you can avoid doing this, it is your biggest opportunity to speed things up.

As a last remark, here is how I would write the input operation with error handling and all:

for (int temp; myFile >> temp;)
    inputArray.push_back(temp);

if( inputArray.size() != number) 
    std::cerr << "Number of numbers read did not match number in first line!\n";

Upvotes: 4

Omar Natour
Omar Natour

Reputation: 111

  1. 1. You have to create a dynamic array to be able to choose it's size while executing the program
    1. An error in logical operator
    2. It's better to put weather you want an input or an output file when declaring my file

try this :

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

int main(int argc, char * argv[])
{
    std::fstream myfile("in.txt", ios::in | ios::out);

    int number;
    int counter = 0;
    myfile >> number;
    int *inputArray = new int[number];

    for (counter = 0; counter != number; counter++){
        myfile >> inputArray[counter];
    }

    for (counter = 0; counter != number; counter++){
        printf("%i", inputArray[counter]);
    }
    delete[] inputArray;
    return 0;
}

I think binary files are better for huge amount of data.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206717

Looks like there is a typo in this line:

for(counter=0;counter=number;counter++)

That should be:

for(counter=0; counter < number; counter++)

You have the same error in the next for loop.

Upvotes: 2

Related Questions