After being told using scanf and printf instead of cout and cin
because they are treated like a function instead like cout and cin being treated as a object thus equaling faster code generation so I am relearning as a i go


Can you not use a string in printf or scanf instead use chars?


also If i have this code

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

const int ROWS = 5, COLS = 5; 


int main()
{
    int matrix[ROWS][COLS], i, j;
    char str[100];
    ifstream inFile;
    
    
    printf("Enter the name of the input file:");
    scanf("%s", &str);
    
    return 0;
}

How do i open the file?

> After being told using scanf and printf instead of cout and cin
Why?
or better yet,
Who said it?

> because they are treated like a function instead like cout and cin being treated as a object thus equaling faster code generation
Do you have references, or evidence of this?

Do you want to be a C programmer or a C++ programmer?

>>From a very good programmer who is both fluent in c and c++
It is a sad part that beginners of the language are actually incapable of telling if a programmer is 'good' or 'bad'. A C guru is not necessarily a C++ guru and vice-versa. Perhaps, a major fraction of C++-is-just-another-C gang are the C programmers only. This concept is straight forward incorrect.
Don't rely on some programmer. Trust a written evident. Always ask a programmer to give you reference about what he is saying. ( Good programmers always give references to their knowledge)

How do i open the file?

inFile.open(str); Note that scanf() will not allow spaces in the text, so if the filename and/or path contains spaces then you need to use getline() instead of scanf().

Comments
I didn't think of that, but it's absolutely right !

>because they are treated like a function instead like cout and cin
>being treated as a object thus equaling faster code generation
Code generation differs between compilers. scanf and printf may be faster on the "very good programmer who is both fluent in c and c++"'s compiler, but the opposite could be true on your compiler. It sounds like your "very good programmer who is both fluent in c and c++" isn't a very good programmer after all.

It's generally best practice to use stdio in C and iostreams in C++ unless you're so intimately familiar with both that you're comfortable mixing them.

>Can you not use a string in printf or scanf instead use chars?
No, printf and scanf are limited to the types inherited from C, and std::string is a C++ creation.

As if to prove a point, the very first system I tried it on, the results conflict with the assertion.

Some test code, to format and print 1E7 integers to a text file, then read them back in again.

Test C program

#include <stdio.h>
#define MAX_R   10000
#define MAX_C   1000
int main ( ) {
#if DO_WRITE
    FILE *fp = fopen("test_c.txt","w");
    int r, c, prod;
    long long int sum = 0;
    for ( r = 0 ; r < MAX_R ; r++ ) {
        for ( c = 0 ; c < MAX_C; c++ ) {
            prod = r * c;
            fprintf( fp, "%d ", prod );
            sum += 1;
        }
        fprintf( fp, "\n");
    }
    fclose(fp);
    printf("Last prod value =%d\n", prod );
    printf("Number of values=%lld\n", sum );
#else
    FILE *fp = fopen("test_c.txt","r");
    int prod;
    long long int sum = 0;
    while ( fscanf( fp, "%d", &prod ) == 1 ) {
        sum += 1;
    }
    fclose(fp);
    printf("Last prod value =%d\n", prod );
    printf("Number of values=%lld\n", sum );
#endif
    return 0;
}

Test C++ program

#include <iostream>
#include <fstream>
using namespace std;
const int MAX_R = 10000;
const int MAX_C = 1000;
int main ( ) {
    std::ios_base::sync_with_stdio(false);
#if DO_WRITE
    ofstream op("test_cpp.txt");
    int r, c, prod;
    long long int sum = 0;
    for ( r = 0 ; r < MAX_R ; r++ ) {
        for ( c = 0 ; c < MAX_C; c++ ) {
            prod = r * c;
            op << prod << " ";
            sum += 1;
        }
        op << '\n';
    }
    op.close();
    cout << "Last prod value =" << prod << '\n';
    cout << "Number of values=" << sum << '\n';
#else
    ifstream ip("test_cpp.txt");
    int prod;
    long long int sum = 0;
    while ( ip >> prod ) {
        sum += 1;
    }
    ip.close();
    cout << "Last prod value =" << prod << '\n';
    cout << "Number of values=" << sum << '\n';
#endif
    return 0;
}

Script and results

G_FLAGS=-O2

gcc -o rw_r_c $G_FLAGS rw.c
gcc -o rw_w_c $G_FLAGS -DDO_WRITE rw.c
echo C write + read times
time ./rw_w_c 
time ./rw_r_c

g++ -o rw_r_cpp $G_FLAGS rw.cpp
g++ -o rw_w_cpp $G_FLAGS -DDO_WRITE rw.cpp
echo C++ write + read times
time ./rw_w_cpp 
time ./rw_r_cpp


$ . fooTest 
C write + read times
Last prod value =9989001
Number of values=10000000

real	0m3.003s
user	0m2.020s
sys	0m0.492s
Last prod value =9989001
Number of values=10000000

real	0m3.850s
user	0m2.984s
sys	0m0.160s
C++ write + read times
Last prod value =9989001
Number of values=10000000

real	0m4.127s
user	0m2.840s
sys	0m0.656s
Last prod value =9989001
Number of values=10000000

real	0m3.279s
user	0m2.536s
sys	0m0.116s
$

And this is just one combination of possible version of compiler / OS / compiler flags.
Also, since we're dealing with file I/O, there is just as much variability between runs as there is variation between approaches (at least for me in this test).

You can't really screw up C++ iostreams, but it is all too easy to screw up the "meta-language" of printf/scanf format strings. In fact, you only have one use of scanf, and it's wrong.
A whole program full of printf/scanf could be bugging you for days.

Comments
Agreed. Specially the last remark about debugging.
This article has been dead for over six months. Start a new discussion instead.