64,648
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
int main(int, char**)
{
std::ifstream infile("1.txt"); // 打开文件
if (!infile) {
return -1;
}
std::vector<int> vis;
std::string line;
while (std::getline(infile, line)) { // 读取一行,处理一行
std::istringstream is(line); // 将一行数据以输入流方式处理
while (!is.eof() && is) { // 如果以行数据没处理完毕,并且流对象正常
int tmp;
std::string stmp;
is >> tmp; // 提取整型数值
if (is) { // 如果操作成功
vis.push_back(tmp); // 将提取的数值存进vector
is >> stmp; // 提取*@,并忽略
}
}
}
infile.close(); // 文件内容操作完毕,关闭文件
// 使用copy算法输出vector中的内容
std::copy(vis.begin(), vis.end(), std::ostream_iterator<int>(std::cout, "\r\n"));
return 0;
}
#include <stdio.h>
int main(int argc, char* argv[])
{
FILE* file;
char ch;
char str[80];
int i;
file = fopen("6.txt", "r");
if(file == NULL)
{
return -1;
}
i = 0;
while(!feof(file))
{
ch = fgetc(file);
if(ch == '*' || ch == '\n')
{
str[i] = '\0';
printf("%s\n", str);
i = 0;
}
else if(isdigit(ch))
{
str[i++] = ch;
}
}
fclose(file);
return 0;
}