C++ 从txt文件中读取字符,然后打印到数组c++;
因此,我试图读取一个文本文件并将它们输入到一个“char”数组中,我希望每一行都作为一个条目输入,到目前为止,我已经有了这一点,但它一次只输入一个符号 txt文件:C++ 从txt文件中读取字符,然后打印到数组c++;,c++,arrays,C++,Arrays,因此,我试图读取一个文本文件并将它们输入到一个“char”数组中,我希望每一行都作为一个条目输入,到目前为止,我已经有了这一点,但它一次只输入一个符号 txt文件: QA310 95 47 50 CM145 320 162 200 MS514 34 20 25 EN212 163 150 160 读取文件的代码: for (int i = 0; i < SIZE; i++) { inFile >> product1[i]; } for
QA310
95
47
50
CM145
320
162
200
MS514
34
20
25
EN212
163
150
160
读取文件的代码:
for (int i = 0; i < SIZE; i++)
{
inFile >> product1[i];
}
for (int i = 4; i < 7; i++)
{
inFile >> product2[i-4];
}
for (int i = 7; i < 11; i++)
{
inFile >> product3[i-7];
}
for (int i = 11; i < 15; i++)
{
inFile >> product4[i-11];
}
outFile << product1[0] << endl;
outFile << product2[0] << endl;
outFile << product3[0] << endl;
outFile << product4[0] << endl;
for(int i=0;i>产品1[i];
}
对于(int i=4;i<7;i++)
{
填充>>产品2[i-4];
}
对于(int i=7;i<11;i++)
{
填充>>产品3[i-7];
}
对于(int i=11;i<15;i++)
{
填充>>产品4[i-11];
}
outFile请澄清,但我认为您需要4行的集合
[编辑]修改代码以匹配注释中的字符串
#include "Header.h"
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace::std;
typedef struct _product {
wstring code;
vector<int> numbers;
} Product;
bool isproduct(const wstring& txtrow, int currentrow, int interval = 4)
{
//// assumes non-numeric product
//if (stoi(txtrow) > 0)
// return false;
//else
// return true;
// assumes constant pattern
if ((currentrow % interval) == 1)
return true;
else
return false;
}
void parsefile(wstring filepath)
{
wfstream inFile(filepath);
vector<Product> list;
Product prod;
int rowcount = 0;
while (!inFile.eof()) {
wstring txtrow;
inFile >> txtrow;
if (txtrow.empty())
continue;
rowcount++;
if (isproduct(txtrow, rowcount)) {
// add product to list
if (!prod.code.empty())
list.push_back(prod);
// start next
prod.code = txtrow;
prod.numbers.clear();
}
else
prod.numbers.push_back(std::stoi(txtrow));
}
// add final product to list
if (!prod.code.empty())
list.push_back(prod);
for (vector<Product>::iterator idx = list.begin(); idx != list.end(); idx++) {
std::wcout << idx->code << L" ";
}
std::wcout << std::endl;
}
#包括“Header.h”
#包括
#包括
#包括
#包括
使用namespace::std;
typedef结构产品{
wstring码;
向量数;
}产品;
bool isproduct(const wstring&txtrow,int currentrow,int interval=4)
{
////假定为非数字乘积
//如果(stoi(txtrow)>0)
//返回false;
//否则
//返回true;
//假设模式不变
如果((当前行%间隔)==1)
返回true;
其他的
返回false;
}
无效解析文件(wstring文件路径)
{
wfstream-infle(文件路径);
向量表;
产品生产;
int rowcount=0;
而(!infle.eof()){
wstring txtrow;
内嵌>>txtrow;
if(txtrow.empty())
继续;
行计数++;
if(isproduct(txtrow,rowcount)){
//将产品添加到列表中
如果(!prod.code.empty())
列表。推回(prod);
//下一步开始
prod.code=txtrow;
prod.number.clear();
}
其他的
产品编号推回(std::stoi(txtrow));
}
//将最终产品添加到列表中
如果(!prod.code.empty())
列表。推回(prod);
对于(向量::迭代器idx=list.begin();idx!=list.end();idx++){
std::wcout code请澄清,但我认为您需要4行的集合
[编辑]修改代码以匹配注释中的字符串
#include "Header.h"
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace::std;
typedef struct _product {
wstring code;
vector<int> numbers;
} Product;
bool isproduct(const wstring& txtrow, int currentrow, int interval = 4)
{
//// assumes non-numeric product
//if (stoi(txtrow) > 0)
// return false;
//else
// return true;
// assumes constant pattern
if ((currentrow % interval) == 1)
return true;
else
return false;
}
void parsefile(wstring filepath)
{
wfstream inFile(filepath);
vector<Product> list;
Product prod;
int rowcount = 0;
while (!inFile.eof()) {
wstring txtrow;
inFile >> txtrow;
if (txtrow.empty())
continue;
rowcount++;
if (isproduct(txtrow, rowcount)) {
// add product to list
if (!prod.code.empty())
list.push_back(prod);
// start next
prod.code = txtrow;
prod.numbers.clear();
}
else
prod.numbers.push_back(std::stoi(txtrow));
}
// add final product to list
if (!prod.code.empty())
list.push_back(prod);
for (vector<Product>::iterator idx = list.begin(); idx != list.end(); idx++) {
std::wcout << idx->code << L" ";
}
std::wcout << std::endl;
}
#包括“Header.h”
#包括
#包括
#包括
#包括
使用namespace::std;
typedef结构产品{
wstring码;
向量数;
}产品;
bool isproduct(const wstring&txtrow,int currentrow,int interval=4)
{
////假定为非数字乘积
//如果(stoi(txtrow)>0)
//返回false;
//否则
//返回true;
//假设模式不变
如果((当前行%间隔)==1)
返回true;
其他的
返回false;
}
无效解析文件(wstring文件路径)
{
wfstream-infle(文件路径);
向量表;
产品生产;
int rowcount=0;
而(!infle.eof()){
wstring txtrow;
内嵌>>txtrow;
if(txtrow.empty())
继续;
行计数++;
if(isproduct(txtrow,rowcount)){
//将产品添加到列表中
如果(!prod.code.empty())
列表。推回(prod);
//下一步开始
prod.code=txtrow;
prod.number.clear();
}
其他的
产品编号推回(std::stoi(txtrow));
}
//将最终产品添加到列表中
如果(!prod.code.empty())
列表。推回(prod);
对于(向量::迭代器idx=list.begin();idx!=list.end();idx++){
std::wcout code使product1、product2…
等成为字符串数组,而不是字符。否则,字符数组的每个位置只包含一个字符,这意味着每个数组的“0”索引将始终只是一个字符
然后,逐行读取文件。如果出于任何原因,文件捕获多行,可以使用新行字符\n
进行分隔,但这里我使用getline:
string[] product1;
string[] product2;
string[] product3;
string[] product4;
ifstream file;
file.open("products.txt");
string line;
int count = 0;
while(getline(file, line)) {
if (count < 4) {
product1[count];
} else if (count < 8) {
product2[count-4];
} else if (count < 12) {
product3[count-8];
} else if (count < 16) {
product4[count-12];
}
count++;
}
cout << product1[0] << endl;
cout << product2[0] << endl;
cout << product3[0] << endl;
cout << product4[0] << endl;
string[]product1;
字符串[]product2;
字符串[]产品3;
字符串[]产品4;
ifstream文件;
打开(“products.txt”);
弦线;
整数计数=0;
while(getline(文件,行)){
如果(计数<4){
产品1[计数];
}否则如果(计数<8){
产品2[count-4];
}否则如果(计数<12){
产品3[count-8];
}否则,如果(计数<16){
产品4[计数-12];
}
计数++;
}
不能将product1、product2…
等设置为字符串数组,而不是字符。否则,字符数组的每个位置只包含一个字符,这意味着每个数组的“0”索引将始终只是一个字符
然后,逐行读取文件。如果出于任何原因,文件捕获多行,可以使用新行字符\n
进行分隔,但这里我使用getline:
string[] product1;
string[] product2;
string[] product3;
string[] product4;
ifstream file;
file.open("products.txt");
string line;
int count = 0;
while(getline(file, line)) {
if (count < 4) {
product1[count];
} else if (count < 8) {
product2[count-4];
} else if (count < 12) {
product3[count-8];
} else if (count < 16) {
product4[count-12];
}
count++;
}
cout << product1[0] << endl;
cout << product2[0] << endl;
cout << product3[0] << endl;
cout << product4[0] << endl;
string[]product1;
字符串[]product2;
字符串[]产品3;
字符串[]产品4;
ifstream文件;
打开(“products.txt”);
弦线;
整数计数=0;
while(getline(文件,行)){
如果(计数<4){
产品1[计数];
}否则如果(计数<8){
产品2[count-4];
}否则如果(计数<12){
产品3[count-8];
}否则,如果(计数<16){
产品4[计数-12];
}
计数++;
}
cout因此,要避免的最大问题是编写一个专门的输入例程,该例程依赖于根据计数器执行不同的操作(这在某些情况下是合适的,但在这里不是)。更好的方法是,如果您可以区分产品ID,因为它以[a-Z]
开头,那么使用isalpha()
检查,如果是,只需将ID添加到std::vector
以跟踪您的产品。如果它不是alph字符,则只需将其添加到临时std::vector
中,并构建与上次读取ID关联的产品列表。当遇到下一个产品ID时,只需将临时向量写入std::ve即可将保存与每个产品ID关联的产品向量的ctor
您可以编写一个非常简单的输入循环来处理这种情况,例如:
std::string line; /* string for line */
std::vector<std::string> prodid{}, tmp{}; /* prod ID & tmp vector */
std::vector<std::vector<std::string>> products{}; /* vector vector products */
size_t n = 0; /* simple counter */
while (getline (f, line)) { /* read each line */
if (isalpha(line.at(0))) { /* if it starts with alpha char */
prodid.push_back(line); /* save it as prod ID */
if (tmp.size()) /* if tmp populated */
products.push_back(tmp); /* save in products */
tmp.clear(); /* clear temporary vector */
}
else /* otherwise */
tmp.push_back(line); /* line is product, add to tmp */
}
if (tmp.size()) /* check if tmp populated */
products.push_back(tmp); /* add final vector of products */
将整个过程放在一起,添加所需的标题,将要读取的文件名作为程序的第一个参数,并添加必要的验证,您将拥有:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cctype>
int main (int argc, char **argv) {
if (argc < 2) { /* validate 1 argument given for filename */
std::cerr << "error: insufficient input\n"
"usage: " << argv[0] << " filename\n";
return 1;
}
std::ifstream f (argv[1]); /* open file, validate file open for reading */
if (!f.is_open()) {
std::cerr << "file open failed.\n";
return 1;
}
std::string line; /* string for line */
std::vector<std::string> prodid{}, tmp{}; /* prod ID & tmp vector */
std::vector<std::vector<std::string>> products{}; /* vector vector products */
size_t n = 0; /* simple counter */
while (getline (f, line)) { /* read each line */
if (isalpha(line.at(0))) { /* if it starts with alpha char */
prodid.push_back(line); /* save it as prod ID */
if (tmp.size()) /* if tmp populated */
products.push_back(tmp); /* save in products */
tmp.clear(); /* clear temporary vector */
}
else /* otherwise */
tmp.push_back(line); /* line is product, add to tmp */
}
if (tmp.size()) /* check if tmp populated */
products.push_back(tmp); /* add final vector of products */
for (auto id : products) { /* loop over products vector of vector */
std::cout << prodid[n++] << ": "; /* output prod ID wtih counter */
for (auto prod : id) /* loop over product record */
std::cout << " " << prod; /* outputting each product */
std::cout << '\n'; /* tidy up with '\n' */
}
}
示例使用/输出$ ./bin/product dat/products.txt
QA310: 95 47 50
CM145: 320 162 200
MS514: 34 20 25
EN212: 163 150 160
...
#include <map>
...
std::string line, idstr; /* string for line, id */
std::vector<std::string> tmp{}; /* tmp vector */
std::map <std::string, std::vector<std::string>> products{}; /* map id/products */
while (getline (f, line)) { /* read each line */
if (isalpha(line.at(0))) { /* starts with alpha char? */
if (tmp.size()) /* if tmp populated */
products.insert({idstr, tmp}); /* save in products */
tmp.clear(); /* clear temporary vector */
idstr = line;
}
else /* otherwise */
tmp.push_back(line); /* line is product, add to tmp */
}
if (tmp.size()) /* check if tmp populated */
products.insert({idstr, tmp}); /* add final map of id/products */
for (auto id : products) { /* loop over all elements of map */
std::cout << id.first << ": "; /* output the prod ID */
for (auto prod : id.second) /* loop over each product */
std::cout << " " << prod; /* outputting each product */
std::cout << '\n'; /* tidy up with '\n' */
}
$ ./bin/product_map dat/products.txt
CM145: 320 162 200
EN212: 163 150 160
MS514: 34 20 25
QA310: 95 47 50