Hamid Hoseini
Hamid Hoseini

Reputation: 133

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

I have this program that changes negative numbers to positive in my file. It works, but negative numbers in the file don't change. for example, if I have this numbers in my file : 12 2 -3 -1 when I run the program, the sum of numbers will be 18, but when I open my file again, I see 12 2 -3 -1 . What should I do to see 12 2 3 1 ? here is my code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string name;
cin >> name;
int number; 
int num=0;


ifstream myFile(name, ios::in);
ofstream mine(name, ios::app);
while(myFile >> number)
{
    num += (number<0 ? -number : number);
    mine << num;
}


cout << "num = " << num << endl;


    system("pause");
return 0;
}  

Upvotes: 0

Views: 167

Answers (3)

suspectus
suspectus

Reputation: 17258

  • Opening a read and write file streams for the same file and process at the same time is inviting file corruption. Use ostringstream to store the values read from the file. The values from the file are read, and the values stored in the ostringstream buffer. The ifstream object is closed before re-opening the file with an ofstream object so that the buffer contents can be saved.

  • Note the ios::app flag has been removed. Otherwise the new values will append to the existing values.

  • abs() function is used to write back the absolute values - this forces all values positive.

    #include<sstream>
    int main()
    {
        string name;
        cin >> name;
        int number;
        int num=0;
    
        ifstream myfile(name.c_str(), ios::in);
        ostringstream oss;
    
        while (myfile >> number)
        { 
           num += (number<0 ? -number : number);
           oss << abs(number) << " ";
        }
    
        myfile.close();
        ofstream mine(name.c_str());
        cout << "num = " << num << endl;
        mine << oss.str();
        return 0;
    

    }

Upvotes: 1

petersohn
petersohn

Reputation: 11730

  1. Opening the file for reading and writing for the same time is generally not a bad idea. You probably got an I/O error during opening mine, but since you didn't check it, the program ignored your writes silently. Try reading the file contents first (to a vector for example), then close the file for reading and open again for writing (not appending, because that would leave the old contents in the file).
  2. When writing the values back to the file, also write whitespace after the number, otherwise you'll just get a string of digits in the file but you won't know where one begins and another ends.
  3. Your program now doesn't "change negative numbers to positive" but it prints the cumulative sum of absolute values to the file.
  4. Try writing to the standard output first so you won't ruin your file while you are testing. If it works, then change cout to your output stream.

Here is the code.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

int main()
{
string name;
cin >> name;
int number;
int num=0;


ifstream myFile(name, ios::in);
vector<int> vec;
while(myFile >> number)
{
    vec.push_back(abs(number));
}

ofstream mine(name, ios::out);
for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
    num += *it;
    mine << *it << " ";
}

cout << "num = " << num << endl;


return 0;
}

Upvotes: 1

qwr
qwr

Reputation: 3670

string name;
cin >> name;
int number=0; 

int sum=0;
string outname=name+".pos.txt";

ifstream myFile(name,ifstream::in);
ofstream mine(outname, ofstream::out );
while(myFile >> number)
{
    number= (number<0 ? -number : number);
    sum+=number;

    mine << number<<' ';
}
myFile.close();
mine.close();
cout << "sum = " << sum << endl;


    system("pause"); 

Upvotes: 0

Related Questions