| | |
Copy from one file to another
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 14
Reputation:
Solved Threads: 0
So I'm supposed to copy the word "computer" or "computers" from the text of one file and past the word each time to the new file. It compiles but it doesnt run right. Any input would be great.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cstring> using namespace std; int main() { char filename, filename2, word[20]; //char to read the input and output files as well as word from the file ofstream fout;//output file ifstream fin;//input file cout << "Enter you input file:"; cin>>filename;//stores the input filename fin.open("filename");//open input file cout << "Enter your output filename"; cin>>filename2;//stores output filename do { if(strcmp("COMPUTER", word) || strcmp ("COMPUTERS", word))//looks for both computer and computers in file { fout.open ("filename2");//open output file fout<<word;//prints the word in the output file } }while(word); fin.close();//closes input file fout.close();//closes output file return 0; }
C++ Syntax (Toggle Plain Text)
do { if(strcmp("COMPUTER", word) || strcmp ("COMPUTERS", word))//looks for both computer and computers in file { fout.open ("filename2");//open output file fout<<word;//prints the word in the output file } // read the word here from the input file in 'word' }while(word);//curently it is a bad loop
is not doing the job. You need to read word by word and then compare. Also there is no need re opening the output file
fout.open ("filename2");//open output file . You can find several examples in this forum on how to read a word from a file.
-Prateek
Last edited by Ancient Dragon; Dec 20th, 2007 at 6:21 pm. Reason: corrected icode tag syntax
I know I am. Therefore I am.
>> char filename, filename2, word[20];
You are attempting to tread char as if it were std::string. My suggestion is to get rid of those c-style character arrays and use the easier-to-use std::string instead
You are attempting to tread char as if it were std::string. My suggestion is to get rid of those c-style character arrays and use the easier-to-use std::string instead
C++ Syntax (Toggle Plain Text)
std::string filename, filename2, word; ... cout << "Enter your output filename"; getline(filename2,cin);//allow filename to contain spaces ofstream out(filename2.c_str()); while( fin >> word) { if( word == "COMPUTER" || word == "COMPUTERS") fout << word; }
Last edited by Ancient Dragon; Dec 20th, 2007 at 6:28 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- how to copy a file (Python)
- Copy file with a name change (ASP)
- Cannot copy file: Data error (cylcic redundancy check) (Windows NT / 2000 / XP)
- how to copy a file from one machine to another using beans? (Java)
- copy a file (Python)
- Cannot copy file: Data error (cylcic redundancy check) (Windows NT / 2000 / XP)
- Windows XP Pro install on new machine "could not copy file .." (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Linux & C++ compiler
- Next Thread: C++ Client: connect to MySQL from C++ using MySQL++
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






