Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Posted on 2001-07-07
Last Modified: 2012-06-27
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;
Question by:Diverden
LVL 22

Expert Comment

ID: 6261777
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.

Expert Comment

ID: 6263836
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.

Expert Comment

ID: 6264573
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 22

Accepted Solution

nietod earned 100 total points
ID: 6264767
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?
LVL 11

Expert Comment

ID: 6825602
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


Expert Comment

ID: 6875066
Per recommendation comment force/accepted by

Community Support Moderator
Experts Exchange

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
Unable to build a Win32 solution with EXE and DLL projects inside: Visual Studio 2010 3 105
C++ assignment question 7 236
C++ to C# code conversion issue 4 112
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question