1, 整数
intNum.txt
10 12 45 0 100 12 45 0 10 12 45 0 10 12 45 0 100 12 45 0 10 12 45 0
int main() { ifstream infile; infile.open("intNum.txt"); if (!infile) cout << "error" << endl; string str; int t1; //存入数组 cout << "存入数组" << endl; int a[6][4]; int *p = &a[0][0]; while (infile >> t1) //遇到空白符结束 { *p = t1; p++; } infile.close(); for (int i = 0; i<6; i++) { for (int j = 0; j<4; j++) cout << a[i][j] << "\t"; cout << endl; } system("pause"); return 0; }
test.txt
lorem ipsum0 1.0 0.0 0 10.25 100 100 12 45 0 10.2 12 45 0 10.2 12 45 0 10.129.10.30 1.0 0.0 0 10.25 100 100 12 45 0 10.2 12 45 0 10.2 12 45 0 10.129.10.30 1.0 0.0 0 10.25 100 100 12 45 0 10.2 12 45 0 10.2 12 45 0 more lorem ipsum
int main() { char szTest[1000] = { 0 }; // = NULL; char *temend = 0; char temp[10] = { '0' }; int len = 0; char IP[20]; vector<string> IPS; vector<string> matrix; vector<float> mat; vector<vector<char>> mats; float a[20] = { 0 }; FILE *fp = fopen("test.txt", "r"); if (NULL == fp) { printf("failed to open dos.txt\n"); return 1; } int i = 0; int k = 0; while (!feof(fp)) { ++i; memset(szTest, 0, sizeof(szTest)); fgets(szTest, sizeof(szTest) - 1, fp); // 包含了\n char *temstart = &szTest[0]; if (i % 5 == 1) { IPS.push_back(szTest); } else { for (int i = 0; i < strlen(szTest); i++) { //cout << szTest[i] << endl; if (szTest[i] == ' ' || szTest[i] == '\n') { temend = &szTest[i]; if (temstart <= temend) { for (int j = 0; temstart < temend; temstart++, j++) { temp[j] = *temstart; } matrix.push_back(temp); float te = atof(temp); mat.push_back(te); memset(temp, 0, sizeof(temp)); ++temend; temstart = temend; } } } //cout << "hello!\n" << endl; } if (i % 5 == 0) { i = 0; } printf("%s", szTest); } fclose(fp); for (vector<string>::iterator it = IPS.begin(); it != IPS.end(); it++) { cout << *it << endl; } cout << "------------------mat------------------" << endl; for (vector<float>::iterator its = mat.begin(); its != mat.end(); its++) { cout << *its << endl; } printf("\n"); getchar(); return 0; }
input.txt
116 98 133 137 129 120 164 105 168 180 178 141 73 80 184 250 267 188 329 333 288 287 312 173 259 262 290 258 271 232 565 1624 1426 1234 749 123
output1.txt
116 98 133 137 129 164 105 168 180 178 73 80 184 250 267 329 333 288 287 312 259 262 290 258 271 565 1624 1426 1234 749
output2.txt
98 133 137 129 120 105 168 180 178 141 80 184 250 267 188 333 288 287 312 173 262 290 258 271 232 1624 1426 1234 749 123
int main() { int i; int sum1 = 0; int sum2 = 0; vector<int> vec; //int k; ifstream inf("input.txt"); ofstream onf1("output1.txt"); ofstream onf2("output2.txt"); if (!inf) cout << "error"; while (inf >> i) { ++sum1; if (sum1 % 6 == 0) { onf1 << "\n"; onf2 << i; vec.push_back(i); continue; } onf1 << i << " "; if (sum1 % 6 == 1) { onf2 << "\n"; continue; } onf2 << i << " "; } cout << "---------vector-----------" << endl; vector<int>::iterator it = vec.begin(); for (; it != vec.end(); it++) { cout << *it << endl; } return 0; }