ファイルを垂直方向にトラバースする必要があります。ファイルの内容が次の場合:
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();
}
私はファイル処理に少し慣れていないので、間違いを見つけることができませんでした。また、これに従うためのより良いアプローチはありますか?