C++ 垂直遍历文件

C++ 垂直遍历文件,c++,C++,我需要以垂直方式遍历文件。如果假定文件内容为: adg beh cfi 它应将文件打印为: abc def ghi 每条线的长度应相同(即,上述示例中,所有线的长度均为3)。我已经编写了一个代码,但它没有按要求遍历文件 #include<iostream> #include<fstream> #include<string> using namespace std; int main() { fstream fs; fs.open("asd

我需要以垂直方式遍历文件。如果假定文件内容为:

adg
beh
cfi
它应将文件打印为:

abc
def
ghi
每条线的长度应相同(即,上述示例中,所有线的长度均为3)。我已经编写了一个代码,但它没有按要求遍历文件

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    fstream fs;
    fs.open("asd.txt",ios::in);
    string str;
    char *ch = new char();
    int lineLen = 0, k = 0;
    if(getline(fs,str))
    {
        lineLen = str.length();
    }
    fs.seekg(0);
    if(lineLen > 0)
    {
    for(int i = 0;i<lineLen;i++)
    {
        fs.seekg(i+k*lineLen);
        while(fs.read(ch,1))
        {
            k++;
            fs.seekg(i+k*lineLen);
            cout<<*ch;
        }
        k = 0;
    }
    }
    fs.close();
    cin.ignore();
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
fstream fs;
打开(“asd.txt”,ios::in);
字符串str;
char*ch=new char();
int-lineLen=0,k=0;
if(getline(fs,str))
{
lineLen=str.length();
}
fs.seekg(0);
如果(lineLen>0)
{

对于(int i=0;i),如果使用的话,您可能会发现这个任务要简单得多。可能有一个C++等价物或包装器,但恐怕我在这方面不是专家。如果有这种情况,希望有人能给出更好的答案。 下面是一个快速的C(不是++)示例。我将看看是否可以在google上搜索并用C++将其进一步加密:

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void)
{
    int fd = open("input", O_RDONLY);

    struct stat s;
    fstat(fd, &s);

    // map the file as one big string
    char *c = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);

    // calculate sizes
    int columns = strchr(c, '\n') - c; // first newline delimits a row
    int stride = columns + 1;          // count the newline!
    int rows = s.st_size / stride;     // all rows are the same length

    for (int x = 0; x < columns; x++)
    {
        for (int y = 0; y < rows; y++)
        {
            putchar(c[y*stride + x]);
        }
        putchar('\n');
    }

    munmap(c, s.st_size);
    close(fd);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
int fd=打开(“输入”,仅限Ordu);
结构统计;
财政司司长(财政司司长及社会福利司);
//将文件映射为一个大字符串
char*c=mmap(0,s.st\u大小,保护读取,映射共享,fd,0);
//计算尺寸
int columns=strchr(c,'\n')-c;//第一个换行符分隔一行
int stride=columns+1;//计算换行符!
int rows=s.st_size/stride;//所有行的长度相同
对于(int x=0;x

<>编辑:在我看来,一个快速的搜索并没有找到一个更好的方法来处理C++。我的意思是,我可以在<代码> MMAP>代码>行中添加一个类型转换,并将<代码> PTCCHAR/<代码>调用到<代码> STD::CUT,但这看起来并没有什么区别。可能会有一个C++等价物或包装器,但恐怕我在这方面不是专家。如果有这种情况,希望有人能给出一个更好的答案。 下面是一个快速的C(不是++)示例。我将看看是否可以在google上搜索并用C++将其进一步加密:

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void)
{
    int fd = open("input", O_RDONLY);

    struct stat s;
    fstat(fd, &s);

    // map the file as one big string
    char *c = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);

    // calculate sizes
    int columns = strchr(c, '\n') - c; // first newline delimits a row
    int stride = columns + 1;          // count the newline!
    int rows = s.st_size / stride;     // all rows are the same length

    for (int x = 0; x < columns; x++)
    {
        for (int y = 0; y < rows; y++)
        {
            putchar(c[y*stride + x]);
        }
        putchar('\n');
    }

    munmap(c, s.st_size);
    close(fd);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
