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

0

FILE

THE FIRST LINE IS THE HEADING THAT I WANT TO DISCONTINUE. So that the counter "num" does not begin counting from the HEADER

Company About Name Function

CompanyA Bedecs Anna Owner

CompanyB Gratacos Antonio Owner

CompanyC Axen Thomas Shopping

CompanyD Lee Christina Manager

CompanyE ODonnell Martin Owner

CompanyF PrezOlaeta Francisco Manager

CompanyG Xie Ming-Yang Owner

Company Andersen Elizabeth Representative

CompanyI Mortensen Sven Shopping

Company J Wacker Roland Shopping

CompanyK Krschne Peter Manager

CompanyL Edwards John Manager

CompanyM Ludick Andre Representative

CompanyN Grilo Carlos Representative

CompanyO Kupkova Helena Manager

CompanyP Goldschmidt Daniel Representative

CompanyQ Bagel Jean Proprietary

CompanyR Autier Catherine Representative

Company Eggerer Alexander Accounting

CompanyT Li George Manager

Company U Tham Bernard Manager

Company Ramos Luciana Assistant

CompanyW Entin Michael Manager

CompanyX Hasselberg Jonas Owner

Company And Rodman John Manager

CompanyZ Liu Run Wizard

CompanyAA Toh Karen Manager

CompanyBB Raghav Amritansh Manager

Company CC 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");
}
    
asked by anonymous 09.09.2018 / 21:11

1 answer

0

see if this is what you would like it to be done:

What I changed was to include if (num! = 0) to display on the screen and then save 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 you have 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");
}
    
09.09.2018 / 21:37