1.

Solve : Want help with simple I/O in C or C++?

Answer»

I have a hard time with tutorials. Can somebody out there give me a code snippet that can do the following:
Fiat the is e already  simple text files created by notepad.
start program
get one or more parameters from the COMMAND line.
Open the file from the name of one of the parameters.
until end  of file
   read on line of the file (and automatically ADVANCE to next line.)
   send to the  to the standard output
loop
close the file
end of program.
Also, how do I compile it to an executable.

Yes, I did this stuff yeas ago, but my memory is fading because of lack of refresh.
Thanks in advance.
Here:

Code: [Select]#include <stdio.h>

int main (int argc, char *ARGV[])
{
    FILE *file = fopen ( argv[1], "r" );
    if ( file != NULL )
    {
        char line [ 128 ]; /* or other suitable maximum line size */

        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {
            FPUTS ( line, stdout ); /* write the line */
        }
        fclose ( file );
    }
    else
    {
        printf("Error: Can't open file!"); /* why didn't the file open? */
    }
    return 0;
}

Quote

Also, how do I compile it to an executable.

I RECOMMEND code blocks. It's free, just google.


Discussion

No Comment Found