Stefan
Stefan

Reputation: 113

Main function with arguments

I'm trying to understand the main function with the arguments argc and argv. In the command line I am trying to copy the contents of multiple txt files on the screen (concatenation). When I write in the command line appname.exe something f1.txt, the content from the f1.txt prints in a loop. If f1.txt had the text "abcda" the output in console would be "abcdaabcdaabcda...". Sorry for my english; can someone help me understand what I did wrong?

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int main(int argc, char *argv[])
{   
    int i;
    for (i = 2; i <= argc - 1;i+2)
    {
        FILE *f = fopen(argv[i], "r");
        if (f == 0)
        {
            printf("Error\n");
        }
        else
        {
            int x;
            while ((x = fgetc(f)) != EOF)
            {
                printf("%c", x);
            }
        }
        fclose(f);
    }
}

Upvotes: 0

Views: 44

Answers (1)

Gillespie
Gillespie

Reputation: 6561

Here's one big problem:

for (i = 2; i <= argc - 1;i+2)

I think you mean to do:

for (i = 2; i <= argc - 1; i++)

Upvotes: 1

Related Questions