Read integers from a text file with C++ ifstream

So i have text file input.txt. This file consists of 54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15, 29, 23, 83, 95, 25, 91, 33, 47, 24, 62, 13, 42, 73, 44, 78, 72, 7, 5, 10, 48, 71, 18, 39, 97, 64, 79, 51, 74, 31, 37, 57, 30, 94, 80, 28, 1, 56, 85, 46, 100, 82, 40, 26, 21, 68, 43, 14, 3, 65, 99, 89, 52, 84, 36, 2, 6, 11, 50.
Goal is to put these numbers in massive and output from 1-100.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 #include<iostream>
#include<fstream>

using namespace std;

int main()
{


    const int size = 100;
    int array[size];


    ifstream file("input.txt");

    int count = 0;
    int x;


    while (count < size && file >> x)
        array[count++] = x;


    for (int i = 0; i < count; i++)
        cout << array[i] <<' ';

}
frog1990 wrote:
Goal is to put these numbers in massive


Pardon?
i meant array
frog1990 wrote:
i meant array

Well, you've put them into an array. What do you want to do with that?
If you mean obliterate the commas and then sort them, then you could try this. (The stringstream is only there to simulate your input file).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

istringstream file( "54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, "
                    "61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, "
                    "20, 22, 69, 77, 86, 35, 19, 32, 49, 15, 29, 23, 83, 95, 25, 91, "
                    "33, 47, 24, 62, 13, 42, 73, 44, 78, 72, 7, 5, 10, 48, 71, 18, 39, 97, "
                    "64, 79, 51, 74, 31, 37, 57, 30, 94, 80, 28, 1, 56, 85, 46, 100, 82, 40, "
                    "26, 21, 68, 43, 14, 3, 65, 99, 89, 52, 84, 36, 2, 6, 11, 50" );

int main()
{
   const int size = 100;
   int arr[size];
// ifstream file("input.txt");

   int counter = 0;
   char comma;
   if ( file >> arr[0] ) counter++;
   for ( ; counter < size && file >> comma >> arr[counter]; counter++ );
 
   sort( arr, arr + counter );
   for ( int i = 0; i < counter; i++ ) cout << arr[i] << ' ';
}

Last edited on
Hello frog1990,

One thing I would like to point out. On line 13 you define a file stream variable and open the file. But how do you know that the file is open and usable?

Being an "ifstream" the file may not open, but the program continues as if nothing is wrong.

You may want to do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    const std::string inFileName{ "input1.txt" };  // <--- Put File name here.

    std::ifstream inFile(inFileName);

    if (!inFile)
    {
        std::cout << "\n File " << std::quoted(inFileName) << " did not open." << std::endl;
        //std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;

        return 1;
    }

I did this first because if the file does not open there is no need to continue with the program as there will be nothing read.

Over time I settled on the variable names "inFile" and "outFile", but you are free to choose whatever name you like.

And for the while loop:
1
2
3
4
5
6
while (count < size && inFile >> x)
{
    array[count++] = x;

    inFile >> junk;
}

Line 5 eats the "," until the rhs of && fails, (setting the "eof" bit), ending the while loop and reading all the numbers. Even with lastchance's code it misses the last number, sorry about that lastchance.

The first output may look like this:

 File "input1.txt" did not open.


I altered the file name so the if statement would work.

When the file does open I set up the output to get this:

   1    2    3    4    5    6    7    8    9   10
  11   12   13   14   15   16   17   18   19   20
  21   22   23   24   25   26   27   28   29   30
  31   32   33   34   35   36   37   38   39   40
  41   42   43   44   45   46   47   48   49   50
  51   52   53   54   55   56   57   58   59   60
  61   62   63   64   65   66   67   68   69   70
  71   72   73   74   75   76   77   78   79   80
  81   82   83   84   85   86   87   88   89   90
  91   92   93   94   95   96   97   98   99  100



Andy
Even with lastchance's code it misses the last number, sorry about that lastchance.


NO. It doesn't. Using lastchance's code, I get displayed:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 5
7 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

istringstream file("54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, "
	"61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, "
	"20, 22, 69, 77, 86, 35, 19, 32, 49, 15, 29, 23, 83, 95, 25, 91, "
	"33, 47, 24, 62, 13, 42, 73, 44, 78, 72, 7, 5, 10, 48, 71, 18, 39, 97, "
	"64, 79, 51, 74, 31, 37, 57, 30, 94, 80, 28, 1, 56, 85, 46, 100, 82, 40, "
	"26, 21, 68, 43, 14, 3, 65, 99, 89, 52, 84, 36, 2, 6, 11, 50");

int main()
{
	const int size = 100;
	int arr[size];
	// ifstream file("input.txt");

	int counter = 0;
	char comma;
	if (file >> arr[0]) counter++;
	for (; counter < size && file >> comma >> arr[counter]; counter++);

	sort(arr, arr + counter);
	for (int i = 0; i < counter; i++) cout << arr[i] << ' ';
}

Last edited on
@seeplus and @lastchance,

You are right. My apologies. The first time I ran it it did not come out right. Opon further testing it worked right.

Andy
Last edited on
No worries!

I'm still not sure what the OP actually wants to do. After all, I can think of easier ways of writing out the numbers 1 - 100.
Last edited on
I suspect it's an exercise from somehere.
Mmm, probably right.

It has apparently made it to Chegg:
https://www.chegg.com/homework-help/questions-and-answers/need-writing-c-language-programm-need-start-include-prepare-text-file-dataintxt-following--q53387382

Start solving Q2b in that link.
(Oh, and Handy Andy appears to have correctly guessed the purport of Q3 and Q4).
Last edited on
Nice to know I did something right without even knowing about it.

My initial problem may be due to my computer needing a restart. It seems to be running sloooow.

Andy
There's no need to treat the first number differently from the rest. All numbers can be assumed to be followed by a ,. As the last number isn't all that will happen is that when this non-existent comma comes to be read, the stream will enter a fail state - which then doesn't matter. Also as we know the numbers are from 1 to 100 inclusive, we don't need to sort the array - just use the number as the array index.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

istringstream file("54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, "
	"61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, "
	"20, 22, 69, 77, 86, 35, 19, 32, 49, 15, 29, 23, 83, 95, 25, 91, "
	"33, 47, 24, 62, 13, 42, 73, 44, 78, 72, 7, 5, 10, 48, 71, 18, 39, 97, "
	"64, 79, 51, 74, 31, 37, 57, 30, 94, 80, 28, 1, 56, 85, 46, 100, 82, 40, "
	"26, 21, 68, 43, 14, 3, 65, 99, 89, 52, 84, 36, 2, 6, 11, 50");

int main()
{
	const int size {100};
	int arr[size] {};
	// ifstream file("input.txt");

	int counter {-1};
	char comma {};

	for (int v {}; ++counter < size && file >> v && v > 0 && v <= size; arr[v - 1] = v, file >> comma);
	std::copy_n(arr, counter, std::ostream_iterator<int>(std::cout, " "));
}

Yes, that's a good point. I didn't need special treatment for one less comma.

I only sorted then because I didn't (at the time) have the faintest idea what the OP was really intending to do with his/her array.

If the Chegg link is to be believed then all that is apparently needed is to write the array forward and backward, but I seem to be running up against OPs providing rather mangled instructions tonight.
Topic archived. No new replies allowed.