Saturday 15 February 2014

how can I change a file content in c++? -


I have this program that makes positive changes in positive numbers in my file. It works, but the negative numbers do not change in the file. For example, if I have this number in my file: 12 2 -3 -1 When I run the program, then the sum of the numbers will be 18, but when I open my file again, I get 12 2 -3- 1 get. What should I do to see 12 2 3 1? Here is my code:

  #include & lt; Iostream & gt; # Include & lt; Fstream & gt; #include & lt; String & gt; using namespace std; Int main () {string name; Cin & gt; & Gt; Name; Int number; Int num = 0; Ifstream myFile (name, iOS :: in); Offstream mine (name, iOS :: AP); Whereas (mimefile> gt; number) {num + = (number & lt; 0? -umber: number); My & lt; & Lt; Number; } Cout & lt; & Lt; "Num =" & lt; & Lt; Number & lt; & Lt; Endl; System ("pause"); Return 0; }    

  string name; Cin & gt; & Gt; Name; Int number = 0; Int sum = 0; String outname = name + ".post.txt"; Ifstream myFile (name, ifstream :: in); Offream mine (outname, offream :: out); Whereas (mimefile> gt; number) {number = (number & lt; 0? -umber: number); Yoga = number; My & lt; & Lt; The number & lt; & Lt; ''; } MyFile.close (); Mine.close (); Cout & lt; & Lt; "Sum =" & lt; & Lt; Amount & lt; & Lt; Endl; System ("pause");    

No comments:

Post a Comment