1 1 2 3 1 2 2 2 3 3 1 2 5 34 32 345 3 234 23 23
#include <stdio.h> #include <iostream> #include <stdlib.h> using namespace std; void main(void) { FILE *f; f=fopen("1.txt","r"); char c[16]; int byf[128],i=0,j=0; while(1) { i=0; while(1) { c[i]=fgetc(f); if(c[i]==' ' || c[i]=='\n' || c[i]=='\t' || c[i]==EOF) break; i++; } byf[j]=atoi(c); j++; if(c[i]==EOF) break; } for(i=0;i<j;i++) cout<<byf[i]<<endl; }
#include <string> #include <vector> #include <fstream> #include <sstream> int _tmain(int argc, _TCHAR* argv[]) { //вариант 1: загоняем все значения в вектор std::vector<int> buf; { std::ifstream f("C:/file.txt"); std::copy(std::istream_iterator<int>(f),std::istream_iterator<int>(),std::back_inserter(buf)); } //вариант 2: загоняем все значения в вектор векторов std::vector<std::vector<int> > buf2; std::ifstream f("C:/file.txt"); { std::string tmp; while(std::getline(f,tmp,'\n')){ std::vector<int> buf; std::stringstream ss; ss << tmp; std::copy(std::istream_iterator<int>(ss),std::istream_iterator<int>(),std::back_inserter(buf)); buf2.push_back(buf); } } return 0; }