How do I enter with several different files

Asked

Viewed 34 times

0

Good evening, I have a question, I have a program that will receive several files of the type nome.txt, and I must count how many characters are inside the file, I tried to make a prototype but doubt is on how I will add it using getline and using file pointers?? I mean, how do I get files in txt and then check the characters??

Below is the code:

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    int counter = 0;
    FILE* file = NULL;
    char * arquivo = NULL;
    char frases;
    size_t aux = 0;

    getline(&arquivo,&aux, stdin);

    file = fopen("arquivo", "r" );

    while(fgetc(file) != EOF)
        counter++;

    printf("%d", counter);

    fclose(file);

}
  • I didn’t quite understand the question. What’s going wrong in your program? What was the result you were hoping for?

1 answer

0

file = fopen("arquivo", "r" );

This instruction of your program will open the name file "arquivo"; will not open the file with the name that the user entered and that is contained in the variable arquivo. That’s what you need to do

file = fopen(arquivo, "r");

Moreover, according to the POSIX manual for getline(), the vaiável contains the ENTER user used. You should remove this ENTER before opening the file.

To do it with several different files, you have to cycle. And place within that cycle the file name request, the file opening, the character count, and the file close.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.