Help with txt file importing to c++
Dec 17, 2011 at 4:40pm UTC
Hi ppl, I'm trying to import a txt file using c++, so I created a class file to make it easier. Anyway, it's not easy, I cannot make it work!!
I'm using mac xcode and Codeblocks for windows.
Can anyone help me?
The txt file have 60 lines, "char char float float" each, I want my program to return some of these lines.
Thank you all!!
.h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#ifndef LEARQUIVO_H_INCLUDED
#define LEARQUIVO_H_INCLUDED
#include <iostream>
#include <fstream>
using namespace std;
class LeArquivo
{public :
void Abrir(char * Nome);
char **Marca, **Modelo;
float *kcv, *mcv;
int tamanho;
};
void LeArquivo::Abrir(char * Nome)
{
fstream File;
File.open(Nome); //file.open
std::cout << "Arquivo Aberto\n" ;
Marca = new char * [100];
Modelo = new char * [100];
kcv = new float [100];
mcv = new float [100];
char buf[50];
int i = 0;
while ( File )
{
File >> buf;
char *str = new char [strlen(buf)+1];
memcpy(str, buf, strlen(buf)+1);
Marca[i] = str;
File >> buf;
str = new char [strlen(buf)+1];
memcpy(str, buf, strlen(buf)+1);
Modelo[i] = str;
File >> kcv[i] >> mcv[i];
++i;
}
tamanho = i;
}
#endif // LEARQUIVO_H_INCLUDED
main file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <cmath>
#include <fstream>
#include "LeArquivo.h"
using namespace std;
int main()
{
LeArquivo Arquivo;
Arquivo.Abrir("cavalosmecanicos.txt" );
float a, b, l;
float p_cc, m_se, p_l, r_tc;
a=1.16;
b=0.87;
l=11.5;
p_cc=696;
m_se=1450;
p_l=780;
r_tc=((l-2)/(2*l-8))*l*(p_cc+acos(-1)*a*b*p_l); //CALCULO DE Rtc
float k_tc, k_cv, m_cv, m_cv_max;
k_tc=((l-6)/(2*l-8))*l*(p_cc+acos(-1)*a*b*p_l); //CALCULO DE Ktc
m_cv_max=48500-(k_tc+r_tc+m_se); //CALCULO DE Mcv,max
k_cv=0;
m_cv=48500;
int i;
for (i=0;i<Arquivo.tamanho; ++i){
if ((Arquivo.kcv[i]>=k_tc) && (Arquivo.mcv[i]<=m_cv_max)){
float sumdif=(Arquivo.kcv[i]-k_tc)+(m_cv_max-Arquivo.mcv[i]);
cout<<Arquivo.Marca[i]<<"\t" <<Arquivo.Modelo[i]<<"\t" <<Arquivo.kcv[i]<<"\t" <<Arquivo.mcv[i]<<"\t" <<sumdif;
}
}
return 0;
}
Dec 17, 2011 at 7:58pm UTC
I'm trying to make it easier now, making all part of a main function, but still not working =/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
float a, b, l, p_cc, m_se,p_l, r_tc, k_tc, m_cv_max;
char marca[61], modelo[61];
int kcv[61], mcv[61];
//ENTRADA DE VARIAVEIS
a=1.16;
b=0.87;
l=11.5;
p_cc=696;
m_se=1450;
p_l=780;
r_tc=((l-2)/(2*l-8))*l*(p_cc+acos(-1)*a*b*p_l); //CALCULO DE Rtc
k_tc=((l-6)/(2*l-8))*l*(p_cc+acos(-1)*a*b*p_l); //CALCULO DE Ktc
m_cv_max=48500-(k_tc+r_tc+m_se); //CALCULO DE Mcv,max
cout<<"Mcv,max: " <<m_cv_max<<endl;
ifstream file ("cavalos-mecanicos.txt" );
if (file.is_open())
{
cout<<endl<<"Arquivo Aberto." <<endl<<endl;
for (int i=0; i<61; ++i){
file>>marca[i]>>modelo[i]>>kcv[i]>>mcv[i];
if ((kcv[i]>=k_tc) && (mcv[i]<=m_cv_max)){
float sumdif=(kcv[i]-k_tc)+(m_cv_max-mcv[i]);
cout<<marca[i]<<"\t" <<modelo[i]<<"\t" <<kcv[i]<<"\t" <<mcv[i]<<"\t" <<sumdif<<endl;
++i;
}
}
}
return 0;
}
Someone?!
Last edited on Dec 17, 2011 at 8:52pm UTC
Topic archived. No new replies allowed.