0

The task is to copy the contents of the first array and second array into the third array and display it. I'm getting garbage values when I try this. What am I doing wrong? Thanks.

  • input1.txt= How are you doing
  • input2.txt= today sir
#include <iostream>
#include <fstream>

using namespace std; 

void display(char* ptr, int size)
{ 
    for (int i = 0; i < size; i++)
    { 
        cout << ptr[i]<< " "; 
    } 
    cout << endl; 
}

int main() 
{ 
    ifstream fin1; 
    fin1.open("input1.txt"); 
    int size1 = 0; 
    while (!fin1.eof()) 
    {
        fin1.get(); size1++; 
    } 
    char* arr1 = new char[size1]; 
    fin1.close(); 
    fin1.open("input1.txt"); 
    fin1.getline(arr1, size1); 
    ifstream fin2; 
    fin2.open("input2.txt"); 
    int size2 = 0; 
    
    while (!fin2.eof())
    { 
        fin2.get(); size2++; 
    } 
    char* arr2 = new char[size2]; 
    fin2.close(); 
    fin2.open("input2.txt"); 
    fin2.getline(arr2, size2); 
    int size3 = size1 + size2; 
    char* arr3 = new char[size3 + 2]; 
    return 0; 
}
dipdopkop
  • 1
  • 4
  • 2
    "to display the contents of the first array and second array into the third array" -> I don't understand this. Instead of 'display', should it say copy or concatenate or append, etc.? Currently, `arr3` is unused and nothing is printed anywhere. Where are the "garbage values"? Could you provide examples of current output and expected output? Additionally, there are memory leaks. – Yun Sep 09 '21 at 17:21
  • @Yun Thanks for the response. I have to print it to arr3 but when I try to print it, it returns with "= = = = = = = = = =" The contents can be collection of words in both the files, input1 and input2, the output should be the combination of both. Please guide me on this and point the memory leaks, thanks. – dipdopkop Sep 09 '21 at 17:30
  • You're welcome. Why is this not the output you expect? What is in the input files? Clarifications are best edited into your question, rather than given as comments. – Yun Sep 09 '21 at 17:32
  • Good tip, noted and added. – dipdopkop Sep 09 '21 at 17:36
  • Tactical note: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Sep 09 '21 at 17:43
  • You need to do something after allocating the space for the new array at `char* arr3 = new char[size3 + 2];`. If you have done something, then you haven't shown us what; neither does the code you present ever call the `display` function. – Adrian Mole Sep 09 '21 at 17:45
  • @AdrianMole I have omitted the output since it didn't even function closely to what the output should be so I have asked help here on how to output it, the output should be combo of the input1 and input2 text files. – dipdopkop Sep 09 '21 at 17:54
  • We can't tell you what you are doing wrong and how to fix it unless you show us what you are doing wrong. I suppose any of us could show you what we would do to get the result you desire, but what would you learn from that? Plus the program would be totally different, only a few lines long, and probably use libraries you're not allowed to use for this assignment. – user4581301 Sep 09 '21 at 17:54
  • @user4581301 I get the point but it's a condition imposed on us by our teacher. – dipdopkop Sep 09 '21 at 17:55
  • My point still stands: We can't help you fix what you wrote if you don't show what you wrote. You might have only a tiny mistake that is easily corrected, but even if you have a huge mistake, we can show you where you went off course and how to avoid it in the future. If someone answers with "Just do this: " you learn nothing and will keep making the same mistakes. Your time is wasted. – user4581301 Sep 09 '21 at 18:04

1 Answers1

0

Depending on how the assignment has been worded, this may be legally correct. It only uses the two headers you're currently using. No std::strings or any of the "fancy stuff" normally disallowed by instructors.

#include <iostream>
#include <fstream>

int main()
{
    // open the files
    std::ifstream fin1("input1.txt");
    std::ifstream fin2("input2.txt");

    if (fin1 && fin2)
    { // if both files are in a good state, they're both open, write to output
        std::cout << fin1.rdbuf()   // dump the contents of file 1 to output
                  << ' '            // add a space
                  << fin2.rdbuf();  // dump the contents of file 2 to output
        return 0; // program successful
    }
    else
    { // at least one file failed to open
        std::cerr << "File input failure"; // notify user of failure
        return 1; // notify system of failure
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54