int fd=打开(“输入”,仅限Ordu);
结构统计;
财政司司长(财政司司长及社会福利司);
//将文件映射为一个大字符串
char*c=mmap(0,s.st\u大小,保护读取,映射共享,fd,0);
//计算尺寸
int columns=strchr(c,'\n')-c;//第一个换行符分隔一行
int stride=columns+1;//计算换行符!
int rows=s.st_size/stride;//所有行的长度相同
对于(int x=0;x

<>编辑:在我看来,一个快速的搜索并没有找到一个更好的方法来处理C++。我的意思是,我可以在<代码> MMAP>代码>行中添加一个类型转换,并将<代码> PTCCHAR/<代码>调用到<代码> STD::CUT,但这看起来并没有什么区别。

< P>未经测试的伪代码,它可能会给你一些Ii。基本上,将整个文件加载到一个2d字符向量中以便于访问。它将比直接从文件中读取占用更多内存,但这并不重要,除非文件非常大

vector<vector<char>> filemap;
string line;
while (getline(filestream, line))
{
    filemap.push_back(vector<char>(line.begin(), line.end()));
}

for (int x = 0; x < XSIZE; x++)
{
    for (int y = 0; y < YSIZE; y++)
    {
        filestream << filemap[y][x]; // note x/y are opposite way round in 2d vectors
    }
    filestream << '\n';
}
矢量文件映射;
弦线;
while(getline(filestream,line))
{
filemap.push_back(向量(line.begin(),line.end());
}
对于(int x=0;xfilestream未经测试的伪代码,可能会给您一些想法。基本上,将整个文件加载到二维字符向量中以便于访问。它将使用比直接从文件读取更多的内存,但这并不重要,除非文件非常大

vector<vector<char>> filemap;
string line;
while (getline(filestream, line))
{
    filemap.push_back(vector<char>(line.begin(), line.end()));
}

for (int x = 0; x < XSIZE; x++)
{
    for (int y = 0; y < YSIZE; y++)
    {
        filestream << filemap[y][x]; // note x/y are opposite way round in 2d vectors
    }
    filestream << '\n';
}
矢量文件映射;
弦线;
while(getline(filestream,line))
{
filemap.push_back(向量(line.begin(),line.end());
}
对于(int x=0;xfilestream您可以将所有行放入一个列表中,然后在两个循环中迭代并打印字符,第一个循环迭代单词的长度,第二个循环迭代文本文件的行。因此,您将在每次迭代中打印每行的一个字符

下面是一个示例代码(虽然是c语言,但只使用字符串数组)


您可以将所有行放入一个列表,然后在两个循环中迭代并打印字符,第一个循环迭代单词的长度,第二个循环迭代文本文件的行。因此,您将在每次迭代中打印每行的一个字符

下面是一个示例代码(虽然是c语言,但只使用字符串数组)

与其在源文件中反复尝试seek(),不如简单地读取整个源文件,然后从内存中的内容生成输出,这样更容易、更快

这听起来很像一个课堂作业,所以我不会简单地为你写答案。但是这应该为你指明正确的方向——包括一些PseodoCode

为避免痛苦,假定线长度和最大线的上限是安全的,即:

const int MaxLines = 100;
const int MaxLength = 80;

int lineno, linelength;

// array of char pointers for each line
char *lines[] = (*lines[])malloc(Maxlines * sizeof(char*));

// ReadLoop
lineno = 0;
while (not eof)
{
  getline(buffer);
  if (++lineno++ == 1)
  {
    linelength = strlen(buffer);
  }
  else
  {
    if (linelength != strlen(buffer))
    {
      cout "Line # " << lineno << " does not match the expected length";
      exit();
    }
  } 
  lines[lineno] = malloc(strlen(buffer)+1));
  strcpy(lines[lineno], buffer);
}

int cc, linecnt = lineno;

// now all data in memory, output "vertical data"
for (cc = 0; cc < linelength; ++cc)
{
  for (lineno=0; lineno<<linelength; ++lineno)
  {
     cout << lines[xx][yy]; // xx && yy left you to figure out
  }
  cout "\n";
}
const int MaxLines=100;
常量int MaxLength=80;
int lineno,linelength;
//每行的字符指针数组
char*lines[]=(*lines[])malloc(Maxlines*sizeof(char*);
//读循环
lineno=0;
while(非eof)
{
getline(缓冲区);
如果(++lineno++==1)
{
linelength=strlen(缓冲区);
}
其他的
{
if(线宽!=strlen(缓冲区))
{
cout“Line#”而不是在源文件中反复尝试seek(),这样,只需读取整个源文件就更容易、更快了
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

int main(int argc, char *argv[]) 
{
    std::fstream infile("foo.txt");
    std::vector<std::string> lines;

    std::string line;
    while(std::getline(infile,line)) {
        lines.push_back(line);
    }

    int m=lines.size();
    int n=lines[0].length();
    for(int i=0; i<n; i++) {
        for(int j=0; j<m; j++) {
            std::cout << lines[j].at(i);
        }
        std::cout << std::endl;
    }
    return 0;
}
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

int main(int argc, char *argv[])
{
    std::fstream infile("foo.txt");
    std::vector<std::string> lines;

    std::string line;
    std::getline(infile, line);
    int n = line.length();
    int m = 1+std::count(std::istreambuf_iterator<char>(infile),
           std::istreambuf_iterator<char>(), '\n');

    infile.clear();

    for(int i=0; i<n; i++) {
        for(int j=0; j<m; j++) {
            infile.seekg(j*m+i);
            std::cout << char(infile.peek());
        }
        std::cout << std::endl;
    }

    return 0;
}
//lines = no. of lines in file
fs.seekg(0,fs.beg);
fs.clear(); 
if(lineLen > 0)
{
for(;k<lineLen;k++){
    for(int i = 0;i<lines;i++){
        fs.seekg(k+i*(lineLen+2),fs.beg); //use lines+2
        if(fs.read (ch,1));
          cout<<*ch; 
    }
   cout<<endl;
}