1

I created file which in which I fill strings with a keyboard and my task is to copy from my first file(F1) to second file(F2) all strings which contain only one word.

#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;
}

image

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Olafus
  • 7
  • 3
  • 1
    What is your actual question? Also, `char buff[255];` would be better as a `std::string` instead. And `char mass[len][strings]` is not legal as the value of `strings` is not known at compile-time, variable length arrays are a non-standard extension in some compilers, use `std::vector` instead. – Remy Lebeau Nov 14 '19 at 00:23
  • Copy from file F1 to file F2 all strings containing only one word. – Olafus Nov 14 '19 at 00:25
  • 2
    That is not a question, that is a goal. What is your QUESTION? What are you having TROUBLE with exactly? Please be more specific. You want to copy lines to a second file, but there is no attempt in this code to do that. And this code is much more complex than it needs to be for such a simple task. – Remy Lebeau Nov 14 '19 at 00:26
  • Welcome to Stackoverflow! So what *specifically* is not working? – Kevin Nov 14 '19 at 00:28
  • how to check each line for containing only one word? – Olafus Nov 14 '19 at 00:28
  • That question is a little ambiguous. Do you want to ignore lines that contain a specific word within a group of words? Do you want to ignore lines that consist of just 1 word total? Please be SPECIFIC. Please [edit] your question to provide an EXAMPLE of what you want the output to look like. – Remy Lebeau Nov 14 '19 at 00:30
  • Getting the number of strings from the user seems odd. I would expect the number of strings to be determined by the file read in. – user4581301 Nov 14 '19 at 00:32
  • I have: Strings in file ;I need to output: only those strings which have only one word – Olafus Nov 14 '19 at 00:33
  • Option 2 of the first answer to [Read file line by line using ifstream in C++](https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c) can be used for inspiration. – user4581301 Nov 14 '19 at 00:34
  • In C++, you can read a string from a file that is one word (default behavior when using `operator>>`. So, please clarify your definition of string, word and how you are reading. – Thomas Matthews Nov 14 '19 at 01:04

1 Answers1

0
  1. Trim string (Function removes whitespaces from starting and end of string)
  2. Now search for white spaces in string :
    • If found: then there is more than one word
    • Else: it is a single word string
#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;
}      

Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim (Implemented similair function for C++)