Computer Science Canada

Output File with same name portion as Input File ?

Author:  hmafia [ Sat Feb 12, 2011 7:09 pm ]
Post subject:  Output File with same name portion as Input File ?

Hey guys.

So I have a project due soon and basically what I have to do is create a C++ console application that converts an old-style text file into a format more appropriate for use in a computer, smart-phone, or e-book reader. Specially, this program converts an ASCII text file to an xhtml 1.0 file containing the same textual content as the original ASCII text file.

The program will operate as a command-line utility (not a console input program).

Here's the beginning code.


code:
#include <iostream>
#include <fstream>
using namespace std;
int main( int argc, char* argv[] ) {
    ifstream infile( argv[1] );
    char ch = 0;
    while( infile.get( ch ) ) {
        cout.put( ch );
    }
}


Can I get a clue to do the following:
Create a new output file with same name portion as the input filename ?

Author:  md [ Sun Feb 13, 2011 9:07 pm ]
Post subject:  RE:Output File with same name portion as Input File ?

Overwriting existing files is generally a bad idea; so if it's not explicitly stated as a requirement I'd write your output to a second file.

Otherwise you have a couple of options:
1) write the new file to a temporary location, then replace the original with the temporary file after you're done creating it.
2) create the new document entirely in memory (think a large character buffer) and then write the buffer into the file once you're done
3) read in a bit of the file, translate, and then write back to the file. This has complications as the new data is almost certainly going to be longer then the original which means you need to read in enough to cover the difference. Unless you have very limited memory I'd avoid this one.

Author:  hmafia [ Mon Feb 14, 2011 5:21 pm ]
Post subject:  Re: Output File with same name portion as Input File ?

The output file doesn't have to be overwritten onto the input file. What I'm trying to do is convert a .txt file to .html with the same name portion. For example, if the file name is Pluto.txt (with a title and paragraphs), once I drag and drop it onto my .exe coded file it would convert the .txt file to .html with the same name (Pluto.html) and therefore would launch in a browser. I'm having problems starting this project so any help would be grateful. I'm a beginner in C++ and have little experience with JAVA.

EDIT: Alright guys i'm running out of time, and i've spent all day reading about different classes and what not in C++ on cplusplus.com but i'm still mind boggling. I understand how to write and read to a file but I can't understand how to do what i've mentioned above ^. It's tough to learn programming, seems like you've got to teach yourself everything.. I've tried asking some members in my classroom but no one seems to want to help ? So I would really appreciate it !!

Author:  OneOffDriveByPoster [ Mon Feb 14, 2011 7:42 pm ]
Post subject:  Re: Output File with same name portion as Input File ?

It sounds like you want to look into basic_string and see how you figure out where the last "." is and then replace the text from that point with your new extension.

Author:  hmafia [ Mon Feb 14, 2011 7:53 pm ]
Post subject:  Re: Output File with same name portion as Input File ?

Okay here we go, thank you, i'll look into it

Author:  md [ Mon Feb 14, 2011 10:50 pm ]
Post subject:  RE:Output File with same name portion as Input File ?

So... you open the original file for reading and open the new file for writing.

10 Start by writing an HTML header to the new file and opening a body and paragraph tag
20 Read a line from the new file
30 If it's blank close the paragraph tag and open a new one
40 else write the line you just read to hte new file
50 if there is still lines to be read goto 20
60 close the paragraph tag, body tag, and html tag
70 close the input and output files.

Easy as pie, no need for anything more complicated then fstream and string.


: