Home > Enterprise >  C Read and append
C Read and append

Time:04-16

Hello everyone I need help with one of the excercise I am doing. I have searched alot but didn't found what I was looking. So, I want my program to read from one .txt file and then gives output in another .txt file.

Source File have this contents.
Name Salary 
Aamir 12000 
Amara 15000
Adnan 13000
Afzal 11500

Output File should have this 
Name Salary 
Aamir 14000
Amara 17000
Adnan 15000
Afzal 13500

The program should read the source file and add 2000 to the salary in the output file.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char c;
    char inputFile[] = "my-file.txt";
    char outputFile[] = "my-file-out.txt";

    ifstream inFile;
    ofstream outFile;

    inFile.open(inputFile, ios::in);

    if (!inFile)
    {
        cout << "The file cannnot be open please check" << endl;
        exit(1);
    }

    outFile.open(outputFile, ios::out | ios::ate);

    while ((c = inFile.get()) != EOF)
    {

        outFile.put(c);
    }

    inFile.close();
    outFile.close();
}

I was able to copy the content from source file to output file after some struggle but now no matter what I do it won't give me the desirable solution. What should I add into the code that would give the correct output.

CodePudding user response:

  1. prefer using const std::string over const char*
  2. extract the first line and write to output stream. std::getline can help you.
  3. read data from source with operator>> if your source data constists of fixed types of datas.
  4. raise salary and write to dest file with operator<<

Following code works but I omitted handling exceptions or erros for simplicity. You should add handling code when opening, reading, writing data and closing streams.

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

using std::string;

int main() {
  const string name_input_file = "D:/my-file.txt";
  std::ifstream ifs(name_input_file);

  const string name_output_file = "D:/my-file-out.txt";
  std::ofstream ofs(name_output_file);

  string fist_line;
  std::getline(ifs, fist_line);
  ofs << fist_line << '\n';

  string name;
  int salary;
  while (ifs && ofs) {
    ifs >> name >> salary;
    salary  = 2'000;
    ofs << name << ' ' << salary << '\n';
  }

  return 0;
}

CodePudding user response:

Here's a dirty solution. Reads my-file.text, adds 2000 to each salaries and writes the new strings to my-file-out.txt. Note that my-file-out.txt will lose its previous data everytime you run the program.

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>

int main()
{
  std::ifstream ifile;
  std::ofstream ofile;
  ifile.open("my-file.txt", std::ifstream::in);
  ofile.open("my-file-out.txt", std::ofstream::out | std::ofstream::trunc);
  std::string line{};
  bool flag{true};

  while (std::getline(ifile, line))
  {
    if (flag){ flag = false; ofile << line << "\n"; continue; }
    std::string salary{};
    for (size_t i{line.length() - 1}; i >= 0; --i)
    {
      if (line[i] == ' ') break;
      salary  = line[i];
    }
    std::reverse(salary.begin(), salary.end());
    line.replace(line.length() - salary.length(), line.length(), std::to_string(std::stoi(salary)   2000));
    ofile << line << "\n";
  }

  ifile.close();
  ofile.close();
}
  • Related