Create a program that asks you what is the position of the value (element) you need, and search for this value according to the chosen position, it also separates the values by comma "," as your need expressed in the question.
Let’s take the example, follow the example below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE* file = fopen("arquivo.txt", "r");
char line[256];
char * valor;
char * valor_escolhido;
int cont_elemento = 1, posicao_elemento;
printf("Informe a possicao ° do valor, (exemplo 1, 2 ou 6): ");
scanf("%d", &posicao_elemento);
if (file == NULL)
return EXIT_FAILURE;
/*Faz a leitura do arquivo linha por linha.*/
while (fgets(line, sizeof(line), file))
{
/*Pega o primeiro elemento separado por uma virgula.*/
valor = strtok(line,",");
/*Obtem os outros elementos até o fim da linha.*/
while (valor != NULL)
{
printf("%s\n",valor);
valor = strtok(NULL, ",");
cont_elemento++;
if (cont_elemento == posicao_elemento)
valor_escolhido = valor;
}
}
printf("\n\nValor escolhido: %s\n\n", valor_escolhido);
fclose(file);
return EXIT_SUCCESS;
}
I used the function strtok
that is part of header string.h
, it can be used when we need to break a string into C using a delimiter, so that it will return its parts according to the value specified in delimiter which in this case is the comma ,
. View the function signature strtok
:
char * strtok ( char * str, const char * delimiters );
Explanation of the program.
I assumed that the structure of your file is in the following format according to the contents of the same that was informed in the question, follows the structure of the file that will be the input data:
0.00053714,0.00053714,-0.00061595,0.30794,-0.00061595,0.30794,1.0001,1,0.0050735
The program will ask the value position (element):
Enter the possible ° of the value, (example 1, 2 or 6):
Value I informed you:
6
I chose the 6° (sixth) element, and it will return me the value 0.30794
.
Exit from the program.
Based on the input data that was reported to the program in the above example, the program will generate the following output:
0.00053714
0.00053714
-0.00061595
0.30794
-0.00061595
0.30794
1.0001 1
0.0050735
Chosen value: 0.30794
Completion.
To get the chosen value you have to count the number of occurrences issued by the function strtok
using the variable cont_elemento
, and do the validation to compare to the chosen position in a if
, see: if (cont_elemento == posicao_elemento)
, then just assign the string with the separate value by the function to the string valor_escolhido
, so I got the chosen value.
Sources:
Split string in C Every white space.
Split string into tokens.
How to use the Strtok() function to break a C string using delimiters.
How do you want the output of the program?
– gato
I would just like to get this sixth value and then print it in a txt file, because I can do it in one, I already have a piece of code ready to read all the files, so I would just add this snippet to it.
– Túlio Alves
This
0.30794
would be the sixth value?– gato
Yeah, that’s the one.
– Túlio Alves
As an example, the contents of your file would be this:
0.00053714,0.00053714,-0.00061595
, the output you would want would be this way:0.00053714
|0.00053714
|-0.00061595
separating values by comma , that?– gato
In fact, I don’t even need the others, only 0.30794, because in the end I will add the sixth value of each file to form a single.
– Túlio Alves
Tulio - I know you’ve learned C - and it’s important to learn better - but these kinds of tasks are best done in other languages - which automate details like opening files, allocating strings, cutting strings, etc. A one-line program in Python that does what you are asking is:
print (open("[nome_do_arquivo.txt]").read().split(",")[5])
(ended that’s the whole program - save to a "program.py" file and run withpython programa.py
. To join the data of the 4000 files, a formatted and readable program will have about 10 lines.– jsbueno
Actually @jsbueno , this file I’m reading is the result of a Pyton script, but since I haven’t been programming for a long time, I searched what I knew was C, since I didn’t have time to learn Pyton. Thanks for the tip, I will look for material to study this language, because it seems much more practical looking for this aspect.
– Túlio Alves
Cool - whatever, my email is in the profile here. Or ask the same question for a Python script that the staff answers. :-)
– jsbueno