Question about reading file into arrays

My question is about reading a txt file and inputting the contents into an array. I can get the file to open and read the values, and got it to skip spaces. Basically what I need is a looop setup that will get the first values put them in array a, then skip spaces put next set of values into array b, then skip spaces and put final values into array c. Then (since there is an endline) start first array with the new line. So far all I have set up is 1 array that will just get the numbers, and store the spaces into a separate temporary array. Any help would be appreciated.
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 <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ifstream in;
	char num[4000];
	int spaces[9000];
	in.open("candycrush.txt");
	
	for (int i = 0; i < 4000; i++ )
	{
	if (num[i] != 32)
	{
	
	in >> num[i];
	}
	else
	in >> spaces[i];
	}
	for (int i = 0; i < 4000; i++ )
	{
		cout << (int)num[i] ;
	}
	return 0;
}
for (int i = 0; i < 4000; i++ )

mmm... , this is a bad method, your loop will read past the end of the file, you should use a while loop instead, this way, the loop automatically stop whenever it encounters the end of file,

~~~~~~~~~~~
Basically what I need is a loop setup that will get the first values put them in array a, then skip spaces put next set of values into array b, then skip spaces and put final values into array c.


you can do something like :
1
2
3
4
5
6
7
8
9
10
11
12
int num[ 100 ];
int bla[ 100 ];
char some_name[ 100 ];

int index = 0;

while( in >> num[ index ] )
{
    in >> bla[ index ];
    in >> some_name[ index ];
    ++index;
}

as simple as that, since after cin >> encounters spaces, newlines etc.. it "skips" to the next available character to read ( which of course is not a space, newline tab... )

~~~~~~~~~~~
and store the spaces into a separate temporary array


Nahh, spaces aren't read by >> operator, it skips it ( and also newlines, tabs, etc... )

~~~~~~~~~~~
Then (since there is an endline) start first array with the new line


