I need to know how to ignore the first rows of a txt in c such as:
AQJAS
KJSA
KSJA
1 2 3
123235
In that example I want to read from 1 forward, how can I ignore those characters above thanks.
I need to know how to ignore the first rows of a txt in c such as:
AQJAS
KJSA
KSJA
1 2 3
123235
In that example I want to read from 1 forward, how can I ignore those characters above thanks.
I need to know how to ignore the first rows of a txt in C
If the rows are of variable length (as it seems to be your case) you can not ignore them: you should read them in full.
In C, the files are processed as a data stream: a read pointer is placed in a position and the reading pointer is advanced or retranslated by the contents of the file; It is possible to advance or retract fixed amounts of data (through fseek
) but if the data to be processed is not they have a fixed size the only way to distinguish some data from others is by reading them.
You should read the data file line by line:
char *buffer = NULL;
FILE *archivo = fopen("archivo.txt", "r");
if (archivo)
{
for (size_t length; archivo && (getline(&buffer, &length, archivo) != -1); )
{
// 'buffer' contiene la línea leída.
}
fclose(archivo);
}
When the read line contains the data you are looking for, start processing, ignoring the rest, for example:
void procesa_datos(FILE *entrada, char **buffer)
{
printf("Empezamos a procesar archivo...\n\n"
"Datos a procesar: %s", *buffer);
for (size_t length; entrada && (getline(buffer, &length, entrada) != -1); )
printf("Datos a procesar: %s", *buffer);
}
int main()
{
char *buffer = NULL;
FILE *archivo = fopen("archivo.txt", "r");
if (archivo)
{
for (size_t length; archivo && (getline(&buffer, &length, archivo) != -1); )
{
if (isalpha(*buffer)) // El primer carácter es una letra o un número?
printf("Datos a ignorar: %s", buffer);
else
procesa_datos(archivo, &buffer);
}
fclose(archivo);
}
return 0;
}
I have used as a criterion that the first character of the line is a number, you must use the criterion that is convenient for you.