Solved

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
6
1,205 Views
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.

#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;
0
Comment
Question by:Diverden
6 Comments
 
LVL 22

Expert Comment

by:nietod
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.
0
 

Expert Comment

by:sumansamanta
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.
0
 
LVL 2

Expert Comment

by:MadYugoslav
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;
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
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 22

Accepted Solution

by:
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

#include<iostream.h>
#include<fstream.h>

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];
cin>>name;

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];
cin.getline(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

exit(1);

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;
cin>>name;
input.open(name);

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?
0
 
LVL 11

Expert Comment

by:griessh
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

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6875066
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now