Link to home
Start Free TrialLog in
Avatar of Diverden
Diverden

asked on

Write a program that reads text from one file and writes an edited version of the same text to another <br>file.

Write a program that reads text from one file and writes an edited version of the same text to another file. The edited version is identical to the unedited version except that every string of two or more consecutive blanks is replaced by a single blank. Thus the text is edited to remove any extra blank characters. The program should define a function that is called the input and output-file streams as arguments. Make up a test file to see if it works.

I have the following code already written, but I am not sure how to get it to read a test file, how do I specify the path so that it will automatically find the correct path.

#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
int main()
{
ifstream input;
ofstream output;
char name[13];
double value=0,average;
double sum=0;
int count=0;
cout<<"Enter a file name. 12 characters or less<<endl;
cin>>name;
input.open(name);
output.open("outfile.dat")
if(!output)
{
cout<<"Cannot open outfile.data Aborting.!!!"<<endl;
exit(1);
}if(!input)
{
cout<<"Cannot open file"<<"Aborting."<<endl;
exit(1);
}
input>>value;
while(input)
{
count=count+1;
sum=sum+value;
input>>value;
}
average=sum/count;
cout<<"Average of"<<count<<"number is"<<average<<endl;
output<<"Average of"<<count<<"number is"<<average<<endl;
input.closed();
output.close()
return 0;
Avatar of nietod
nietod

We cannot do your schoolwork for you.  That is unethical and is grounds for removal from this site.  (for both you and the experts involved.)  

We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?

***********************************

To get started, why don;t you write a program that opens a file and reads characters from it.  At least try something.   Or try to ask a question to get you started.
See, you are only taking the file-name as input from the user, not the file-path. Now when you specify only the file-name in ifstream class's constructor, it searches for that file in the directory in which the application itself is located (i.e current working directory). So, in this case the filename which you have specified must be present in your application's directory, otherwise ifstream will not be able to locate the file. Same rule holds for ofstream class as well.

    A simple solution is that you also accept full-path along with the filename. If you want more elegant solution then you can use Windows Registry (if you are working on Windows platform), there you create a key with the name of your application, and under that create two sub-keys called "input_dir" and "output_dir" and store the input path and out put path in these subkeys.

   So, in this implementation, at the startup, your app must read the registry and store the value of input_dir and output_dir in two seperate variables, which you can use throughout your program. Here you can search for files (based on extension may be) in the input_dir, if found, proccess them and store the new file(s) in the output_dir. And remove the processed file to some other location or simply delete them from the input directory.

If, you are on Unix/Linux, then you can use .ini files in a specific location, as substitute for registry.

Actually , all thiese strategy depends on your app's reqirement.
Replace declaration of your main function with:
int main(int argc, char* argv)
and in your code replace lines where get input filename from user with:
CString name;
name=argv[1];
When You start program start it with one argument which will be name of the input file. For example:
[program_name].exe c:\work\InFile.txt
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "nietod" comment(s) as an answer. (Unfortunatelly with an edited question, I can't follow the complete discussion)

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange