Hello, friends!
I am creating a program that reads data separated by semicolons, checks them and returns a list with possible errors. (example: the user enters CNPJ, social name, UF as follows: 12345678901234; company x; SP). I'm doing this because the next step is to get a ready .txt file with thousands of companies in this format and use it in the program.
The problem is that since data such as CNPJ and UF have specific numbers of characters to insert (14 and 2), I must put in the scanf for the strings to receive specific values of cnpj and UF. This I can do without problems, including the part of reading the data to the semicolon. What happens is that if the user types extra characters in CNPJ (15 characters, for example), the keyboard buffer carries the extra character for the next data, compromising all the following information. How do I prevent this from happening? Here is the passage I am referring to:scanf( "%14[^;];%40[^;];%8[^;];%2[^;\r\n]%*[;\r\n]",
cnpj,
razao_social,
data_de_fundacao,
uf );
validaCNPJ(cnpj);
if (validaCNPJ(cnpj) == 0){
cnpjErr = cnpjErr + 1;
}
contador_linhas++;
printf( "\n[%4d][%-14s][%-40s][%-8s][%-2s]",
contador_linhas,
cnpj,
razao_social,
data_de_fundacao,
uf );