LaVOZs

The World’s Largest Online Community for Developers

'; C++ When writing to a file it only writes the last line not the full body of text? - LavOzs.Com

I am having an issue in C++ in which a program that I am writing reads a pre-existing text file called Builders.txt and then writes the same information into a new text file called output.txt, however, when I go to run the program the only line visible in the output.txt file is the last line, which is Tom:90:3.

Text file

Reliable Rover:70:1.
Sloppy Simon:20:4.
Technical Tom:90:3.

The expect output from the output.txt file would be the file shown above

Body Program

int main() {

  readFile();
  writeFile();
  getch();
  return 0;
}

Implementation file

std::ifstream filereadBuilders("Builders.txt");
std::string input;

void readFile() { //fumction that reads each file. 

  std::vector < std::string > readBuilders;

  //loop to read builders.txt
  while (filereadBuilders >> input) {
    readBuilders.push_back(input);
  }

  std::cout << endl;
}

void writeFile() {

  std::ofstream file("output.txt");
  std::vector < std::string > RAT;
  RAT.push_back(input);

  for (std::string outputFile: RAT) {
    file << outputFile << std::endl;
  }

}

I am also having an issue where when it reads/writes, it skips white space. As seen in the expected output file the last line is Technical Tom:90:3., however, my program only returns Tom:90:3., as if its ignoring all white space.

When you use

std::vector<std::string> RAT;
RAT.push_back(input);

you are adding only the last line that was read from the file to RAT. It makes sense that only that line is written to the output file.

Suggestion for future coding projects -- avoid global variables as much as possible.

In your case, make sure that readFile returns the vector of lines to the calling function. Then, pass that data to writeFile. While at it, pass the input file to readFile and pass the output file to writeFile as well.

void readFile(std::string const& inputFile,
              std::vector<std::string>& lines)
{
   std::ifstream file(inputFile);
   std::string line;

   // Use getline. This makes sure that even whitespaces are read in.
   while(std::getline(file, line)) 
   {
      lines.push_back(line);
   }

   std::cout << endl;
}

void writeFile(std::string const& outputFile,
               std::vector<std::string> const& lines)
{
   std::ofstream file(outputFile);
   for(std::string const& line: lines)
   {
      file << line << std::endl;
   }
}

and call them as

std::vector<std::string> lines;
std::string inputFile = "Builders.txt";
std::string outputFile = "output.txt";

readFile(inputFile, lines);
writeFile(outputFile, lines);
Related
reading data from files, file name as input
Identify objects in boost::shared_ptr<boost::thread>
Read file line by line using ifstream in C++
std streams to write to void* and back
Big text file random access in C++
Error reading characters to matrix
How do I make a non concurrent print to file system in C++?
Writing huge 2D vectors to text file too slow, how to improve performance?
i have written a program to manage student data and overloaded operators but i got an error free(): invalid pointer