简体   繁体   English

我应该用什么从文件中读取整数

[英]What should I use to read integers from a file

Hi I am a beginner in programming, and I want to read many integers distributed in row and columns, and how many integer is there.嗨,我是编程初学者,我想读取分布在行和列中的许多整数,以及那里有多少 integer。 Whenever I try infile>>x it just take the first value.每当我尝试 infile>>x 时,它只取第一个值。

I would appreciate it so much,if you can just explain how can you do it to me.如果你能解释一下你怎么能对我做的话,我会非常感激。 the file looks like this:(much longer but I want you to get the idea):该文件看起来像这样:(更长,但我希望你明白):

41  467 334 500 169 724 478 358 962 464 705 145 281 827 961 491 995 942 827 436 391 604 902 153 292 382 421 716 718 895 447 726 771 538 869 912 667 299 35  894 703 811 322 333 673 664 141 711 253 868 547 644 662 757 37  859 723 741 529 778 316 35  190 842 288 106 40  942 264 648 446 805 890 729 370 350 6   101 393 548 629 623 84  954 756 840 966 376 931 308 944 439 626 323 537 538 118 82  929 541 833 115 639 658 704 930 977 306 673 386 21  745 924 72  270 829 777 573 97  512 986 290 161 636 355 767 655 574 31  52  350 150 941 724 966 430 107 191 7   337 457 287 753 383 945 909 209 758 221 588 422 946 506 30  413 168 900 591 762 655 410 359 624 537 548 483 595 41  602 350 291 836 374 20  596 21  348 199 668 484 281 734 53  999 418 938 900 788 127 467 728 893 648 483 807 421 310 617 813 514 309 616 935 451 600 249 519 556 798 303 224 8   844 609 989 702 195 485 93  343 523 587 314 503 448 200 458 618 580 796 798 281 589 798 9   157 472 622 538 292 38  179 190 657 958 191 815 888 156 511 202 634 272 55  328 646 362 886 875 433 869 142 844 416 881 998 322 651 21  699 557 476 892 389 75  712 600 510 3   869 861 688 401 789 255 423 2   585 182 285 88  426 617 757 832 932 169 154 721 189 976 329 368 692 425 555 434 549 441 512 145 60  718 753 139 423 279 996 687 529 549 437 866 949 193 195 297 416 286 105 488 282 455 734 114 701 316 671 786 263 313 355 185 53  912 808 832 945 313 756 321 558 646 982 481 144 196 222 129 161 535 450 173 466 44  659 292 439 253 24  154 510 745 649 186 313 474 22  168 18  787 905 958 391 202 625 477 414 314 824 334 874 372 159 833 70  487 297 518 177 773 270 763 668 192 985 102 480 213 627 802 99  527 625 543 924 23  972 61  181 3   432 505 593 725 31  492 142 222 286 64  900 187 360 413 974 270 170 235 833 711 760 896 667 285 550 140 694 695 624 19  125 576 694 658 302 371 466 678 593 851 484 18  464 119 152 800 87  60  926 10  757 170 315 576 

Try using a while to loop through every integer in the file:尝试使用while循环遍历文件中的每个 integer:

while(infile>>x) {
    // Do something with the integer
}

Note that if you want it in a 2 dimensional array of rows and columns, you could do something more elaborate like this using a std::stringstream :请注意,如果您希望它在行和列的二维数组中,您可以使用std::stringstream做一些更精细的事情:

#include <sstream>
#include <string>

//...

std::vector<std::vector<int>> result;
std::string tmp;
while(getline(infile,tmp)) {
    stringstream stream;
    stream << tmp;

    // add new row
    std::vector<int> row;
    while (ss >> x) {
        // add x to the new row
   }

   result.push_back(row);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM