+ 1
help with c++ ifstreams
so im reading numbers from a txt file that look like: 3 8 1 6 3 5 7 4 9 2 i only need to store 3 in the value of n, without using getline, 3 is what i read first to make the 3x3 matrix below when i use my while loop: while(read >> n){ cout << n << endl; } it displays all the numbers an i only need n = 3 how can i get it to only read 3?
8 Answers
+ 4
Baric You will need to do more work,but,
you're welcome!
+ 3
/*bad practice using auto but
try this
picked fileData instead of n
*/
for( auto fileData : read){
try{
if(atoi(fileData) == 3){
cout << fileData << endl;
}
}
catch(...){}
}
+ 3
Try it see what happens.
+ 3
If you want to just read 3, and you can be sure that 3 is on the first line, assuming inData is your ifstream object,
inData >> n;
Yep.
+ 2
still a bit confused that's pretty advanced, havent gone over that in class but it helped :)
+ 1
thank you
0
will this only work for 3 because I need to run it for 5x5 matrix an 7x7 an 9x9 all in the same txt file 3x3 is just the first one
0
i needed to set it to a function in the while
while(read >> n){
loadnumbers(array[][], size);
}
void loadnumbers(int n, int array[size][size]){
for (int row = 0; row < n; row++){
for(int col = 0; col < n; col++){
read >> array[row][col]
}
}
}