-2

Hi I'm trying to read numbers from a file and print them exactly how they are written in the

file.

This is how they are written in the file and I want to print them exactly like that.

833 833 835 840 847 850 858 861 866 874 
881 883 892 898 906 915 921 927 936 936 
944 951 953 960 967 975 979 980 989 989 
991 996 997 1001 1001 1001 1001 1002 1006 1011 
1012 1015 1022 1024 1024 1029 1031 1037 1038 1041 

this is my code but it's not doing what I want

void sort(string path){
    fstream fs; 
    fs.open(path); 
    int number; 
    while(fs >> number){
        cout << number << endl;
        }
    }
}

this is the output:

33530
33533
33542
33550
33553
33554
33556
33561
33569

as you can see they are not on the same line.

I even tried this:

void sort(string path){
    fstream fs; 
    fs.open(path); 
    string number; 
    while(getline(fs, number)){
        cout << number << endl;
        }
    }
}

but then the numbers are not integers they are strings

can anyone please help?

Marek R
  • 32,568
  • 6
  • 55
  • 140
brah79
  • 37
  • 5
  • Possible duplicate: https://stackoverflow.com/a/7868998/1387438 – Marek R Nov 15 '22 at 14:55
  • In your line `cout << number << endl;`, the `endl` is a *newline* (and a *flush*). – Eljay Nov 15 '22 at 14:57
  • I tries without the endl but then I get all the numbers at the same line – brah79 Nov 15 '22 at 14:58
  • 1
    The first one you write endl (which means end line) after each number, so that's what you got. On the second one, If you want them printed "just like that" then what's wrong with strings? They are strings in the original file. – Kenny Ostrom Nov 15 '22 at 14:58
  • 1
    `fs.open(path)` Are you sure it doesn't open another file? – 273K Nov 15 '22 at 14:58
  • Sounds like you might want to add a new line after every ten numbers, so may need to keep a count – doctorlove Nov 15 '22 at 15:02
  • the problem with strings is that I can't do any any operation on them. as you can see the method is called sort() so I'm trying to sort then and I can't do that if they are strings. @273K (path) is just an example, the actual file has another name. – brah79 Nov 15 '22 at 15:03
  • @doctorlove that's just part of the file but the file is much bigger than that then the number of elements in every line is changing every time – brah79 Nov 15 '22 at 15:04
  • `sort` is a strange name for something that does not sort – 463035818_is_not_an_ai Nov 15 '22 at 15:30
  • @463035818_is_not_a_number I'm about to apply the sorting algorithm – brah79 Nov 15 '22 at 15:36

1 Answers1

3

Read each line, one by one, and for each line read it into your int number; and output the result.

#include <iostream>
#include <sstream>
#include <string>
#include <utility>

using std::cout;
using std::exchange;
using std::getline;
using std::istream;
using std::istringstream;
using std::string;

static void sort(istream& in){
    string line;

    while (getline(in, line)) {
        istringstream ss(line);
        int number;
        auto sep = "";

        while(ss >> number){
            cout << exchange(sep, " ") << number;
        }

        cout << "\n";
    }
}

static char const* input_data =
R"aw(833 833 835 840 847 850 858 861 866 874
881 883 892 898 906 915 921 927 936 936
944 951 953 960 967 975 979 980 989 989
991 996 997 1001 1001 1001 1001 1002 1006 1011
1012 1015 1022 1024 1024 1029 1031 1037 1038 1041
)aw";

int main() {
    istringstream ss(input_data);
    sort(ss);
}
Eljay
  • 4,648
  • 3
  • 16
  • 27
  • complicated and I got a bunch of errors this is one of them. no member named 'exchange' in namespace 'std' – brah79 Nov 15 '22 at 15:13
  • It works here: [https://ideone.com/QJGNe3](https://ideone.com/QJGNe3) – drescherjm Nov 15 '22 at 15:29
  • [std::exchange](https://en.cppreference.com/w/cpp/utility/exchange) requires that you have a compiler that supports the 2014 standard and that is enabled. This should be easy in 2022 eight years later! – drescherjm Nov 15 '22 at 15:31
  • is there another simpler approach – brah79 Nov 15 '22 at 15:34
  • [https://www.fluentcpp.com/2020/09/18/what-stdexchange-does-and-how-to-remember-it/](https://www.fluentcpp.com/2020/09/18/what-stdexchange-does-and-how-to-remember-it/) – drescherjm Nov 15 '22 at 15:38
  • @brah79 • One way to simplify code is to extract out what could be (or ought to be...?) a *subroutine* such that the function is refactored into separate routines and subroutines that each do **one thing, and only one thing** (and avoid rollercoaster rides across different levels of abstraction). What Robert Martin called "extract 'til you drop". The body of the outer while loop could be hoisted out to a separate function. I wrote this code using C++17, but it should compile as C++14. What C++ standard are you using? – Eljay Nov 15 '22 at 16:14
  • @Eljay c++ 14 bro – brah79 Nov 15 '22 at 16:43
  • @brah79 • C++14 should support `std::exchange` from `` header. You can verify that with `__cpp_lib_exchange_function` from the [feature testing macros](https://en.cppreference.com/w/User:D41D8CD98F/feature_testing_macros). – Eljay Nov 15 '22 at 17:08