How to read a file in C but not to count with the header in the counter?

Asked

Viewed 31 times

0

FILING CABINET

The first line is the header I want to disregard. So that the counter "num" does not start counting from the HEADER

Company About Funcao Name

Empresaa Bedecs Anna Proprietario

Empresab Gratacos Antonio Proprietario

Empresac Axen Thomas Shopping

Empresad Lee Christina Manager

Empresae Odonnell Martin Proprietario

Empresaf Prezolaeta Francisco Manager

Empresag Xie Ming-Yang Proprietary

Empresah Andersen Elizabeth Representative

Empresai Mortensen Sven Shopping

Empresaj Wacker Roland Shopping

Empresak Krschne Peter Manager

Empresal Edwards John Manager

Empresam Ludick Andre Representative

Empresan Grilo Carlos Representative

Empresao Kupkova Helena Manager

Empresap Goldschmidt Daniel Representative

Empresaq Bagel Jean Proprietario

Loan Autier Catherine Representative

Eggerer Alexander Accounting companies

Empresat Li George Manager

Empresau Tham Bernard Manager

Empresav Ramos Luciana Assistant

Empresaw Entin Michael Manager

Empresax Hasselberg Jonas Proprietario

Empresay Rodman John Manager

Empresaz Liu Run Assistant

Empresaaa Toh Karen Manager

Empresabb Raghav Amritansh Manager

Empresacc Lee Soo Manager

SOURCE CODE BELOW

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

main(){
FILE * input_text, *output_text;
char line[1024];
char * last;
int num = 0;
setlocale(LC_CTYPE,"");
    if((input_text = fopen("entrada03.txt","r")) == NULL)
        printf("\n[ERRO]Travou o Nintendo!\n");
    else
        printf("\n[SUCESSO]Sucesso ao abrir o arquivo!\n\n");
    printf("\n\nImprimindo a saida\n\n");
    sleep(2);
    output_text = fopen("saida02.txt","w");

            while(!feof(input_text)){
                fgets(line, 1024, input_text);
                    last = strtok(line, "\n");                                  //"last" recebe variável "line" até o delimitador "\n"
                        while(last != NULL){                                    //Se last não for "NULL" executa essa
                            printf("%d: %s\n",num, last);
                                last = strtok(NULL, "\n");                      //Se last for "NULL" até o delimitador "\n"
                                fprintf(output_text,"%d: %s\n", num, line);
                        }
                    num++;
            }
fclose(input_text);
fclose(output_text);
printf("\n");
system("pause");
}

1 answer

0


see if that’s what you’d like done:

What I changed was to include if(num != 0) to present on the screen and then write to your file, so it reads/writes anything other than line 0;

Another point, be careful because you are using C++ applications in your code as long as it works, but it is not very elegant...

I hope it helped. Hugs!!

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

int main(){
FILE * input_text, *output_text;
char line[1024];
char * last;
int num = 0;
setlocale(LC_CTYPE,"");
    if((input_text = fopen("entrada03.txt","r")) == NULL)
        printf("\n[ERRO]Travou o Nintendo!\n");
    else
        printf("\n[SUCESSO]Sucesso ao abrir o arquivo!\n\n");
    printf("\n\nImprimindo a saida\n\n");
    sleep(2);
    output_text = fopen("saida02.txt","w");

        while(!feof(input_text)){
            fgets(line, 1024, input_text);
                last = strtok(line, "\n");                                  //"last" recebe variável "line" até o delimitador "\n"
                    while(last != NULL){                                    //Se last não for "NULL" executa essa
                        if(num != 0)
                        printf("%d: %s\n",num, last);
                            last = strtok(NULL, "\n");                      //Se last for "NULL" até o delimitador "\n"
                            if(num != 0)                      
                            fprintf(output_text,"%d: %s\n", num, line);
                    }
                num++;
        }
fclose(input_text);
fclose(output_text);
printf("\n");
system("pause");
}
  • 1

    good ok, that was it right there. Thank you, a simple detail changes everything.

  • Great that it served. Hugs!

Browser other questions tagged

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