Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interesting question. Something which should be cross platform is like the example below (I have tested on Windows and *nix but do not have a mac to test on unfortunately). Basically, you will read the initial file and extract its data (in the case of the example, it assumes that the file is formatted exactly as you have mentioned above) and store that data somewhere. Then, write the data back to the file from which you read it.</p> <pre><code>/** * File Input/Output * * Input file is also output file * * 12/13/10 */ #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;sstream&gt; #include &lt;string&gt; using namespace std; int main() { // Get data and store it ifstream in("output.txt"); // Simple error checking if(!in.is_open()) { cout&lt;&lt; "There was an error opening output.txt!" &lt;&lt; endl; return 0; } cout&lt;&lt; "Reading file..." &lt;&lt; endl &lt;&lt; endl; // Strings are quicker to implement string tmp; string data; int tmpi = 0; // Here is where we store the input - the stringstream allows us to handle // multiple variable types and convert them. NOTE: there is no error checking here, // so wrong types _WILL_ throw errors (format needs to be one number per line) stringstream ss(stringstream::in|stringstream::out); while(getline(in,tmp)) { tmpi = 0; // Reset ss.str(string()); ss &lt;&lt; tmp; ss &gt;&gt; tmpi; tmpi *= 2; // Reset it again so we can get the doubled value ss.clear(); ss.str(string()); ss &lt;&lt; tmpi; data.append("Enter a number: "+tmp+"\r\n"+"Twice your number is: "+ss.str()+"\r\n"); } in.close(); // Output handling ofstream out("output.txt",ios::binary|ios::trunc); // Delete everything which was in the file? // Simple error checking if(!out.is_open()) { cout&lt;&lt; "There was an error opening output.txt!" &lt;&lt; endl; return 0; } cout&lt;&lt; "Generating output..." &lt;&lt; endl &lt;&lt; endl; // Write to the file out.write(data.c_str(),data.size()); out.close(); cout&lt;&lt; "Done!"&lt;&lt; endl; cin.get(); // Pause momentarily return 0; } </code></pre> <p>My original input file was:</p> <pre><code>12 2312349 324843 3249 0909 </code></pre> <p>The output was:</p> <pre><code>Enter a number: 12 Twice your number is: 24 Enter a number: 2312349 Twice your number is: 4624698 Enter a number: 324843 Twice your number is: 649686 Enter a number: 3249 Twice your number is: 6498 Enter a number: 0909 Twice your number is: 1818 </code></pre> <p>Hope this helps!<br /><br /></p> <p>Good luck!<br /> Dennis M.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload