Read only specific lines of . txt files in C

Asked

Viewed 2,030 times

-1

Hello, I want to read a file .txt and get a specific name on it.

I have the following code that does this, but checks on all lines of it.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	FILE *arq;
	char busca[80], nome[80], buscaIdade[80], buscaEnd[100];
	int tamanho, idade, endereco;

//	Abre o arquivo
	arq = fopen("ex-1.txt", "r");

	if(arq == NULL) {
		printf("Erro na criacao do arquivo");
		exit(1);
	}

	printf("Digite um nome a ser buscado no arquivo: ");
	scanf("%[^\n]s", nome);

	strcat(nome, "\n");

	tamanho = strlen(nome);

    do {
        fgets(busca, tamanho+1, arq);

        if(strcmp(busca, nome) == 0) {
            fgets(buscaIdade, tamanho, arq);
            fgets(buscaEnd, tamanho, arq);

            printf("Nome: %s", busca);
            printf("Idade: %s", buscaIdade);
            printf("Endereco: %s\n", buscaEnd);
            exit(1);
        }
    } while(!feof(arq));

	printf("Pessoa nao cadastrada!");

//	Fecha o arquivo
	fclose(arq);
}

My file of .txt is structured line by line, i.e.:

line1 - Name;

Linha2 - Age;

line3 - Address.

And so on and so forth

I wonder how you could be doing to read only these lines, multiples of 3 (counting the line 1 as 0) for when you search for a name, search only in the lines referring to them.

Gratefully. :D


EDIT 1:

I thought of a way to do this, read only specific lines. And what I did was count the lines of the file and when the nLinha is equivalent to 0 or a multiple of 3, I would read and compare your information:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	FILE *arq;
	char busca[80], nome[80], buscaIdade[80], buscaEnd[100], c, letra = '\n';;
	int tamanho, idade, endereco, nLinhas = 0;

//	Abre o arquivo
	arq = fopen("ex-1.txt", "r");

	if(arq == NULL) {
		printf("Erro na criacao do arquivo");
		exit(1);
	}

	printf("Digite um nome a ser buscado no arquivo: ");
	scanf("%[^\n]s", nome);

	strcat(nome, "\n");

	tamanho = strlen(nome);
    
    while(fread (&c, sizeof(char), 1, arq)) {
        if(c == letra) {
            nLinhas++;
        }
        
        if((nLinhas == 0) || (nLinhas % 3 == 0)) {
        	fgets(busca, tamanho+1, arq);
            
            if(strcmp(busca, nome) == 0) {
	
	            printf("Nome: %s", busca);
	            printf("Idade: %s", busca);
	            printf("Endereco: %s\n", busca);
	            exit(1);
	        }
		}
    }

	printf("Pessoa nao cadastrada!");
	
//	Fecha o arquivo
	fclose(arq);
	
	return 0;
}

The problem that arises now is that to compare the information, the strcmp() will never return true because the fgets() is ignoring the first letter of the texts.

Ex:

ex-1.txt

qwe
1
asd
zxc
2
vbn
rty
1
fgh

And if I print what you have in search, return to me:

we

sd
xc

bn
ty

gh
Pessoa não cadastrada!

  • As it is a text file and therefore sequential you have no alternative but to go reading line by line until you find what you are looking for. What I didn’t understand was why you keep modifying the maximum amount of characters to be read in the fgets function at each reading, it seems to me that you misinterpreted the meaning of the function parameters.

  • Sorry, I was trying to read in one line and I forgot to change it. My problem is that if I type for example 1 (an existing age), it will return it and the result will get confused :v

  • But in question to read line by line, would not have some way to check only on specific lines? Says I change in question.

  • If by "check only on specific lines" you mean reading a particular line without reading the previous lines then in a text file, that is: sequential, it does not. If it were a binary file you could use the fseek function to position where the next reading will be made.

  • 1

    only a specific line could be read if all the file lines have the same size, otherwise you will have to do sequential reading

  • zentrunix I had an idea of how to do and added to Edit. What tells me about? What might be happening now... :v

Show 1 more comment

1 answer

0

You must use binary mode files, they have similar functioning, to create/read/write binary files you must change the opening parameters of the fopen(), as an example fopen("arquivo.txt", "rb");. Using binary mode files just use the function fseek().

Browser other questions tagged

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