Problem with txt file reading into C++

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 512 Views - Last Post: 17 December 2011 - 11:10 PM Rate Topic: -----

#1 victorfc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-December 11

Problem with txt file reading into C++

Posted 17 December 2011 - 08:05 PM

Hello guys, I'm having some trouble import a txt file using C++. I searched for it all day but couldn't find a solution.
I'm using Xcode for mac and Codeblocks for windows.

The txt file have 60 lines, "char char float float" each (I show you a example after de code), I want my program to return some of these lines.
Can anyone help me?
Thank you all!!


#include <iostream>
#include <fstream>
#include <cmath>

#define HEIGHT 60
#define WIDTH 4

using namespace std;

int main()
{


    float a, b, l, p_cc, m_se,p_l, r_tc, k_tc, m_cv_max, sumdif;
    char marca[61], modelo[61];
    int kcv[61], mcv[61];

    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;
   file.open("arquivo.txt");

   if (file.is_open())
   {

            for (int i=0; i<60; ++i){

                file>>marca[i]>>modelo[i]>>kcv[i]>>mcv[i];

                if ((kcv[i]>=k_tc) && (mcv[i]<=m_cv_max)){
                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;

                }
            }
   }

    return 0;

}



The txt file are like:

MERCEDES-BENZ Axor_2544 14192 8808
VOLVO VM_4x2T 10050 5950
VOLVO FM11_6x2T 14100 8900
VOLVO FM11_4x2T 8600 7400
VOLVO FMX13_6x4T 13350 9650
...more 55 lines...

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with txt file reading into C++

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 08:20 PM

Each element of a char array is a single char. You are trying to store a string in there. You should change the data type of marca and modelo to string:
string marca[61], modelo[61];

You will also have to
#include <string>
Was This Post Helpful? 1
  • +
  • -

#3 victorfc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-December 11

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 08:31 PM

Hey r.stiltskin, thank you a lot for your help!

I changed it to string, but it still not giving me results when i run it...
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4059
  • View blog
  • Posts: 12,530
  • Joined: 25-December 09

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 08:33 PM

What results is it producing? What results is it supposed to produce?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 08:35 PM

Wel if you change those variables to strings, it will certainly read the input file and print output according to line 46, so exactly what "result" are you looking for that you're not getting?
Was This Post Helpful? 0
  • +
  • -

#6 victorfc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-December 11

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 08:46 PM

I want the program reads all lines and return those lines that satisfy the equation.

By example, my txt file have 60 lines, some of those are:

MERCEDES-BENZ Axor_2544 14192 8808
VOLVO VM_4x2T 10050 5950
VOLVO FM11_6x2T 14100 8900
VOLVO FM11_4x2T 8600 7400
VOLVO FMX13_6x4T 13350 9650

and the last numbers of each line means the kcv and the mcv.

I trying to make the program return the same complete lines when the arguments for the kcv and the mcv implemented are true (when kcv>=k_tc & mcv<=m_cv_max, k_tc and m_cv_max are previously calculated).
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 08:47 PM

When I ran it, it printed this:

Mcv,max: 10606.7
MERCEDES-BENZ Axor_2544 14192 8808 2628.1
VOLVO FM11_6x2T 14100 8900 2444.1


What were you expecting?
Was This Post Helpful? 1
  • +
  • -

#8 victorfc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-December 11

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 08:55 PM

Exactly that! Unfortunately I'm geting a bad result here. Maybe I'm doing something wrong, I'll check again...

Thank you again, r.stiltskin!!

Are you opening it from a txt file?!? =)
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 08:59 PM

Yes, I just put those 5 entries that you listed:
MERCEDES-BENZ Axor_2544 14192 8808
VOLVO VM_4x2T 10050 5950
VOLVO FM11_6x2T 14100 8900
VOLVO FM11_4x2T 8600 7400
VOLVO FMX13_6x4T 13350 9650


in a text file, arquivo.txt

Post your revised code, and post the output you're getting.
Was This Post Helpful? 0
  • +
  • -

#10 victorfc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-December 11

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 09:06 PM

That's my code, I only changed data and modelo to string.


#include <iostream>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    
    float a, b, l, p_cc, m_se,p_l, r_tc, k_tc, m_cv_max, sumdif;
    string marca[61], modelo[61];
    int kcv[61], mcv[61];
    
    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;
    file.open("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)){
                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;
            }
        }
    }
    
    return 0;
    
}



And I'm getting back 10 lines with nothing at the first 2 columns and some random numbers at the others, when I should get 28 lines...
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 09:21 PM

Please cut and paste your exact terminal output so I can see exactly what you're seeing.

And attach your cavalos-mecanicos.txt file as an attachment (see below the "Post" window).
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 09:28 PM

By the way, performing input that way, in a for loop with a fixed number of iterations, is not ideal because it requires that the input file have exactly that number of entries.

Here's a better way:
            int i = 0;
            while( file >> marca[i] ) {
                file>>modelo[i]>>kcv[i]>>mcv[i];

                if ((kcv[i]>=k_tc) && (mcv[i]<=m_cv_max)){
                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++;
            }


That way it will just keep reading as long as it finds another line of input.


edit: forgot to declare & increment i.

This post has been edited by r.stiltskin: 17 December 2011 - 10:27 PM

Was This Post Helpful? 1
  • +
  • -

#13 victorfc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-December 11

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 09:39 PM

These are my results and the txt file:

8454456 0 8.4517e+006
2012101291 4 2.0121e+006
8454144 4 8.45138e+006
8469768 0 8.46803e+006
8470792 5 8.4683e+006

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 10:25 PM

Your input file isn't exactly what it appears to be. I wrote a little program to examine it byte by byte. I expected my output to begin with:

77 69 82 67 69 68 69 83 45 66 69 78 90
those are the ASCII values for the characters MERCEDES-BENZ.

Instead, my output begins with:
-1 -2 77 0 69 0 82 0 67 0 69 0 68 0 69 0 83 0 45 0 66 0 69 0 78 0 90 0

I don't know why the -1 and -2 are there. But the rest are apparently wchars instead of ordinary 1-byte chars.

How did you produce that file? Can you convert it to 1-byte ascii chars?

Here is your file in ordinary ascii. Try running the program with this.

Attached File(s)


Was This Post Helpful? 1
  • +
  • -

#15 victorfc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-December 11

Re: Problem with txt file reading into C++

Posted 17 December 2011 - 10:39 PM

Awesome! Thank you a lot, r.stiltskin! You totally saved me!
I got that file at college, I would never think about that error. Thank you a lot about the explanation!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2