0

I was working on a small project to practice i/o files in c++ but i can't figure out this problem. I wanted to write and algorithm that rearranges words in alphabetical order in a text file.(preferable bubble sorting). This is what i have so far

ifstream file("lab01.txt");
ofstream fileOut("lab01_out.txt");
char s[20][10];//variable for copying the words

//check if file was oppened
if (!file.is_open()) {
    cout << "Error, file was not oppened!" << endl;
    return -1;
}

//copy words from file to 2d array
for (int i = 0; i < 20; i++) 
    file >> s[i];


char check[1];

//bubble sort
for (int i = 0; i < 19; i++) {
    for (int j = 0; j < 18 - i; j++) {
        if (strcmp(s[j], s[j + 1]) > 0) {
            strncpy_s(check, s[j], _TRUNCATE);//if not truncated error "buffer to small"
            strncpy_s(s[j], s[j + 1], _TRUNCATE);
            strncpy_s(s[j + 1], check, _TRUNCATE);
        }
    }
}

//printing array to output file and to console.
for (int i = 0; i < 20; i++) {
    cout << s[i] << endl;
    fileOut << s[i] << endl;
}

//closing files.
file.close();
fileOut.close();

The problem is that this is what my output file looks like. I'm getting these symbols instead of words... enter image description here

Any help will be appreciated!

sziko
  • 53
  • 2
  • 11
  • 1
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Mar 06 '17 at 20:04
  • ***I was working on a small project to practice i/o files in c++*** If you are really doing that my advice is to quit using char arrays instead of std::string. – drescherjm Mar 06 '17 at 20:08
  • For starters, I would change `char check[1];` to `char check[10];`. – R Sahu Mar 06 '17 at 20:11
  • @RSahu Well, that worked. Thanks a lot! – sziko Mar 06 '17 at 20:16
  • @sziko, ok. Voting to close for typo. – R Sahu Mar 06 '17 at 20:17
  • Search stackoverflow for "c++ read file array". There are many similar posts already. – Thomas Matthews Mar 06 '17 at 20:43

1 Answers1

1

Some tips how to program in Modern C++.

  • Don't bring the whole std namespace into your code - Why is “using namespace std” considered bad practice?
  • Instead of legacy array use a std::vector;
  • Don't comment the obvious eg. !file.is_open() this could lead to stale comments after code is modified, and comments are not modified. Make the code obvious.
  • Don't need to close the file at end of block (destructor does it for You)
  • Use standard available algorithms (eg. std::swap)
  • Use meaningful variable names

-

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm> // until c++11
#include <utility> // since c++11

using std::cout;
using std::endl;
using std::string;

int main()
{
    std::ifstream fileToRead( "lab01.txt" );
    std::ofstream fileOut( "lab01_out.txt" );

    if ( !fileToRead.is_open() || !fileOut.is_open() )
    {
        cout << "Error, file was not oppened!" << endl;
        return -1;
    }

    std::vector< string > strings;
    string readString;

    while ( fileToRead >> readString )
    {
        strings.push_back( readString );
    }

    const auto stringsCount = strings.size();
    // bubble sort
    for ( auto lastPosition = stringsCount - 1; lastPosition > 0; lastPosition-- )
    {
        for ( std::size_t checkedPosition = 0; checkedPosition < lastPosition; checkedPosition++ )
        {
            if ( strings[ checkedPosition ] > strings[ checkedPosition + 1 ] )
            {
                std::swap( strings[ checkedPosition ], strings[ checkedPosition + 1 ] );
            }
        }
    }

    for ( string str : strings )
    {
        cout << str << endl;
        fileOut << str << endl;
    }
}
Community
  • 1
  • 1
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31