Jump to content
Fórum Script Brasil
  • 0

Importar arquivo de texto


RodrigoPrado

Question

Bom dia galera, faz algum tempo que não mexo com c/c++.

Tenho estes dados dentro de um arquivo de texto:

------------------

Cod: 1222

Aluno: Teste

Nota1: 1.00

Nota2: 2.00

Nota3: 3.00

------------------

Tenho por fim a seguinte estrutura.

dentro de uma classe em C.

struct Aluno {

int Cod;

char nome[30];

float n1;

float n2;

float n3;

};

Atualmente leio o txt da sequinte forma linha a linha...

x = fopen("Aluno.txt","r"); // abre o arquivo somente para leitura

while(fgets(linha,80,x)!=NULL)

printf("Linha Atual: %s",linha );

fclose(x);

Todavia o que necessito é importar o txt na estrutura... mas como posso proceder...???

queria algo como Aluno.Cod = x.Cod algo assim..

galera como fazer?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Fiz um código para demonstrar uma possível solução para o problema apresentado, mas por tê-lo feito rapidamente, sem dedicar muito esforço pode haver falhas e/ou erros, por isso modifique-o para suas necessidades.

/*
* main.c
*
* Copyright 2012 Mateus G. Pereira <mateusgpe@hotmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/


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

typedef struct Aluno {
int Cod;
char nome[30];
float n1;
float n2;
float n3;
} Aluno;

int scmp (const char* s1, const char* s2)
{
while(isspace(*s1))
s1++;
while(*s2 != '')
if(toupper(*s1++) != toupper(*s2++))
return 0;
return 1;
}

int stok (char* d, char t, char* s)
{
static char* str = NULL;

if(s != NULL)
str = s;
else if(str == NULL)
return 0;

while(*str == t)
str++;

while((*str != t) && (*str != ''))
*d++ = *str++;

*d = '';
return *str++ != '';
}

int structset (Aluno* al, char* s)
{
char label[128], value[192];
char* str;

if(stok(label, ':', s) == 0)
return 0;
stok(value, '\n', NULL);

str = value;

while(isspace(*str))
str++;

if(scmp(label, "ALUNO"))
strncpy(al->nome, str, 30);
else if(scmp(label, "COD"))
al->Cod = (int) strtol(str, NULL, 10);
else if(scmp(label, "NOTA1"))
al->n1 = (int) strtod(str, NULL);
else if(scmp(label, "NOTA2"))
al->n2 = (int) strtod(str, NULL);
else if(scmp(label, "NOTA3"))
al->n3 = (int) strtod(str, NULL);
else
return 0;
return 1;
}

int main(int argc, char **argv)
{
int i = 0, p = 0, z = 0;
char line[256];
Aluno als[256];
FILE* in;

in = fopen(*(argv + 1), "r");

if(in == NULL)
return 1;

while(fgets(line, 256, in) != 0)
{
structset(als + i, line);

if(p++ >= 4)
{
p = 0;
i++;
}
if(i >= 256)
break;
}
fclose(in);

while(z < i)
{
printf("[1] %s\n[2] %d\n", als[z].nome, als[z].Cod);
printf("[3] %f\n[4] %f\n", als[z].n1, als[z].n2);
printf("[5] %f\n\n", als[z].n3);
z++;
}
return 0;
}[/CODEBOX]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152k
    • Total Posts
      651.6k
×
×
  • Create New...