Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy, Translate, and Save Text File

Reply
Thread Tools

Copy, Translate, and Save Text File

 
 
Michael Lester
Guest
Posts: n/a
 
      04-05-2013
On Friday, April 5, 2013 1:55:03 AM UTC-7, ?? Tiib wrote:
> On Friday, 5 April 2013 10:34:35 UTC+3, Michael Lester wrote:
>
> >

>
> > The following code appears to read all lines from my input file,
> > but only outputs the last line to my output file.

>
> That is exactly what your program does:


> > [...]
> > while (getline (myfile,XYZ));
> > std::cout << XYZ << std::endl;
> > [...]

>
> Putting ';' after while is dangerous because it is often hard to
> notice and reader (even yourself few months later) may misunderstand
> your original intentions. Use syntax like:
>
> while (getline (myfile,XYZ))
> { }
>
> As I understand you just want to remove that semicolon to output
> all lines read not only last line.


Thank you, ?? Tiib!
Thanks to Paavo, and the others who helped me, too.

With this last change (and moving my "output to file" routine up to replace
std::cout << XYZ << std::endl;
the program now seems to work as intended.

I will have to deal with replacing the headers in the input file now to getthe new headers into the new file, but I will spend some time fooling around with that myself to see what I can figure out.
 
Reply With Quote
 
 
 
 
Michael Lester
Guest
Posts: n/a
 
      04-06-2013
On Friday, April 5, 2013 9:14:57 AM UTC-7, Michael Lester wrote:

> the program now seems to work as intended.
>
> I will have to deal with replacing the headers in the input file now to get the new headers into the new file, but I will spend some time fooling around with that myself to see what I can figure out.


I have tried for the past five hours to figure out how to output the user selected input text file to a new text file. I will use the copy to manipulate the data (a multi-line header and a long list of nine character words). I will need to remove the first five characters of each word, but that willbe my next challenge.

I have written a test program which outputs the entire contents of the input file to the console as intended, and creates the new file, but does not put any data in the new file. I have pasted 40 lines of code below. If I getit working, I will attempt to integrate it into the code I posted here previously.

I would very much appreciate any help you are willing to give.

Sincerely, Michael

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile;
ofstream mynewfile;

cout << "Please enter the input file name and location> " << flush;
while (true)
{
myfile.close();
myfile.clear();
string myfilename;
getline( cin, myfilename );
myfile.open( myfilename.c_str() );
if (myfile) break;
cout << "Invalid file. Please enter a valid input file name and location> " << flush;
}

if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
ofstream mynewfile ("Outputfile.txt");
if (mynewfile.is_open())
{
mynewfile << line << endl;
}
}

myfile.close();
}

else cout << "Unable to open file";
return 0;
}
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      04-06-2013
On 2013-04-06, Michael Lester <(E-Mail Removed)> wrote:
> #include <iostream>
> #include <fstream>
> #include <string>
> using namespace std;
>
> int main () {
> string line;
> ifstream myfile;
> ofstream mynewfile;
>
> cout << "Please enter the input file name and location> " << flush;
> while (true)
> {
> myfile.close();


In the first iteration of the while loop, myfile is not associated
with an open file. Closing it is an error.

> myfile.clear();
> string myfilename;
> getline( cin, myfilename );
> myfile.open( myfilename.c_str() );
> if (myfile) break;
> cout << "Invalid file. Please enter a valid input file name and location> " << flush;
> }
>
> if (myfile.is_open())
> {
> while ( myfile.good() )
> {
> getline (myfile,line);
> cout << line << endl;
> ofstream mynewfile ("Outputfile.txt");


Why is mynewfile re-opened for every line in myfile?

> if (mynewfile.is_open())
> {
> mynewfile << line << endl;
> }
> }
>
> myfile.close();
> }
>
> else cout << "Unable to open file";
> return 0;
> }

 
Reply With Quote
 
Michael Lester
Guest
Posts: n/a
 
      04-06-2013

Thank you for your help Ike, and the others who have helped me make progress on this.

The attached code (without the indented code in the middle - see below) works to read a user-entered text file and then output values from the map to a new text file. However, I need to make a copy of the user input file first, because I want to modify its contents, and I don't want to risk damagingthe original file. (I want to remove the first five characters from each line of nine characters in the input file before testing them against the map. I also have to deal with a header in the input file, but that is too much for me right now.)

When I added the code to copy the file (indented in middle of code attachedbelow), it copied the file to a new file as intended, but then the map routine, which worked fine without the new code, returned the error "Sorry '' Not in move list." How can I fix the code so that both parts work to (1) copy the input file to a new file, and (2) return the map values to the output file?

Thanks for your help.

#include <iostream>
#include <map>
#include <string>
#include <fstream>
using namespace std;
int main()

{
std::map<std::string, std::string> move_list;

move_list["XXXXXXXXX"] = "ABCD";
move_list["YYYYYYYYY"] = "EFGH";
move_list["ZZZZZZZZZ"] = "IJKL";

for (std::map<std::string, std::string>::const_iterator ci = move_list.begin();
ci != move_list.end();
ci++

)
std::cout << (*ci).first << ' ';

std::cout << std::endl;

std::string TXT;

//New code to ask user for file name
ifstream myfile;
string line;
ofstream copyfile ("Outputfile.txt");

cout << "Please enter the input file name and location> " << flush;
while (true)
{
myfile.close();
myfile.clear();
string myfilename;
getline( cin, myfilename );
myfile.open( myfilename.c_str() );
if (myfile) break;
cout << "Invalid file. Please enter a valid input file name and location> " << flush;
}

if (myfile.is_open())
//New code to copy input file to new file
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;

{
copyfile << line << endl;
}
}

}

else cout << "Unable to open file";

//end of new code to copy input file
{
while(getline (myfile,TXT))

{

std:fstream mynewfile ("Testing.TXT", ios::app);
if (mynewfile.is_open())
{
mynewfile << (*move_list.find(TXT)).second <<std::endl;
mynewfile.close();
}
else std::cout << "Unable to open file";

}

myfile.close();
}

else std::cout << "Unable to open file";

if (move_list.count(TXT) == 0)
std::cout << "Sorry, `" << TXT << "' not in move_list.";
else

std::cout << std::endl;

}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
edit text in a file and save it again cyberco Python 3 11-20-2006 06:45 AM
How can I read pdf file and save content in text file ? Alex Smith ASP .Net 0 06-15-2005 06:05 AM
Save images and text in a single file Rud HTML 11 01-16-2004 04:08 AM
Save alternative file on right-click->save-picture-as Shahar Golan Javascript 5 10-16-2003 05:01 PM
beside save as htm and txt in IE , can i save as jpeg or other formats ? hycn office ASP General 2 10-10-2003 12:57 PM



Advertisments