从文件 F1 复制所有只包含一个单词的字符串到文件 F2
Copy from file F1 to file F2 all strings which containing only one word
我创建了一个文件,其中我用键盘填充了字符串,我的任务是将所有仅包含一个单词的字符串从我的第一个文件 (F1) 复制到第二个文件 (F2)。
#include <iostream>
#include <fstream>
#include <clocale>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
int i,n;
char buff[255];
ofstream fout("F1.txt"); // open file for filling
cout << "Enter number of strings ";
cin >> n;
cin.ignore(4096,'\n');
for(i=0;i<n;++i)
{
cout << "[" << i+1 << "-string]" << ":";
cin.getline(buff,255); // write strings from keyboard
fout << buff << "\n"; // fill the file with all strings
}
fout.close(); /close file
const int len = 30;
int strings = n;
char mass[len][strings]; // creating two-dimensional array as buffer while reading
const char ch = '\n';
ifstream fin("F1.txt"); //open file for reading
if (!fin.is_open())
cout << "File can not be open\n"; //checking for opening
else
{
for(int r = 0; r<strings; r++)
{
fin.getline(mass[r], len-1,ch);
cout << "String " << r+1 << " = "<< mass[r] << endl; // output strings from file(buffer)
}
}
return 0;
}
- Trim 字符串(函数删除字符串开头和结尾的空格)
- 现在搜索字符串中的空格:
- 如果找到:则不止一个字
- 否则:是单字串
#include <iostream>
#include <fstream>
#include <clocale>
#include <cstdlib>
#include <cstring>
using namespace std;
std::string trim(const std::string &s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && isspace(*it))
it++;
std::string::const_reverse_iterator rit = s.rbegin();
while (rit.base() != it && isspace(*rit))
rit++;
return std::string(it, rit.base());
}
int main()
{
int i,n;
char buff[255];
ofstream fout("F1.txt");
cout << "Enter number of strings ";
cin >> n;
cin.ignore(4096,'\n');
for(i=0;i<n;++i)
{
cout << "[" << i+1 << "-string]" << ":";
cin.getline(buff,255);
fout << buff << "\n";
}
fout.close();
const int len = 30;
int strings = n,flag=0;
char mass[len][strings];
const char ch = '\n';
ifstream fin("F1.txt");
if (!fin.is_open())
cout << "File can not be open\n";
else
{ ofstream f2out("F2.txt");
for(int r = 0; r<strings; r++)
{ flag=0;
fin.getline(mass[r], len-1,ch);
string str=trim(mass[r]);
for(int it=0;it<str.length();it++) {
if(str[it]==' ') {flag=1;break;}
}
if(flag==0) {
f2out << str << "\n";
cout << str << "\n";
}
}
f2out.close();
}
return 0;
}
阅读更多:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim(为 C++ 实现了类似功能)
我创建了一个文件,其中我用键盘填充了字符串,我的任务是将所有仅包含一个单词的字符串从我的第一个文件 (F1) 复制到第二个文件 (F2)。
#include <iostream>
#include <fstream>
#include <clocale>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
int i,n;
char buff[255];
ofstream fout("F1.txt"); // open file for filling
cout << "Enter number of strings ";
cin >> n;
cin.ignore(4096,'\n');
for(i=0;i<n;++i)
{
cout << "[" << i+1 << "-string]" << ":";
cin.getline(buff,255); // write strings from keyboard
fout << buff << "\n"; // fill the file with all strings
}
fout.close(); /close file
const int len = 30;
int strings = n;
char mass[len][strings]; // creating two-dimensional array as buffer while reading
const char ch = '\n';
ifstream fin("F1.txt"); //open file for reading
if (!fin.is_open())
cout << "File can not be open\n"; //checking for opening
else
{
for(int r = 0; r<strings; r++)
{
fin.getline(mass[r], len-1,ch);
cout << "String " << r+1 << " = "<< mass[r] << endl; // output strings from file(buffer)
}
}
return 0;
}
- Trim 字符串(函数删除字符串开头和结尾的空格)
- 现在搜索字符串中的空格:
- 如果找到:则不止一个字
- 否则:是单字串
#include <iostream>
#include <fstream>
#include <clocale>
#include <cstdlib>
#include <cstring>
using namespace std;
std::string trim(const std::string &s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && isspace(*it))
it++;
std::string::const_reverse_iterator rit = s.rbegin();
while (rit.base() != it && isspace(*rit))
rit++;
return std::string(it, rit.base());
}
int main()
{
int i,n;
char buff[255];
ofstream fout("F1.txt");
cout << "Enter number of strings ";
cin >> n;
cin.ignore(4096,'\n');
for(i=0;i<n;++i)
{
cout << "[" << i+1 << "-string]" << ":";
cin.getline(buff,255);
fout << buff << "\n";
}
fout.close();
const int len = 30;
int strings = n,flag=0;
char mass[len][strings];
const char ch = '\n';
ifstream fin("F1.txt");
if (!fin.is_open())
cout << "File can not be open\n";
else
{ ofstream f2out("F2.txt");
for(int r = 0; r<strings; r++)
{ flag=0;
fin.getline(mass[r], len-1,ch);
string str=trim(mass[r]);
for(int it=0;it<str.length();it++) {
if(str[it]==' ') {flag=1;break;}
}
if(flag==0) {
f2out << str << "\n";
cout << str << "\n";
}
}
f2out.close();
}
return 0;
}
阅读更多:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim(为 C++ 实现了类似功能)