Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Who can help me with c/c++ input / output program .?

Well I need a simple

C/C++ program which will read line from a text file . and will write all the words present in a line.

Word will be written to the file as soon as it encounters a comman semicolon or fullstop or space .

Example ::

Input :---->

This is a computer. It's a machine. Computer works fast .

Output :---->

This

is

a

computer

it's

a

machine

computer

works

fast

2 Answers

Relevance
  • ?
    Lv 7
    1 decade ago
    Favorite Answer

    We all know strtok. I did not want to explain it here.

    This is merely to outline how unpleasant writing

    a function is, compared to using boost or stl,

    where most programming problems studied

    at school are resolved - in one line :O)

    #include<iostream>

    #include <iterator>

    #include <string>

    #include <fstream>

    using namespace std;

    const string strFilename="output.dat";

    void output(string strWrite, const string& strSeparator)

    {

    bool bFlag = false;

    char *c_str = strdup(strWrite.c_str());

    int iLen = strWrite.length();

    for (int i = 0; i < iLen; i++) {

    for (int j = 0; j < strSeparator.length(); j++) {

    if (c_str[i] == strSeparator[j]) {

    j = 0;

    if(!bFlag) {

    c_str[i++] = '\n';

    bFlag = true;

    }

    else {

    strcpy(&c_str[i], &c_str[i + 1]);

    --iLen;

    if (!iLen) break;

    }

    }

    }

    bFlag = false;

    }

    ofstream os(strFilename);

    if (os.is_open()) {

    os << c_str << endl;

    }

    free(c_str);

    }

    void tok_main()

    {

    string strSeparator = ":,;. ";

    string strInput = "This is a computer. It's a machine. Computer works fast .";

    output(strInput, strSeparator);

    ifstream is(strFilename);

    copy(istream_iterator<string>(is), istream_iterator<string>(), ostream_iterator<string>(cout, "\n"));

    }

  • ?
    Lv 7
    1 decade ago

    There is no language called C/C++. Which one do you want to use?

    Either way, it's a tokenization problem, solved numerous times by numerous people. Search for "how to tokenize a string in C" or for "how to tokenize a string in C++" and you'll have more solutions to your question than you'll ever need!

    To give a working example, I'll use the C++ library boost.tokenizer (from http://www.boost.org,/ although it's available in many development environments out of the box)

    #include <iostream>

    #include <algorithm>

    #include <string>

    #include <fstream>

    #include <iterator>

    #include <boost/tokenizer.hpp>

    using namespace std;

    using boost::tokenizer;

    using boost::char_separator;

    int main ()

    {

    ifstream input("test.txt");

    string s;

    getline(input,s);

    ofstream output("test.out");

    tokenizer<char_separator<char> > tok(s, char_separator<char>(" ,;."));

    copy(tok.begin(), tok.end(), ostream_iterator<string>(output, "\n"));

    }

    test run:

    ~ $ cat test.txt

    This is a computer. It's a machine. Computer works fast .

    Output :---->

    ~ $ ./test

    ~ $ cat test.out

    This

    is

    a

    computer

    It's

    a

    machine

    Computer

    works

    fast

Still have questions? Get your answers by asking now.