My entrada.txt jas the following contents:

#Commented text
#This should be used like:
#Bar:Bar_number:Type_of_the_bar;
#
#
Bar:1:PV:1.05

So the lines that starts with # are commented so the program will not process them. Everything is working fine, but when I used getline() to delimit the string on ":" it gives the error above, saying that getline only accept 2 instead of 3 arguments?

#include "stdafx.h"
#include <fstream> 
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>

//using namespace std;
using std::ifstream;

int _tmain(int argc, _TCHAR* argv[])
{
	//
	std::ifstream arquivo("entrada.txt");
		if (!arquivo)
		{
			std::cout << "File not found" << std::endl<< std::endl;
			system("PAUSE");
			exit(0);
		}
		else
		{
			std::cout << "Loading file...Please wait..." << std::endl << std::endl;
		}
	//fim do carregamento
	//declarações das strings
		std::string token,arq_linha_part;
		char arquivo_linha[255];

		while(arquivo)
		{
			arquivo.getline(arquivo_linha,255);
			if (arquivo_linha[0] != 35)
			{
				token = arquivo_linha;
				while(std::getline(token,arq_linha_part,":"))
				{
				std::cout << arq_linha_part << std::endl;
				}
			}
		}

		system("PAUSE");
	return 0;
}

error:
1>------ Build started: Project: Interface GaussSolve, Configuration: Debug Win32 ------
1>Compiling...
1>Interface GaussSolve.cpp
1>c:\users\fernando\documents\visual studio 2008\projects\interface gausssolve\interface gausssolve\interface gausssolve.cpp(39) : error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\string(527) : see declaration of 'std::getline'
1>c:\users\fernando\documents\visual studio 2008\projects\interface gausssolve\interface gausssolve\interface gausssolve.cpp(39) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\string(475) : see declaration of 'std::getline'
1>c:\users\fernando\documents\visual studio 2008\projects\interface gausssolve\interface gausssolve\interface gausssolve.cpp(39) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\string(475) : see declaration of 'std::getline'
1>c:\users\fernando\documents\visual studio 2008\projects\interface gausssolve\interface gausssolve\interface gausssolve.cpp(39) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\string(475) : see declaration of 'std::getline'
1>c:\users\fernando\documents\visual studio 2008\projects\interface gausssolve\interface gausssolve\interface gausssolve.cpp(39) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\string(475) : see declaration of 'std::getline'
1>c:\users\fernando\documents\visual studio 2008\projects\interface gausssolve\interface gausssolve\interface gausssolve.cpp(39) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Users\Fernando\Documents\Visual Studio 2008\Projects\Interface GaussSolve\Interface GaussSolve\Debug\BuildLog.htm"
1>Interface GaussSolve - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Recommended Answers

All 4 Replies

The delimiter needs to be a character not a string, so try putting ':' instead of ":" on line 36.

getline()'s first parameter is an istream reference. It looks like you want an intermediate string stream.

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib> // For exit()

int main()
{
    std::ifstream arquivo("entrada.txt");

    if (!arquivo)
    {
        std::cout << "File not found\n\n";
        std::system("PAUSE");
        std::exit(EXIT_FAILURE);
    }
    else
    {
        std::cout << "Loading file...Please wait...\n\n";
    }

    std::string arquivo_linha, arq_linha_part;

    while (getline(arquivo, arquivo_linha))
    {
        if (arquivo_linha[0] != '#')
        {
            std::istringstream token(arquivo_linha);

            while(std::getline(token, arq_linha_part, ':'))
            {
                std::cout << arq_linha_part << std::endl;
            }
        }
    }
}

Ed also cleaned up the code a bit. Apologies. :$

Thanks for the help, and for cleaning up my code! =) It worked fine!!

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.