首页 > 解决方案 > 从 ifstream 中读取整数。在将 ifstream 传递给函数时

问题描述

我正在开发一个程序,我需要将文件中的整数读入数组并使用数组来工作。不是超级复杂,应该几乎没有时间。

我仍然是一名学习 C++ 的学生,我已经用尽了所有的选择,试图让它按照我的理解发挥作用。

文本文件有 200 行,每行包含一个 int,后跟一个 '\n' 换行符。

当我将 main 函数中的文件读入 int 时,它按预期工作。当我通过引用将 ifstream 传递给执行完全相同的 for 循环的函数时,它会中断给我一个未初始化的 in 作为唯一值。

#include<iostream> // required
#include<fstream>
#include<random>
#include<string>


using namespace std; // using standard namespace as for this entire program

void readNumbers(ifstream&, int[], int); // passing a input filestream by reference, c style array, and an integer for the array length
int totalInts(ifstream&); // this will read how many lines the file has
int main()
{
    ifstream is("numbers.txt"); // this is the text file that we are reading from
    int ar[200]; // the array of length 200 that everything will be read into
    int temp; // temporary holding for our ints
    

    if (!is) // error checking
        cout << "somethings <expletive deleted>";
    else {

        for (int i = 0; i < 200; i++) // iterating through the file 200 times and inputting the int into the temp holding, this is being used to test if this method works as expected.
        {
            is >> temp;
            cout << i << ' ' << temp << '\n'; // prints it out for troubleshooting
            

        }
    //  cout << totalInts(is);
        readNumbers(is, ar, 200);


    }
    return 0;

}
int totalInts(ifstream& file)
{
    string i;
    int intNum;
    intNum = 0;

    while (getline(file, i))
    {
        intNum++;
    }
    return intNum;

}
void readNumbers(ifstream &reading, int papi[], int rayLength) // where we really want to read the file
{
    int temp; // for temp holding while we troubleshoot
    if (!reading)
        cout << "somethings fucked";
    else
    {
        for (int i = 0; i < rayLength;i++) // reads the file for as many time as we have space in the array.
        {
            reading >> temp; // reads into temp
            cout << temp; // outputs for troubleshooting

        }
    }



}

下面的程序输出

0 41
1 485
2 340
3 526
4 188
5 739
6 489
7 387
8 988
9 488
10 710
11 173
12 304
13 843
14 970
15 491
16 997
17 953
18 831
19 441
20 423
21 618
22 905
23 153
24 292
25 394
26 438
27 734
28 737
29 914
30 452
31 747
32 785
33 549
34 870
35 931
36 692
37 325
38 52
39 903
40 731
41 834
42 353
43 363
44 690
45 668
46 156
47 718
48 281
49 874
50 572
51 671
52 694
53 789
54 57
55 871
56 731
57 750
58 556
59 778
60 328
61 38
62 212
63 843
64 288
65 136
66 49
67 950
68 283
69 670
70 473
71 828
72 905
73 735
74 394
75 365
76 21
77 132
78 417
79 551
80 648
81 635
82 108
83 973
84 774
85 851
86 970
87 383
88 944
89 334
90 960
91 471
92 650
93 334
94 542
95 559
96 134
97 84
98 951
99 557
100 837
101 146
102 643
103 687
104 726
105 939
106 990
107 308
108 704
109 408
110 26
111 773
112 950
113 91
114 276
115 834
116 803
117 588
118 102
119 528
120 10
121 303
122 170
123 654
124 377
125 791
126 678
127 589
128 35
129 64
130 377
131 151
132 957
133 745
134 979
135 433
136 138
137 221
138 25
139 348
140 472
141 299
142 780
143 393
144 959
145 917
146 241
147 767
148 245
149 606
150 428
151 970
152 533
153 43
154 429
155 197
156 900
157 623
158 780
159 656
160 427
161 365
162 651
163 557
164 569
165 489
166 622
167 45
168 605
169 374
170 301
171 866
172 383
173 31
174 600
175 45
176 375
177 222
178 687
179 508
180 289
181 738
182 53
183 1
184 444
185 965
186 906
187 791
188 145
189 467
190 731
191 907
192 672
193 505
194 824
195 423
196 324
197 623
198 835
199 523
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
E:\Github Repo's\assignment 3.2\Debug\assignment 3.2.exe (process 12832) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

正如您所看到的,第一个 for 循环的输出与预期的完全一样,但是具有所有相同功能的函数不起作用,并且显示了一个未初始化的 int for temp。

这意味着它没有从我传递给函数的 ifstream 中读取。所以编写了另一个函数,它使用新创建的 ifstream 以及传递的 ifstream 读取文件并打印从两个流中读取的内容。

totalInts(ifstream &file)
{
    ifstream tester("numbers.txt");
    string  l,i;
    int intNum;
    intNum = 0;
    

    for (int j = 0; j < 200; j++)
    {
        file >> l;
        tester >> i;
        intNum++;
        cout << intNum << ' ' << i << ' ' << l << '\n';
    }
    file.close();
    return intNum;

}

当使用它时,输出符合预期,因此使用引用将 ifstream 作为参数传递是问题,因为当我创建一个新的 ifstream 时,程序使用该 ifstream 运行良好并按预期读取文件,其中传递的 ifstream 显示没有读取的值。

0 41
1 485
2 340
3 526
4 188
5 739
6 489
7 387
8 988
9 488
10 710
11 173
12 304
13 843
14 970
15 491
16 997
17 953
18 831
19 441
20 423
21 618
22 905
23 153
24 292
25 394
26 438
27 734
28 737
29 914
30 452
31 747
32 785
33 549
34 870
35 931
36 692
37 325
38 52
39 903
40 731
41 834
42 353
43 363
44 690
45 668
46 156
47 718
48 281
49 874
50 572
51 671
52 694
53 789
54 57
55 871
56 731
57 750
58 556
59 778
60 328
61 38
62 212
63 843
64 288
65 136
66 49
67 950
68 283
69 670
70 473
71 828
72 905
73 735
74 394
75 365
76 21
77 132
78 417
79 551
80 648
81 635
82 108
83 973
84 774
85 851
86 970
87 383
88 944
89 334
90 960
91 471
92 650
93 334
94 542
95 559
96 134
97 84
98 951
99 557
100 837
101 146
102 643
103 687
104 726
105 939
106 990
107 308
108 704
109 408
110 26
111 773
112 950
113 91
114 276
115 834
116 803
117 588
118 102
119 528
120 10
121 303
122 170
123 654
124 377
125 791
126 678
127 589
128 35
129 64
130 377
131 151
132 957
133 745
134 979
135 433
136 138
137 221
138 25
139 348
140 472
141 299
142 780
143 393
144 959
145 917
146 241
147 767
148 245
149 606
150 428
151 970
152 533
153 43
154 429
155 197
156 900
157 623
158 780
159 656
160 427
161 365
162 651
163 557
164 569
165 489
166 622
167 45
168 605
169 374
170 301
171 866
172 383
173 31
174 600
175 45
176 375
177 222
178 687
179 508
180 289
181 738
182 53
183 1
184 444
185 965
186 906
187 791
188 145
189 467
190 731
191 907
192 672
193 505
194 824
195 423
196 324
197 623
198 835
199 523
1 41
2 485
3 340
4 526
5 188
6 739
7 489
8 387
9 988
10 488
11 710
12 173
13 304
14 843
15 970
16 491
17 997
18 953
19 831
20 441
21 423
22 618
23 905
24 153
25 292
26 394
27 438
28 734
29 737
30 914
31 452
32 747
33 785
34 549
35 870
36 931
37 692
38 325
39 52
40 903
41 731
42 834
43 353
44 363
45 690
46 668
47 156
48 718
49 281
50 874
51 572
52 671
53 694
54 789
55 57
56 871
57 731
58 750
59 556
60 778
61 328
62 38
63 212
64 843
65 288
66 136
67 49
68 950
69 283
70 670
71 473
72 828
73 905
74 735
75 394
76 365
77 21
78 132
79 417
80 551
81 648
82 635
83 108
84 973
85 774
86 851
87 970
88 383
89 944
90 334
91 960
92 471
93 650
94 334
95 542
96 559
97 134
98 84
99 951
100 557
101 837
102 146
103 643
104 687
105 726
106 939
107 990
108 308
109 704
110 408
111 26
112 773
113 950
114 91
115 276
116 834
117 803
118 588
119 102
120 528
121 10
122 303
123 170
124 654
125 377
126 791
127 678
128 589
129 35
130 64
131 377
132 151
133 957
134 745
135 979
136 433
137 138
138 221
139 25
140 348
141 472
142 299
143 780
144 393
145 959
146 917
147 241
148 767
149 245
150 606
151 428
152 970
153 533
154 43
155 429
156 197
157 900
158 623
159 780
160 656
161 427
162 365
163 651
164 557
165 569
166 489
167 622
168 45
169 605
170 374
171 301
172 866
173 383
174 31
175 600
176 45
177 375
178 222
179 687
180 508
181 289
182 738
183 53
184 1
185 444
186 965
187 906
188 791
189 145
190 467
191 731
192 907
193 672
194 505
195 824
196 423
197 324
198 623
199 835
200 523

E:\Github Repo's\assignment 3.2\Debug\assignment 3.2.exe (process 24600) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

如果这取决于我,我只需将文件名作为字符串参数传递给函数,并在这些函数中创建新的 ifstream 以使事情正常工作,但是对此的要求是至少有一个函数可以传递ifstream 通过引用并利用它。但是,经过大量故障排除和阅读有关该主题的 stackexange 帖子后,我无法使其按预期运行。

我究竟做错了什么?

任何帮助,将不胜感激。

标签: c++fstreamifstream

解决方案


推荐阅读