On 3/12/2026 2:24 AM, Bonita Montero wrote:
There was a programming-"contest" on comp.lang.c and I wanted to show
the simpler code in C, here it is:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <optional>
#include <algorithm>
using namespace std;
static optional<size_t> parse( const char *str );
int main( int argc, char **argv )
{
-a-a-a-a-aif( argc < 3 )
-a-a-a-a-a-a-a-a return EXIT_FAILURE;
-a-a-a-a-aoptional<size_t>
-a-a-a-a-a-a-a-a pRows = parse( argv[1] ),
-a-a-a-a-a-a-a-a pCols = parse( argv[2] );
-a-a-a-a-aif( !pRows || !pCols )
-a-a-a-a-a-a-a-a return EXIT_FAILURE;
-a-a-a-a-asize_t rows = *pRows, cols = *pCols;
-a-a-a-a-aoptional<size_t> pClip( rows * cols );
-a-a-a-a-aif( argc >= 4 && !(pClip = parse( argv[3] )) )
-a-a-a-a-a-a-a-a return EXIT_FAILURE;
-a-a-a-a-asize_t clip = min( *pClip, rows * cols );
-a-a-a-a-astreamsize width = (ostringstream() << clip).str().length();
-a-a-a-a-afor( size_t row = 1; row <= min( rows, clip ); ++row )
-a-a-a-a-a{
-a-a-a-a-a-a-a-a bool head = true;
-a-a-a-a-a-a-a-a for( size_t value = row; value <= clip; value += rows, head =
false )
-a-a-a-a-a-a-a-a-a-a-a-a cout << " "sv.substr( head, !head ) << right <<
setw( width ) << value;
-a-a-a-a-a-a-a-a cout << endl;
-a-a-a-a-a}
}
static optional<size_t> parse( const char *str )
{
-a-a-a-a-aistringstream iss( str );
-a-a-a-a-asize_t ret;
-a-a-a-a-aiss >> ret;
-a-a-a-a-aif( !iss || !iss.eof() )
-a-a-a-a-a-a-a-a return nullopt;
-a-a-a-a-areturn ret;
}
C++ really rocks since you've to deal with much less details than in C.
Is your strategy to just ignore reality, and keep making bogus claims
that - for this challenge at least - you can't support?
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
-a-a-a-aif (argc < 3 || argc > 4) {
-a-a-a-a-a-a-a printf("Enter 2 or 3 arguments:\n$./prog rows columns [stop]\n");
-a-a-a-a-a-a-a return 0;
-a-a-a-a}
-a-a-a-aint rows = atoi(argv[1]);
-a-a-a-aint cols = atoi(argv[2]);
-a-a-a-aint max-a = (argc == 4) ? (atoi(argv[3])) : (rows * cols);
-a-a-a-achar cw[12];
-a-a-a-aint colwidth = sprintf(cw,"%d ",rows * cols);
-a-a-a-afor (int r = 1; r <= rows; r++) {
-a-a-a-a-a-a-a if (r <= max) {
-a-a-a-a-a-a-a-a-a-a-a int nbr = r;
-a-a-a-a-a-a-a-a-a-a-a printf("%*d", colwidth, nbr);
-a-a-a-a-a-a-a-a-a-a-a for (int i = 0; i < (cols - 1); i++) {
-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a ((nbr += rows) <= max) ? (printf("%*d", colwidth,
nbr)) : (0) ;
-a-a-a-a-a-a-a-a-a-a-a }
-a-a-a-a-a-a-a-a-a-a-a printf("\n");
-a-a-a-a-a-a-a }
-a-a-a-a}
-a-a-a-areturn 0;
}
But real C++ code is usually multiple times shorter, mostly because of generic programming. C really sucks since you have to flip every bit
on your own.
On 3/12/26 15:03, Bonita Montero wrote:
But real C++ code is usually multiple times shorter, mostly because of
generic programming. C really sucks since you have to flip every bit
on your own.
-a-a C++ really sucks because you can't know who flip the bits.
On 3/12/2026 10:13 AM, Bonita Montero wrote:
Here, do that in C:
static optional<size_t> parse( const char *str )
{
-a-a-a-a-asize_t ret;
-a-a-a-a-aif( from_chars_result fcr = from_chars( str, str + strlen( str ), >> ret ); (bool)fcr.ec || *fcr.ptr )
-a-a-a-a-a-a-a-a return nullopt;
-a-a-a-a-areturn ret;
}
Explain in detail what it does.
-a-a-a-astatic regex rxNameTel( "^\\s*\"([^\"]*)\"\\s*\"([^\"]*)\"\\s*$" );
Am 12.03.2026 um 15:43 schrieb DFS:
On 3/12/2026 10:13 AM, Bonita Montero wrote:
Here, do that in C:
static optional<size_t> parse( const char *str )
{
-a-a-a-a-asize_t ret;
-a-a-a-a-aif( from_chars_result fcr = from_chars( str, str + strlen( str ),
ret ); (bool)fcr.ec || *fcr.ptr )
-a-a-a-a-a-a-a-a return nullopt;
-a-a-a-a-areturn ret;
}
Explain in detail what it does.
If that isn't self-explanatory stick with C.
I've got a task for you: Do the same in C:
#include <iostream>
#include <regex>
#include <fstream>
#include <vector>
#include <algorithm>
#include <vector>
using namespace std;
int main( int argc, char **argv )
{
if( argc < 2 )
return EXIT_FAILURE;
ifstream ifs( argv[1] );
static regex rxNameTel( "^\\s*\"([^\"]*)\"\\s*\"([^\"]*)\"\\s*$" );
struct name_tel { string name, tel; };
vector<name_tel> phoneList;
while( !ifs.eof() )
{
string line;
getline( ifs, line );
match_results<string::const_iterator> sm;
if( regex_match( line, sm, rxNameTel ) )
phoneList.emplace_back( string( sm[1].first, sm[1].second ), string(
sm[2].first, sm[2].second ) );
}
sort( phoneList.begin(), phoneList.end(),
[]( const name_tel &left, const name_tel &right ) { return left.name <
right.name; } );
for( name_tel &phone : phoneList )
cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\"" << endl;
}
1. Read a file and parse it with ne mentioned regex-pattern.
2. Split both parts of every line in two strings.
3. Sort the "vector" according to the first string.
4. Print it.
You didn't bother to check if the file was opened.
This is certainly a task that can be done simply using a shell
script and the standard POSIX utility set. awk(1) would be
a good starting point; it's possible that it can be done with
a single invocation of the posix 'sort' utility.
Of course, the lack of any in-line documentation (e.g. comments) is
a typical defect in your C++ code.
You didn't bother to check if the file was opened.
Am 12.03.2026 um 18:32 schrieb Scott Lurndal:
You didn't bother to check if the file was opened.
That's not necessary to compare it against a equal solution in C.
This is certainly a task that can be done simply using a shell
script and the standard POSIX utility set.-a awk(1) would be
a good starting point; it's possible that it can be done with
a single invocation of the posix 'sort' utility.
It's comparison of C against C++.
Of course, the lack of any in-line documentation (e.g. comments) is
a typical defect in your C++ code.
Complete idiot.
On 3/12/2026 8:27 PM, Bonita Montero wrote:
I shortened my code a bit.
Do that:
#include <iostream>
#include <regex>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int main( int argc, char **argv )
{
-a-a-a-a-aif( argc < 2 )
-a-a-a-a-a-a-a-a return EXIT_FAILURE;
-a-a-a-a-aifstream ifs( argv[1] );
-a-a-a-a-astatic regex rxNameTel( R"~(^\s*"([^"]*)"\s*"([^"]*)"\s*$)~" );
-a-a-a-a-astruct name_tel { string name, tel; };
-a-a-a-a-avector<name_tel> phoneList;
-a-a-a-a-asmatch sm;
-a-a-a-a-afor( string line; getline( ifs, line ); )
-a-a-a-a-a-a-a-a if( regex_match( line, sm, rxNameTel ) )
-a-a-a-a-a-a-a-a-a-a-a-a phoneList.emplace_back( sm[1].str(), sm[2].str() ); >> -a-a-a-a-asort( phoneList.begin(), phoneList.end(),
-a-a-a-a-a-a-a-a []( name_tel &left, name_tel &right ) { return left.name < >> right.name; } );
-a-a-a-a-afor( name_tel &phone : phoneList )
-a-a-a-a-a-a-a-a cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\""
<< endl;
}
Have a closer look at the regex.
Got a massive set of compile errors:--- Synchronet 3.21d-Linux NewsLink 1.2
= -std=c++20
Your output is messy.-a What you want to do is iterate the data and find
the longest name, then pad spaces after the name so the phone numbers
line up.
88 LOC
16.84MB executable
perfect output
$ ./dfs names-numbers-unsorted-montero.txt
-a1. Anna Becker-a-a-a-a-a-a-a-a-a 0170-2233445
...
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 08:06:32 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
1 files (1,366K bytes) |
| Messages: | 264,936 |