2

Here is a brief discription of the books programming project I am trying to do...

Write a program that reads in a C++ source file and converts all ‘<’ symbols to “<” and all ‘>’ symbols to “>” . Also add the tag <PRE> to the beginning of the file and </PRE> to the end of the file. This tag preserves whitespace and formatting in the HTML document. Your program should create a new file with the converted output. To implement this, you should write a function ‘convert’ that takes the input and output streams as parameters.

I am having issues trying to get the program to work correctly. What's happening is the program will create a new file with .html but it is not converting anything in the file. (i.e. adding <PRE> to the beginning and </PRE> to the end and converting all '<' symbols to &lt and '>' to &gt).

I've been messing with it for a while now and I'm honestly not sure where I am going wrong. I'm super new to programming in general and even more new to c++ so please be nice haha.

Here is my code, any help is greatly appreciated!!

Thanks!

Scott

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

//     main function
int main() {
    // Input file to convert
    string filename;
    // Output file with .html on the end
    string outputname;

    char c;
    int i;
    ifstream inStream;
    ofstream outStream;

    cout << "Enter filename you woudl like to convert: " << endl;
    cin >> filename;

    // Open the input file
   inStream.open(filename.c_str());
   if (inStream.fail()) {
       cout << "I/O failure opening file." << endl;
       exit(1);
   }

  // Create the output file
  outputname = filename + ".html";
  outStream.open(outputname.c_str());

  // First, output the <PRE> tag
  outStream << "<PRE>" << endl;

  // Loop through the input file intil nothing else to get
  while (!inStream.eof()) {
        inStream.get(c);    // Get one character
        // Output &lt; or &gt; or original char
      if (c == '<') {
          outStream << "&lt;";
      }
      else if (c=='>') {
          outStream << "&gt;";
      }
      else outStream << c;
  }
  // Output end /PRE tag
  outStream << "</PRE>" << endl;
  inStream.close();
  outStream.close();
  cout << "Conversion done.  Results are in file " << outputname << endl;
 }
0x499602D2
  • 87,005
  • 36
  • 149
  • 233
sjud9227
  • 21
  • 3
  • There is nothing particularly wrong with your code (aside from the antipattern `while (!inStream.eof())`), it works exactly as you describe. – user657267 Nov 10 '14 at 03:57
  • Really? Every time I run it, it creates a .html file but it's exactly the same text as the original, it doesn't convert what I expect it should. What do you mean by antipattern? – sjud9227 Nov 10 '14 at 04:15
  • [`while(!eof)`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) is a very common beginner c++ antipattern, but your program still works regardless (you should change the loop to something like `for (char c; inStream.get(c); )` instead. Your issue must be related to the files or their paths somehow, make sure the output file has been deleted before trying again. To avoid the files issue you could try command line redirection instead and just use `std::cin` and `std::cout`, although I suppose that doesn't meet the conditions of your book. – user657267 Nov 10 '14 at 04:20
  • Maybe it's how I'm testing it. So, when I try to convert a .cpp file it has to be in the same folder correct? Though instead of copying an additional .cpp file into the same folder I have just been trying the actual file of the program if that makes sense. So the programs name is htmlConvert.cpp and I just add that for the user input. It creates a new .html file but that's it no other change. Thanks for the tip about the loop I'll look into changing it to a for loop. – sjud9227 Nov 10 '14 at 04:34
  • I you only provide a filename then the file will have to be in the same folder as your current working directory, which may or may not be the same folder where your program resides. – user657267 Nov 10 '14 at 04:38
  • On a completely unrelated note, I could be wrong but the fact that your book uses UPPERCASE tags for html means it might be a little old, lowercase became far more common after xhtml adoption (with HTML5 either's fine, but lowercase seems to be the norm nowadays). I'd be wary learning from an old resource as C++ has changed tremendously in the last few years. – user657267 Nov 10 '14 at 04:39
  • That's so weird. So you actually ran it and it worked fine for you? I can't see what I'm doing wrong. As for the book, it is the newest edition and I'll have to check but I think it's 2013 – sjud9227 Nov 10 '14 at 05:13
  • Yeah, it worked fine with no changes. I guess I was wrong about the book, never mind! – user657267 Nov 10 '14 at 05:14
  • I'm sorry to keep bothering you but I just want to figure out what I'm doing wrong. So, you run it and enter htmlConvert.cpp it creates a new file htmlConvert.html and when you open that file it's converted all '' to &gt, and added a
     and 
    to the beginning and end of the file? Any suggestions on what I could be doing wrong?
    – sjud9227 Nov 10 '14 at 05:32
  • The output file should be `htmlConvert.cpp.html`, maybe that might give you some clues as to what's going on. – user657267 Nov 10 '14 at 05:33
  • That was actually a typo, it is saving as htmlConvert.cpp.html – sjud9227 Nov 10 '14 at 06:11
  • But still not converting for me. – sjud9227 Nov 10 '14 at 06:14
  • So to summarize: You compile your file `htmlConvert.cpp` to `htmlConvert` (or whatever the binary name turns out to be). Using your shell, you `cd` into the same directory (which currently **only** has those two files), type `htmlConvert`, and when prompted you type `htmlConvert.cpp`, and this creates `htmlConvert.cpp.html` in the same directory but without changing the text? – user657267 Nov 10 '14 at 06:18
  • If you are absolutely sure that my comment above is correct, word by word, then I have no idea what the issue is. Check the timestamps of each file in the process just to make sure you're not using an older file at any point in the process. – user657267 Nov 10 '14 at 06:33
  • What exactly does this mean... "Using your shell, you cd into the same directory (which currently only has those two files)" – sjud9227 Nov 10 '14 at 06:38
  • What OS are you using, and how do exactly you run your program? Go through the exact steps you take. – user657267 Nov 10 '14 at 06:39
  • Ok, I have a mac. I'm using Eclipse for c++. I build and run the program, it asks the user for a file name, I put htmlConvert.cpp, it says it ran correctly. So, I open up the file where the htmlConvert.cpp is in the project folder, there is a new file htmlConvert.cpp.html. I double click on that file and it opens in a browser. It opens up and the code in that file is exactly the same. I appreciate your patients with me by the way I'm sure its frustrating lol. – sjud9227 Nov 10 '14 at 18:53
  • "You can test your output file by opening it with a web browser. The contents should appear identical to the original source code." I'm an IDIOT! I misread this last line. Plus the sample code makes it seem like it should be producing code with the changes included in it. Sorry for the hassle and thanks for your patients! – sjud9227 Nov 10 '14 at 19:09
  • Yes well that would explain it :) – user657267 Nov 10 '14 at 21:28

0 Answers0