BarneyL. BarStin
BarneyL. BarStin

Reputation: 343

How to read a file in c++?

Im trying to read a file in my method "load". This file is like this:

3
3 Futech tablet 2 7.95
1 El_general_en_su_laberinto Gabriel_García_Márquez 23.50
2 Carne_picada 1 4.56

Where the first line is the number of items and my method should to loaded and print something like:

3 Futech tablet 2 7.95

This is my class with my method load

    #include "ItemProccesor.h"
    #include<string>
    #include<iostream>
    #include<fstream>
    #include <stdlib.h>

    using namespace std;

    ItemProcessor::ItemProcessor() {
        // TODO Auto-generated constructor stub

    }

    ItemProcessor::~ItemProcessor() {
        // TODO Auto-generated destructor stub
    }

    void ItemProcessor::load(string archivo){
int tam;
        int tipo;
        int cant;
        string cad1;
        string cad2;
        float precio;
        ifstream myfile;
        myfile.open(archivo.c_str());
        if (myfile.is_open()){
            myfile >> tam;

            for(int n= 0; n < tam; n++) {
                myfile >> tipo;
                cout << tipo << cad1;
            }
        }
        myfile.close();

    }

Actually my method print:

333

Is like always is taken the same line. This is my method main:

#include <iostream>
#include <string>
#include "ItemProccesor.h"

int main(int argc, char **argv) {
  ItemProcessor *processor = new ItemProcessor();
  processor->load("lista.txt");

}

¿How i can go to the next line?

Upvotes: 0

Views: 237

Answers (3)

Hani Goc
Hani Goc

Reputation: 2441

your method is printing 333 becausing simply you are not iterating through the lines.


  void ItemProcessor::load(string archivo){
            int cant;
            float precio;
            string tipo, cad1, cad2, line;
 
            ifstream myfile(archivo.c_str());
            if(myfile.is_open())
            {
                while(getline(myfile,line))
                {
                     myfile >> tipo >> cad1 >> cad2 >> cant >> precio;
                     cout << tipo << " "<< cad1 <<" "<< cad2 <<" "<<cant <<" "<< precio << endl;
                }
            }
            else
                cout <<"error " << endl;
            myfile.close();
        }

output

3 Futech tablet 2 7.95
1 El_general_en_su_laberinto Gabriel_García_Márquez 23 0.50
2 Carne_picada 1 4.56

the funny part is that it converted 23.50 to 23 0.50. Don't know why but oh well lolololol. At least it works.

Upvotes: 1

ravenspoint
ravenspoint

Reputation: 20616

Your input file seems to be messed up. Sometimes a row has four columns, sometimes five, sometimes three.

This makes it very hard to read!

If you cannot fix the file, then you will have to read each row in one go and then use a tokenizer to split the row at the spaces so that you can count how many columns are in each row before processing the row.

Upvotes: 1

kamilk
kamilk

Reputation: 4049

Not sure if that's the only problem, but you read tipo twice. Try replacing

myfile >> tipo >> cad1 >> cad2 >> cant >> precio;

with

myfile >> cad1 >> cad2 >> cant >> precio;

Upvotes: 2

Related Questions