【问题标题】:Copy from file F1 to file F2 all strings which containing only one word从文件 F1 复制所有仅包含一个单词的字符串到文件 F2
【发布时间】:2020-03-09 20:52:02
【问题描述】:

我创建了一个文件,在该文件中我用键盘填充字符串,我的任务是从我的第一个文件 (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;
}

【问题讨论】:

  • 您的实际问题是什么?此外,char buff[255]; 会更好地作为std::string。并且char mass[len][strings] 是不合法的,因为strings 的值在编译时是未知的,可变长度数组在某些编译器中是非标准扩展,请改用std::vector
  • 从文件 F1 复制所有只包含一个单词的字符串到文件 F2。
  • 那不是问题,那是目标。你的问题是什么?你到底有什么问题?请更具体。您想将行复制到第二个文件,但此代码中没有尝试这样做。而且这段代码比完成这么简单的任务要复杂得多。
  • 欢迎来到 Stackoverflow!那么具体是什么不起作用?
  • 如何检查每一行是否只包含一个单词?

标签: c++ arrays string file iostream


【解决方案1】:
  1. 修剪字符串(函数从字符串的开头和结尾删除空格)
  2. 现在搜索字符串中的空格:
    • 如果找到:则有多个单词
    • 否则:它是一个单字串
#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++ 实现的类似函数)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    相关资源
    最近更新 更多