输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
The first character in the input file is the last character in the output file and vice-versa
我想打印出输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然。但我坚持如何打印输出。
我需要使用数组。我将从输入文件读取到字符数组,并从数组写入输出文件。
例:
输入.txt: A B C D E H
输出.txt: H B C D E A
This is my code
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string FileName, FileName2;
string s, temp, FirstChar, LastChar;;
char again = 'Y';
bool close = false;
char MAXSIZE[1024];
while (close == false)
{
cout << "Open the file: ";
cin >> FileName;
ifstream ReadFromFile(FileName);
if (ReadFromFile.is_open())
{
cout << "Succeed to open the file!n";
// Read character from the input to array
while (!ReadFromFile.eof())
{
ReadFromFile >> MAXSIZE;
cout << MAXSIZE << " ";
}
cout << endl;
cout << "Enter the first character: ";
cin >> FirstChar;
cout << "Enter the last character: ";
cin >> LastChar;
swap(FirstChar, LastChar);
// I stuck at here
ifstream in(FileName);
cout << "Enter a name for a copy file: ";
cin >> FileName2;
ofstream out(FileName2);
while (getline(in, s))
out << s << "n";
cout << "Close the program and then open your copy file.";
cout << endl << endl;
close = true;
}
else{
cout << "Failed to open the file!n";
do {
cout << "Do you want to do it again(Y) or Close (N)? ";
cin >> again;
} while (again != 'y' && again != 'Y' && again != 'n' && again != 'N');
if (again == 'y' || again == 'Y')
close = false;
else
close = true;
cout << endl;
}
}
system("pause");
return 0;
}
您的任务(根据您的解释(需要:
1( 从输入文件读取到数组
2( 更改第一个和最后一个字符
3(将数组保存到输出文件
因此,不应从标准输入(键盘(询问第一个和最后一个字符。
#include <iostream>
#include <fstream>
using namespace std;
// filenames can be given as command line arguments
// change the code if you want to read them from standard input
int main(int argc, char* argv[])
{
ifstream inf;
ofstream outf;
size_t counter = 0;
const size_t MAXSIZE = 1024; // MAXSIZE - name of constant
char buffer[MAXSIZE]; // buffer - name of array
// check the command line arguments
// Alternatively you can define: string InpFileName, OutFileName;
// as it is in your code and enter values (cin >>) instead using argv
// if so, you should change inf.open(argv[1]); to inf.open(InpFileName);
// and outf.open(argv[2]); to outf.open(OutFileName);
if (argc != 3)
{
cerr << "Two arguments are required:" << endl
<< " 1) name of existing file (to read)" << endl
<< " 2) name of new file (to create)" << endl;
return 1;
}
// open files
inf.open(argv[1]);
outf.open(argv[2]);
// check files
if (!inf.is_open() || !outf.is_open())
{
cout << "ERROR: some trouble with files." << endl;
return 2; // stop the program
}
// process
// 1) reading
while (counter < MAXSIZE){
buffer[counter] = inf.get();
if (buffer[counter] != EOF)
{
counter++;
}
else
{
counter--; // the last character is not valid
break; // end of file
}
}
// 2) changing
char b = buffer[counter];
buffer[counter] = buffer[0];
buffer[0] = b;
// 3) output
for (int i = 0; i <= counter; i++)
{
outf.put(buffer[i]);
}
// close files
inf.close();
outf.close();
return 0;
}
更新:
澄清某些不可打印字符(如空格(是第一个或最后一个的情况的任务
看起来像是C++类的家庭作业问题。 一个可能有帮助的提示是将文件划分为 X 字节的块,并以相反的顺序读取这些块。
std::istream::seekg
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何从文本文件中读取数值,直到遇到字符类型?
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 尝试将字符串从文件读取到无符号字符向量中
- 如何在C++中反转文本文件中字符的顺序?
- 生成随机.txt文件,10行,10列。充满随机:"*"、"#"、"@"。然后将随机字符替换为'P'
- 如何存储 unicode 字符并将其输出到文件?
- 无法将整个文本文件复制到字符数组
- 仅读取文件末尾的最后 3 个字符,并将其另存为 int
- 将未签名的字符写入输出文件
- 从我的 2DgridArray 中的文本文件中获取每个字符
- ifstream 读取的字符与文件中写入的字符不同
- VSCode C++调试文件名中包含中文字符的文件时插件损坏
- 从文本文件 (C++) 生成字符矩阵
- 使用指针编译代码后,.cpp文件将变为随机字符
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- 跨平台UTF-8字符文件数据编码/解码