Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read from a file then write to another with condition

Status
Not open for further replies.

miketm

Programmer
Oct 19, 2003
18
AU
Hi everyone,

Please provide me some help on file reading and writing. First, I would like to know how to read data from a file and then write it to another file. I have been searching and seen examples of writing to the screen (Cout)only or writing some short strings to a file but not the whole data file from the input file.

Second, Is it possible to manipulate the writing by setting some conditions such as: If the word "ONE" appears in the file then do someting, "TWO" and "THREE" do something else ? as it can read a single character at a time only so how can it know the word "ONE, TWO or THREE" occur? We can use getword() or something but that people say it will cause errors (problem in new-line character).


ifstream OpenFile("test.doc");
char ch;
while(!OpenFile.eof())
{
OpenFile.get(ch);
cout << ch; //wrting to screen not to a file
}
OpenFile.close();

So how can we write what read from &quot;test.doc&quot; to another file&quot;? Please help. Thanks
 
Create file:
ofstream fout(&quot;file_name&quot;); // in constructor, or later:
fstream fout;
fout.open(&quot;file_name&quot;,ios_base::eek:ut|pos_base::trunc);
file_name may include full path. It may be STL string or char array, of course (not only text string literal).
Now you can write to this file, for example:
fout << &quot;Start of my file&quot; << endl; // or
fout.write(&quot;Start&quot;,5); // Five chars only; or
fout.put('\n'); // One line terminator only etc
There are no error handling in my examples. Add it.
The second question is more difficult. No 'silver bullet' or any built-in (language) conditions to recognize special words in a file input. You must write the program for it.
It depends on additional assumptions about your input file contents. For example, if control words (ONE, TWO etc) fit entirely into text line (no '\n' between letters), you may write:
ifstream fin(&quot;control.txt&quot;);
string s;

while (getline(fin,s,'\n')) // Read about eof cond in stream
{
if (s.find(&quot;ONE&quot;) >= 0)
{
Do ops for ONE keyword (write function for this task)
}
else if (s.find(&quot;TWO&quot;) >= 0)
{
Do ops for TWO keyword (or place your code here)
}
...
else
{
Do nothing or report error or what else
}
}
When you need byte by byte file extraction, you may program the kind of a finite state automata. Read a byte, set state var if you gets 'O' (or another state for 'T'), in 1st case you wait 'N' then 'E' (skip '\n'), in 2nd case you wait 'W' or 'H' etc. It's not so hard, but cumbersome and error prone approach. Try 1st approach if possible.
Don't forget include <string> header and using namespace std declaration.
There are many ways to program the simple keyword scanner/parser. It's your task to code it.
 
Dear ArkM,

Thank very much for your help. You show me a good direction on what to do, especially on the second part of the question. I don't know what to say but I am really appreciated the help from you and Globos (he's helped me too). It saves me from head ache , twist and turn for last couple nights. I will try hard and hope it will turn out alright. Thanks you, bye for now.

Miketm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top