Search code examples
c++symbolsinto-outfile

printing weird symbols to output file, c++


I am writing a code that reads an input file of numbers, sorts them in ascending order, and prints them to output. The only thing printed to output is some really freaky symbols.

Here is my code

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

int main()
{
    int i, y, temp, num[20];
    char file_nameI[21], file_nameO[21];
    ofstream outfile;
    ifstream infile;

    cout << "Please enter name of input file:  ";
    cin >> file_nameI;
    infile.open(file_nameI);
    if (!infile)
    {
        cout << "Could not open input file \n";
        return 0;
    }

    cout << "Please enter name of output file:  ";
    cin >> file_nameO;
    outfile.open(file_nameO);
    if (!outfile)
    {
        cout << "Could not open output file \n";
        return 0;
    }

    for (i = 0; i < 20; i++)
    {
        y = i + 1;
        while (y < 5)
        {
            if (num[i] > num[y])      //Correction3
            {
                infile >> temp;
                temp = num[i];
                num[i] = num[y];
                num[y] = temp;
                //y++;          //Correction4
            }
            y++;
        }
    }

    for (i = 0; i < 5; i++)
        outfile << "num[i]:" << num[i] << "\n";

    return 0;
}

Here is my input

6 7 9 0 40 

Here is the output

 „Ô,üþ 54
H|À°ÀzY „Ô,üþ 0

Solution

  • Problems with your code are already mentioned in the comments but again:

    1. First problem is uninitialized elements of num[20] - elements of num have indeterminate values so accessing any of them triggers undefined behavior. You should first read them from the file or at least initialize them to some default value.
    2. The part of code that should most likely do the sorting is just completely wrong. If you'd like to implement your own function for sorting, you can pick up some well-known algorithm like e.g. quicksort - but C++ Standard Library already provides sorting function - std::sort.

    Besides obvious mistakes:

    1. You are using char[] - in C++ it's almost always better to use std::string.
    2. Your static array can only store 20 values and you are reading those from a file. You can use std::vector which can grow when you add more elements than its current capacity. It also automatically fixes the problem with uninitialized elements of num[20].
    3. As mentioned in the comments you can organize your code and improve readability by splitting it into functions.

    Here you've got it quickly rewritten. This code uses std::string instead of char[], std::vector to store the numbers and std::sort. If there is something you don't understand here, read SO documentation:

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    vector<int> read_file(ifstream& in_file)
    {
        vector<int> vec;
        int value;
        while (in_file >> value)
        {
            vec.push_back(value);
        }
    
        return vec;
    }
    
    void write_file(ofstream& out_file, const vector<int>& values)
    {
        for (size_t i = 0; i < values.size(); ++i)
            out_file << "value #" << i << ": " << values[i] << '\n';
    }
    
    int main()
    {
        string input_filename, output_filename;
        ofstream out_file;
        ifstream in_file;
    
        cout << "Please enter name of input file: ";
        cin >> input_filename;
        in_file.open(input_filename);
        if (!in_file)
        {
            cout << "Could not open input file\n";
            return 0;
        }
    
        cout << "Please enter name of output file: ";
        cin >> output_filename;
        out_file.open(output_filename);
        if (!out_file)
        {
            cout << "Could not open output file\n";
            return 0;
        }
    
        auto numbers = read_file(in_file);
    
        sort(begin(numbers), end(numbers));
    
        write_file(out_file, numbers);
    
        return 0;
    }