Display junk values

hi guys i am trying to work on the file and array at the same time. first i am trying to open the file and count how many numbers of contents are there in the file and again open the same file and output the contents in the file. but my program showing me a junk value. here i am going to paste my program. Thanks in advance.


#include <iostream>
#include <fstream>

using namespace std;

void GetNumbers(ifstream& ,int *p, int size);

int main()
{
int i, x, size;
int *ptr;

ifstream inData;

inData.open("BigNumbers.dat",ios::in);

if(!inData)
{
cout << "Error. File can't open." << endl;
return 1;
}

size = 0;

while(!inData.eof())
{
inData >> x;
size = size + 1;
}

inData.close();

inData.open("BigNumbers.dat",ios::in);

ptr = new int[size];

GetNumbers(inData,ptr,size);


delete [] ptr;

inData.close();

return 0;
}

void GetNumbers(ifstream& inData,int *p, int size)
{
int i, temp;
char ch;

for(i=0;i<size;i++)
{
inData >> p[i];
cout << p[i];

}
cout << endl;


}

You have an off by one error in your program. You are setting size to 1 more than the count of numbers available in the file. Change the above code to the following:

1
2
3
4
5
6
inData >> x;
while(!inData.eof())
{
size = size + 1;
inData >> x;
}
Or you could say:

while(inData >> x) ++size;

But I don't see any reason to avoid using std::vector.
thankyou naivnomore. i changed that one like you said but still showing me junk values.
I tested with a sample file with my suggested changes and it works. Please note you are outputting numbers with no spaces between them. Therefore, all the numbers will be clubbed together & will appear as one long string. Is this the case? If not, please share us your input file and the output you get for us to figure out what is going on.
actually i want to show my output as same as file one. i am going to paste the input file.
This is my input file.

7
9
2 4
7 6
3 5 7 8 5 3 4
5 4 1 6
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
4 4 4 4 4 4 4 4 4 4 4 4 4 4
Following is the output I received for your input file:

79247635785345416222222222222222222222222277777777777777777777777779999999999999999999999999999999999999999999999999988888888888888888888888884444444444444

Is this what you are getting as output? If so, this is correct.
yes that's what i want. so now where do i need to fix the code to get the result like yours. thank you so much naivnomore. i really appreciated.
I am not quite sure I understand your response. The fix that I suggested on my first post was the only fix I made to your code to get the above result. I am re-posting that code here for your reference.

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
#include <iostream>
#include <fstream>

using namespace std;

void GetNumbers(ifstream& ,int *p, int size);

int main()
{
int i, x, size;
int *ptr;

ifstream inData;

inData.open("bignum.dat",ios::in);

if(!inData)
{
cout << "Error. File can't open." << endl;
return 1;
}

size = 0;

inData >> x;
while(!inData.eof())
{
size = size + 1;
inData >> x;
}

inData.close();
inData.open("bignum.dat",ios::in);

ptr = new int[size];

GetNumbers(inData,ptr,size);


delete [] ptr;

inData.close();

return 0;
}

void GetNumbers(ifstream& inData,int *p, int size)
{
int i, temp;
char ch;

for(i=0;i<size;i++)
{
inData >> p[i];
cout << p[i];

}
cout << endl;


}
Why don't you use a std::vector?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    std::vector<int> vec;
    ifstream ifs("bignum.dat");
    int n;
    while(ifs >> n) vec.push_back(n);
    for(unsigned int i = 0; i < vec.size(); ++i) std::cout << vec[i];

    return 0;
}

Notice you don't need to call open() and close() for the ifstream because the constructor will take the file name and open it for you, while the destructor will close it for you. Also, avoid looping on eof() because there are other states the stream might go into (bad() and fail()) which you're not checking.
Last edited on
Topic archived. No new replies allowed.