如何计算文件中的"columns"数?
How can I work out the number of "columns" in a file?
我得到了以下函数;
double* insert(char *file_Name, int Rows, int Columns)
{
double* input = new double[Rows*Columns];
//insert function code
}
为了使其正常工作,我需要知道文件中的行数和列数,以便将它们传递给此函数。我已经计算出了使用的行数;
int RowCount = 0;
string rows;
while (getline(myfile, rows)) {
RowCount++;
}
然而,我在计算我的文件中有多少列时遇到了很多麻烦。
我尝试过的一种方法是尝试查找"\n"的所有实例,并在每个实例上递增一个计数器;
int ColumnCount = 0;
while (!myfile.eof()){
if(myfile.peek()=='n'){
countC++;
}
}
while循环本身从未被触发,因此"ColumnCount"保持为0
文件本身是一个庞大的数字集合,每个数字由空格分隔。
因为我很无聊…
这在边缘情况下肯定有错误(特别是与行末尾的分隔符有关;meh(,但适用于示例输入,并(通常(显示了一种可能的方法。
#include <iostream>
#include <sstream>
#include <cstring>
bool CharIsIn(const char c, const char* str, const size_t strLen)
{
for (size_t i = 0; i < strLen; i++)
if (str[i] == c)
return true;
return false;
}
// Detects the number of subsequences of "delimiters" in the line.
//
// By default a valid delimiter is either whitespace or a tab character,
// and "empty" columns are collapsed.
size_t DetectNumColumns(
const std::string& line,
const char* delimiters = " t",
const bool collapseEmpty = true
)
{
if (line.empty())
return 0;
const size_t delimsLen = std::strlen(delimiters);
size_t result = 1;
bool lastWasDelim = true;
for (size_t pos = 0; pos < line.size(); ++pos)
{
if (CharIsIn(line[pos], delimiters, delimsLen))
{
if (!lastWasDelim || !collapseEmpty)
result++;
else if (pos == line.size()-1 && lastWasDelim && !collapseEmpty)
result++;
lastWasDelim = true;
}
else
{
lastWasDelim = false;
}
}
return result;
}
int main()
{
// Simulating your input file
std::stringstream ss;
ss << "1.5 7.6n";
ss << "2.3 4.5n";
ss << "9.9 7.5n";
bool GotColumnCount = false;
int RowCount = 0, ColumnCount = 0;
std::string line;
while (std::getline(ss, line))
{
// On the first iteration (only!) count columns.
const int columns = DetectNumColumns(line);
if (!GotColumnCount)
{
// On the first iteration, store this.
ColumnCount = columns;
GotColumnCount = true;
}
else
{
// On subsequent iterations, just ensure the column
// count is consistent.
if (columns != ColumnCount)
throw std::out_of_range("Inconsistent column count in input");
}
// Always increment the row count (this bit's easy)
RowCount++;
}
std::cout << "The input consists of " << RowCount << " rows of " << ColumnCount << " columnsn";
}
(现场演示(
突出的一点是,您需要分析至少一行文本,以了解您的分隔符出现了多少次(或分隔符序列显示了多少次,具体取决于您的确切要求(。您可能需要分析每一行文本,以验证整个文件中的列数是否一致。
我故意不修复这些错误,这不仅是因为我不会被愚弄(尽管这肯定是真的(,也是为了阻止你简单地复制/粘贴这个例子作为你的解决方案!请一定要用它来获得灵感,并想出更好的东西。
提示:如果你的分隔符总是只有一个字符(例如一个空格(,并且你不需要宽容地处理添加的前导或尾随空格之类的事情,那么DetectNumColumns
会比我上面的尝试简单得多;它实际上只是在数(但一定要数你围栏的嵌板,而不是柱子!(。
您可以使用字符串流,并假设行具有不同的列。
编辑:对于您的情况,您可能不需要maxColumnCount。只需计算列数并打断即可。
int maxColumnCount = 0;
string row;
while (getline(myfile, row)) {
RowCount++;
std::istringstream iss(row);
int columnCount = 0;
for(string columnValue; iss >> columnValue; )
columnCount++;
if(columnCount > maxColumnCount)
maxColumnCount = columnCount ;
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- 如何计算文件中的"columns"数?