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.

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;
cout<<"Cannot open outfile.data Aborting.!!!"<<endl;
cout<<"Cannot open file"<<"Aborting."<<endl;
cout<<"Average of"<<count<<"number is"<<average<<endl;
output<<"Average of"<<count<<"number is"<<average<<endl;
return 0;
Who is Participating?
nietodConnect With a Mentor Commented:
DiverDen, you edited the question history.  You shoudln't do that.  If you edit the question history we don't get e-mail notifications of what yoyu've done so you get ignored.

next time post your new information in a comment.  Then you will get faster responses!

First of all


Are old include files that you should never use.   I have a pre-written statement on this


The official STL (Standard Template Library) files are all extensionless files, like <iostream>, <vector>, and <list>.  You may find that your compiler has files with these names but a .h extension, like <iostream.h>, <vector.h>, and <list.h> but these are not the standard files.  These files are old, non-standard files that are provided for compativbility for old programs that used them before the standard files were settled upon.  You want to avoid usign this files if you have the standard ones availalbe.  The non-stnadard files may not have all the features of the standard ones, they also likely to have more bugs and other problems, and also since they are not standardized, they will varry from compiler to compiler, and might not even exist on many compilers.  So avoid using these files if you have the extenionless, standardized files.

Now there is one big difference between the standardized files and many of the non-standardized files.  The standardized files place all of their symbolic names in the "std" namespaces.  This is so that the names defined in these files don't clash with names you write in your program.  So in order to access the names inside the standard STL files you need to prefix them with "std::" like

std::vector<std::string> StringArray;

If you do not wish to use those "std::",  you can instead add a using directive like

using namespace std;

after you include the STL  include files.  Then these names will all be brought into your namespace.


The code

char name[13];

is very risky.  I the user enters a string that is more than 12 characters long the stream will try to store that data in name and it will overlfow name and corrupt other parts of memory.  This leads to unpredicatable behavior and possibly crashes.   A better tehcnique is to use getline, like

char name[13];

this tells the stream object exactly how long that array is so that it doesn't overlfow it.   An even better technique is to use a string object, for example

string name;
cin >> name;

A string object will expand its storage to store a string of any required length.  So this is the best solution of all.  No matter how much the user enters, ti will be safely stored in the string--unless you run out of memory.

In C++ never use


That is not safe.  Its for C only.  You must either throw an exception--probably too advanced a technique for you now--or return.  since this is main you can simple use a return statement to end the program.

>>  A simple solution is that you also accept full-path along with the filename.
right, just make the file name variable longer so the user can type a path into it as well, like

char name[80];
cout<<"Enter a file name. 12 characters or less<<endl;

Then the user can type something like "C:\somedir\somefile.txt" to indicate exactly what file they want to work with.

Now the question that raises is what about the output file?  Where does it go?  i can see 3 posibilities

A) Always in the current working directory.
B) always in the directory with the source file
C) in a directory the user specifies.

You have A) currently.  for C) you can do the same thing as A).  Allow the user to enter a file name and give them enough room to enter a path.   For B) you have the most amount of work.  you have to extract the path the user entered for the source file and then use it to generate a path for the destination file.  You have to scan the path the user entered for the source file from the end of the string back towards the front.  Look for either a "\" or ":" or the start fo the string.  (Assuming Unix/DOS/windows)  Take the part of the string before the ":" or "\" (iincluding  the symbol) and copy it to a new string, then tack on the output file name.

Now it seems to me like your code above does somethign very different than the code you originally requested?
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.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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;
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
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: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Per recommendation comment force/accepted by

Community Support Moderator
Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.