as i said, newlines is skipped with >> operator
Last edited on
Ok I'll modify my code when I get off work tonight. Thank you for the help. I will update if I have issues. Next I will have to sort the arrays.
Ok I modified my code, but it just crashes everytime I try to run it. there are 3 sections I need to read from the txt file hence the 3 arrays I'm trying to insert them into.
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
#include <iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>
using namespace std;
int main()
{
int index = 0;
int level[index];
int stars[index];
int score[index];
char blank[index];
ifstream inFile;
inFile.open("candycrush.txt");
if (!inFile)
   {
   cout << "The candycrush.txt input file did not open";
   exit(-1);
   }
   while(inFile >> level[index])
   {
   inFile >> level[index];
   inFile >> blank[index];
   inFile >> score[index];
   inFile >> blank[index];
   inFile >> stars[index];
   index++;

     

}
cout << endl << score << "    " << level << "   " << stars << endl;
inFile.close();
return 0;
}
What does the contents of the input file actually look like? An example of the input data would be useful.
it's a giant text file of candy crush scores. Can I attatch text files?
If it's too big to post directly in a forum message, you could use a file sharing service such as dropbox or fileconvoy.com
1 3840 3
2 5940 3
3 11560 3
4 18140 3
5 18780 3
6 48000 3
7 240080 3
8 67720 3
9 69980 3
10 135160 3
11 121160 3
12 106380 3
13 62000 3
14 113440 3
15 97920 3
16 336200 3
17 116740 3
18 114240 3
19 400080 3
20 57560 3
21 98720 3
22 62560 3
23 133380 2
24 79460 3
25 167200 3
26 123860 3
27 153540 3
28 115880 3
29 240620 3
30 159640 3
31 96640 2
32 56160 3
33 51960 2
34 124640 3
35 297460 3
36 26560 2
37 135740 3
38 111160 2
39 84460 3
40 123140 3
41 167440 3
42 86760 3
43 82460 3
44 113040 2
45 199280 2
46 103100 3
47 32700 2
48 111020 2
49 74500 2
50 197360 2
51 90480 3
52 75600 2
53 111340 3
54 174180 3
55 174560 3
56 162860 3
57 87420 2
58 86980 3
59 28700 2
60 136580 3
61 115440 3
62 88100 3
63 180360 3
64 47400 3
65 262820 2
66 141480 3
67 58780 1
68 286240 3
69 94760 1
70 114640 2
71 77580 2
72 97380 2
73 212480 3
74 173520 2
75 217420 2
76 56380 1
77 77540 1
78 82680 1
79 162480 2
80 10700 1
81 84160 1
82 147620 3
83 33040 1
84 19960 1
85 151020 2
86 90500 3
87 105500 1
88 70760 1
89 110640 1
90 63820 2
91 113420 2
92 53780 1
93 236240 3
94 33420 3
95 66660 1
96 17880 1
97 100660 1
98 175680 3
99 107580 1
100 77100 1
101 150060 3
102 105340 1
103 185880 3
104 133420 1
105 165320 3
106 100840 1
107 70440 2
108 37080 3
109 123460 2
110 100380 1
111 293660 3
112 122400 2
113 136000 3
114 26840 1
115 69360 3
116 166620 3
117 49000 1
118 39200 1
119 83600 2
120 128980 2
121 51040 1
122 43460 1
123 189900 3
124 155680 2
125 151600 3
126 58220 3
127 7220 1
128 113120 3
129 17980 2
130 113980 3
131 93080 2
132 116680 3
133 118740 3
134 55840 1
135 54200 1
136 74320 2
137 35840 2
138 245720 3
139 18960 1
140 84360 2
141 29880 3
142 85640 2
143 228260 3
144 106840 1
145 28940 2
146 117680 3
147 184320 1
148 72700 1
149 108660 2
150 62060 1
151 49040 2
152 136640 3
153 40360 2
154 45200 1
155 178300 2
156 85460 3
157 45220 2
158 38200 1
159 20940 1
160 94000 1
161 200460 3
162 38460 1
163 110480 2
164 80500 1
165 90220 2
166 19460 1
167 198840 3
168 142780 2
169 49300 1
170 234120 2
171 61640 1
172 52600 3
173 125920 2
174 78940 2
175 80180 2
176 55880 3
177 19960 1
178 113160 2
179 78900 3
180 389080 3
181 96120 3
182 32300 1
183 123880 2
184 81220 3
185 220240 2
186 124020 3
187 86760 1
188 153260 2
189 21560 1
190 102580 3
191 295820 2
192 23660 2
193 199840 3
194 130300 3
195 60320 2
196 81000 3
197 218320 3
198 15080 1
199 276820 3
200 127320 2
201 190360 2
202 128740 2
203 68380 1
204 56960 1
205 113540 1
206 106100 3
207 42980 1
208 49520 1
209 63960 1
210 116580 1
211 32040 1
212 68920 3
213 414120 3
214 106340 2
215 52040 1
216 108080 3
217 86960 2
218 43800 3
219 98680 2
220 70160 3
221 122860 2
222 166780 3
223 89180 3
224 97560 3
225 98540 3
226 236320 3
227 121320 3
228 162640 3
229 123420 3
230 184360 3
231 202120 2
232 100920 3
233 264700 3
234 128420 3
235 399000 3
236 221060 3
237 23700 3
238 157500 2
239 166900 3
240 127200 3
241 175600 3
242 92020 3
243 126920 3
244 113560 3
245 206820 3
246 116800 3
247 73820 3
248 70160 1
249 135520 3
250 100440 3
251 184900 3
252 21468340 3
253 77240 3
254 99080 3
255 89300 1
256 104080 3
257 118620 2
258 106500 1
259 72380 3
260 246680 3
261 183780 2
262 221100 3
263 77100 1
264 225160 3
265 107980 2
266 188480 2
267 102260 1
268 275180 3
269 144280 3
270 96100 2
271 139920 3
272 230200 2
273 254020 3
274 652700 3
275 266920 2
276 295700 3
277 107520 1
278 106720 1
279 178700 3
280 88840 2
281 405000 3
282 87720 2
283 32940 1
284 139640 2
285 146280 2
286 95380 3
287 404400 3
288 203240 3
289 115800 3
290 189060 3
291 119700 2
292 520380 3
293 128660 3
294 215740 3
295 154240 1
296 78660 2
297 70140 1
298 134060 3
299 88340 2
300 348780 3
301 399740 3
302 84480 2
303 157960 3
304 148440 2
305 274600 3
306 93680 2
307 176320 3
308 159780 3
309 229520 2
310 106580 1
311 383120 3
312 230400 3
313 36640 3
314 68420 1
315 88940 3
316 245120 3
317 115300 3
318 114020 3
319 62140 1
320 272080 2
321 59080 1
322 154540 2
323 294640 1
324 153000 3
325 152340 2
326 81820 2
That is what is in the text file
Ah, I understand now. I think I was not concentrating properly, I was picturing the data differently.

You would need something like this:
1
2
3
4
    const int maxsize = 330; // choose sise with care
    int level[maxsize];
    int stars[maxsize];
    int score[maxsize];


and then read the data something like:
1
2
3
    int index = 0;
    while (inFile >> level[index] >> stars[index] >> score[index])
        index++; 


(The previous answers said more or less the same thing too).

However, if you don't know for certain how large the file will be, you might be better off using vectors, which work in a very similar way, but can be resized during use.

Also, I was thinking, rather than have three separate arrays, you could declare a struct like this:
1
2
3
4
5
6
7
struct candy {
    int level;
    int stars;
    int score;
}

candy  array[maxsize]; 
Last edited on
hm If I do vector can I sort the contents on the vectors? I am looking to sort the arrays and change the order of them based on the second (score) array.
Yes, you could sort it whether you use arrays or vectors.
Topic archived. No new replies allowed.