title: C++ reading/writing to two different files (need help understanding which syntax to use)
body: <p>I'm currently working on this program for a class in my university. I've tried multiple approach with no success. I'm pretty sure it's just a conversion problem, but I want to understand the differences.</p>
  
  <p><strong>What the program supposed to do</strong> : We're to create a program that ask the user for two filenames. One will be an input and another will be an output. The program is supposed to read the input and write the line to the output while until the end of the input file is not reached.</p>
  
  <p><strong>My Code</strong> :</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt; //included for read/writing files
  #include &lt;string&gt; //Included this for getline to read the file
  
  using namespace std;
  
  int main() {
  
      ifstream infile; // Stream to read from input file
      ofstream outfile; // Stream to write to output file
      char inputfilename[80], outputfilename[80]; //declaring two character arrays to save the file names.
      string text;
  
      cout &lt;&lt; "What is the name of your input file (text.txt)" ; // Prompting user for input file name
      cin &gt;&gt; (inputfilename); // Getting input file
      infile.open(inputfilename, ios::in); // Opening the input file.
      cout &lt;&lt; "What is the name of your output file" ; // Prompting user for output file name
      cin &gt;&gt; (outputfilename);
      outfile.open(outputfilename, ios::out);
      if(!infile) { // If cannot open file 
  
          cout &lt;&lt; "There was an error opening your file!" ;
          return 1; 
  
      }
      if (!outfile) {
  
          cout &lt;&lt; "There was an error opening your file!" ;
          return 1; 
  
      }
      while (infile.eof()==0) {
  
          fgets(text, 80, infile);
          fprintf(outfile,"%d.  %s\n", text);
  
      }
  
      infile.close();  // Closing input file
      outfile.close(); // Closing output file
  
      return 0;
  }
  </code></pre>
  
  <p><strong>What I've tried</strong> : I didn't know if it was being affected by how I opened the file. I previously tried.</p>
  
  <pre><code>    ifstream infile;
      ofstream outfile;
      char text, inputfilename[80], outputfilename[80]; &lt;----- 1
      cout &lt;&lt; "What is the name of your input file (text.txt)" ;
      gets(inputfilename); &lt;----- 2
      infile.open(inputfilename);
      cout &lt;&lt; "What is the name of your output file" ;
      gets(outputfilename); &lt;----- 2
      outfile.open(outputfilename);
  </code></pre>
  
  <p>1) I switched char I previous tried</p>
  
  <pre><code>char text 
  char text[80]
  char *text[80]
  </code></pre>
  
  <p>2) Would switching how getting the file name change anything in the while loop(I previous tried getline and gets)? Additionally the "f" in front of fgets/fprints/etc are always associated with a file stream?</p>
  
  <p>Note: My teacher gave us the hint. </p>
  
  <p>"Suppose you read a line from the input file into a string variable called str using the following statement: fgets(str, 80, infile);You can add a line number and save the line with the line number to the output file using the same statement using: fprintf(outfile,"%d.  %s\n",Next_line_number++, str);"</p>
  
  <p>from this I tried : </p>
  
  <pre><code>while (infile.eof()==0) {
  
          fgets(text, 80, infile);
          fprintf(outfile,"%d.  %s\n", text);
  
      }
  </code></pre>
  
  <p>as well as </p>
  
  <pre><code>while (infile.eof()==0) {
          fgets(text, 80, infile);
          fputs(text, outFile);
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>while (infile.eof()==0) {
          getline(infile, text);
          fprintf(outfile,"%d.  %s\n", text);
  
      }
  </code></pre>
  
  <p>I also tried making a long and using that to increment the line number. I'm fairly new to programming; if any of the methods I'm using our dated please let me know (on some sites they were saying fgets is dated and not supported on cx11 or some version of C++)! I want to understand the concepts vs just get the programming running. <strong>Should note Lines 34-35</strong> are where my code is always erroring out and it's </p>
  
  <blockquote>
    <p>cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'char*' for argument '1' to 'char* fgets(char*, int, FILE*)'</p>
  </blockquote>
  
  <p>I figured I was getting this because it has a pointer to the file and I'm asking the user vs having the file declared in the program. This is causing a conversion that causing my error.</p>