如何找出输入文件中未知的行数和列数? [英] How to find out unknown number of rows and cols in a input file?

查看:118
本文介绍了如何找出输入文件中未知的行数和列数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  00000000100000001010 
00000000010000001001
11100000010100000010
10100100101010101010
00101010010010101000

这是一个5x20的网格示例,行和列可以是任意的。这意味着我需要计算出输入文件有多少行和列,然后才能开始计算我的二维数组。



所以我有点困惑,因为对现在我只是试图读取数组,然后输出到控制台,而不知道最初的行和列。请帮助我,这是烦人的,我找不到办法做到这一点。



我无法使用字符串库 解决方案

有很多方法可以继续,取决于动机是什么你的老师禁止使用 string 。以下两种选择之一可能是可以接受的。请注意,我已经玩了一些快速和松散,并跳过了相当数量的错误检查,但你不应该当你充实。在所有情况下,我假定文件不是以行终止符结尾(如您所示),并且是矩形的(每行上的字符数相同)。

< h2>使用自动调整大小的容器

在不使用字符串的情况下仍然可以获得空闲内存管理的基本好处code>。例如,将字符一个接一个推入 vector :

  std :: ifstream inF(input.txt); 
std :: vector< char>内容;
char c; $($)
$ b $(b)
}

int rows = 1 + std :: count(contents.begin(),contents.end(),'\\\
');
int columns = std :: find(contents.begin(),contents.end(),'\\\
') - contents.begin();



从流中读出文件的长度

找出文件有多大,并预先分配一个足够大的缓冲区来保存它非常简单:

  std :: ifstream inF(input.txt); 

//选项1
inF.seekg(0,std :: ios_base :: end);
int charCount = inF.tellg();

//选项2
// int charCount = 0;
// while(inF.get()!= std :: char_traits< char> :: eof())
// {
// charCount ++;
//}
//inF.clear();

char * contents = new char [charCount + 1];

inF.seekg(0);
inF.read(contents,charCount);

contents [charCount] ='\0';

int rows = 1 + std :: count(contents,contents + charCount,'\\\
');
int columns = std :: find(contents,contents + charCount,'\\\
') - contents;

删除[]内容;


Okay so the input file looks like this:

00000000100000001010 
00000000010000001001
11100000010100000010 
10100100101010101010 
00101010010010101000

This is an example grid which is 5x20 and the catch is that the rows and cols can be arbitrary. Which means that I need to figure out how many rows and cols the input file has before I can start computing my two dimensional array.

So I am a little confused because right now I am just trying to read in the array then output it to the console without knowing the rows and cols initially. Please help me with this it's annoying and I can't find a way to do it.

P.S. I can not use the string library

解决方案

There are many ways to proceed, depending on what the motivation was behind your teacher's prohibition on using string. One of the two options below will probably be acceptable. Note that I've played a little fast and loose and skipped a fair amount of error checking, but you shouldn't when you flesh it out. In all cases, I'm assuming the file does not end in a line terminator (as you've shown) and is rectangular (same number of characters on every line).

Use an automatically resizing container

You can still get the essential benefit (for this case) of free memory management without using a string. For example, push the characters one-by-one into a vector:

std::ifstream inF("input.txt");
std::vector<char> contents;
char c;

while(inF.get(c).good())
{
    contents.push_back(c);
}

int rows = 1 + std::count(contents.begin(), contents.end(), '\n');
int columns = std::find(contents.begin(), contents.end(), '\n') - contents.begin();

Read out the length of the file from the stream

It's a pretty simple matter to figure out how big the file is and pre-allocate a buffer large enough to hold it:

std::ifstream inF("input.txt");

// option 1
inF.seekg(0, std::ios_base::end);
int charCount = inF.tellg();

// option 2
//int charCount = 0;
//while(inF.get() != std::char_traits<char>::eof())
//{
//    charCount++;
//}
//inF.clear();

char* contents = new char[charCount + 1];

inF.seekg(0);
inF.read(contents, charCount);

contents[charCount] = '\0';

int rows = 1 + std::count(contents, contents + charCount, '\n');
int columns = std::find(contents, contents + charCount, '\n') - contents;

delete[] contents;

这篇关于如何找出输入文件中未知的行数和列数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