如何让输入文件进入数组
How to get input file to go into an array
我正在尝试将输入文件中的信息获取到输出文件中,我已经这样做了,但是我不知道如何将文件中的数字获取到数组中,并且只能输出可被7整除的数字。我被困了几个小时,请帮忙。
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string ifilename, ofilename, line;
ifstream inFile, checkOutFile;
ofstream outFile;
char response;
// Input file
cout << "Please enter the name of the file you wish to open : ";
cin >> ifilename;
inFile.open(ifilename.c_str());
if (inFile.fail())
{
cout << "The file " << ifilename << " was not successfully opened." << endl;
cout << "Please check the path and name of the file. " << endl;
exit(1);
}
else
{
cout << "The file is successfully opened." << endl;
}
// Output file
char array[10];
int numberz = 0;
cout << "Please enter the name of the file you wish to write : ";
cin >> ofilename;
checkOutFile.open(ofilename.c_str());
if (!checkOutFile.fail())
{
cout << "A file " << ofilename << " exists.nDo you want to continue and overwrite it? (y/n) : ";
cin >> response;
if (tolower(response) == 'n')
{
cout << "The existing file will not be overwritten. " << endl;
exit(1);
}
}
outFile.open(ofilename.c_str());
if (outFile.fail())
{
cout << "The file " << ofilename << " was not successfully opened." << endl;
cout << "Please check the path and name of the file. " << endl;
exit(1);
}
else
{
cout << "The file is successfully opened." << endl;
}
// Copy file contents from inFile to outFile
while (getline(inFile, line))
{
fscanf(ifilename, &array[numberz]);
cout << line << endl;
outFile << line << endl;
}
// Close files
inFile.close();
outFile.close();
} // main
我会用boost::filesystem
来完成这项任务。
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace fs = boost::filesystem;
int main()
{
fs::path ifilename;
std::cout << "Input file: ";
std::cin >> ifilename;
fs::ifstream inFile{ifilename};
if ( inFile.fail() )
throw std::invalid_argument("Could not open file");
fs::path ofilename;
std::cout << "Output file: ";
std::cin >> ofilename;
if ( fs::exists(ofilename) )
{
std::cout << ofilename << " exists. Do you want to overwrite it? (y/n) ";
char response;
std::cin >> response;
if ( ! ( response == 'y' || response == 'Y' ) )
return 1;
}
fs::ofstream outFile{ofilename};
if ( outFile.fail() )
throw std::invalid_argument("Could not open file");
std::vector<std::string> array;
std::string line;
while ( std::getline(inFile, line) )
{
array.push_back( line );
std::cout << line << 'n';
outFile << line << 'n';
}
inFile.close();
outFile.close();
}
相关文章:
- 如何将内容数组写入文本文件?
- C++将文本文件中的数据读取到结构数组中
- 将值从二维数组输出到文本文件
- C++数据文件、数组和计算赋值
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何从txt文件中读取多个不同长度的数组?
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- C++编程从外部文本文件定义数组大小
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 将系数存储在头文件的数组中("does not name a type"错误)
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 从文件读取,并写入函数C++中的数组
- 如何从具有两列的.txt文件创建并行数组?
- 读取二维数组文件