I'm having some problems when i try to compile my sample C++ project, i'm trying to read a sequencial file, but when i compile i got some errors, here is the code:
// ReadClientFile.cpp
// Lendo e imprimindo um arquivo sequêncial.
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
using std::fixed;
using std::ios;
using std::left;
using std::right;
using std::showpoint;
#include <fstream> // Fluxo de arquivo
using std::ifstream; // Insere fluxo de arquivo
#include <iomanip>
using std::setw;
using std::setprecision;
#include <string>
using std::string;
#include <cstdlib>
using std::exit; // Sai do protótipo de funcão
void outputLine( int, const string, double ); // Protótipo
int main()
{
// Construtor ifstream, abre o arquivo
ifstream inClientFile( "Clients.dat", ios::in );
// Fecha o programa se ifstream não pode abrir o arquivo
if ( !inClientFile )
{
cerr << "File could not be opened" << endl;
exit ( 1 );
} // Fim do if
int account;
char name[ 30 ];
double balance;
cout << left << setw( 10 ) << "Account" << setw( 13 )
<< "Name" << "Balance" << endl << fixed << showpoint;
// Exibe cada registro do arquivo
while ( inClientFile >> account >> name >> balance )
outputLine( account, name, balance );
return 0; // Destrutor ifstream fecha o arquivo
} // Fim de main
// Exibe um registro do arquivo
void outputLine( int account, const string name, double balance )
{
cout << left << setw( 10 ) << account << setw( 13 ) << name
<< setw( 7 ) << setprecision( 2 ) << right << balance << endl;
} // Fim da funcão outputLine
And here is the log of the compile:
ubuntu@ubuntu-laptop:~/C++$ gcc ReadClientFile.cpp
/tmp/ccVkuUp1.o: In function `__static_initialization_and_destruction_0(int, int)':
ReadClientFile.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
ReadClientFile.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccVkuUp1.o: In function `outputLine(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double)':
ReadClientFile.cpp:(.text+0xd1): undefined reference to `std::cout'
ReadClientFile.cpp:(.text+0xd6): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0xe7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0xf8): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
ReadClientFile.cpp:(.text+0x109): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0x11a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
ReadClientFile.cpp:(.text+0x12b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0x13c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setprecision)'
ReadClientFile.cpp:(.text+0x14c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0x15b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
ReadClientFile.cpp:(.text+0x163): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
ReadClientFile.cpp:(.text+0x16b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccVkuUp1.o: In function `main':
ReadClientFile.cpp:(.text+0x1ac): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)'
ReadClientFile.cpp:(.text+0x1bf): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator!() const'
ReadClientFile.cpp:(.text+0x1d2): undefined reference to `std::cerr'
ReadClientFile.cpp:(.text+0x1d7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
ReadClientFile.cpp:(.text+0x1df): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
ReadClientFile.cpp:(.text+0x1e7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
ReadClientFile.cpp:(.text+0x234): undefined reference to `std::cout'
ReadClientFile.cpp:(.text+0x239): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0x24d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0x25d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
ReadClientFile.cpp:(.text+0x271): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setw)'
ReadClientFile.cpp:(.text+0x281): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
ReadClientFile.cpp:(.text+0x291): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
ReadClientFile.cpp:(.text+0x299): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
ReadClientFile.cpp:(.text+0x2a1): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
ReadClientFile.cpp:(.text+0x2b1): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0x2c1): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))'
ReadClientFile.cpp:(.text+0x2e0): undefined reference to `std::allocator<char>::allocator()'
ReadClientFile.cpp:(.text+0x2ff): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
ReadClientFile.cpp:(.text+0x32f): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
ReadClientFile.cpp:(.text+0x357): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
ReadClientFile.cpp:(.text+0x373): undefined reference to `std::allocator<char>::~allocator()'
ReadClientFile.cpp:(.text+0x39b): undefined reference to `std::allocator<char>::~allocator()'
ReadClientFile.cpp:(.text+0x3c4): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
ReadClientFile.cpp:(.text+0x3d5): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*)'
ReadClientFile.cpp:(.text+0x3e9): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(double&)'
ReadClientFile.cpp:(.text+0x3fd): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator void*() const'
ReadClientFile.cpp:(.text+0x41d): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
ReadClientFile.cpp:(.text+0x453): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
/tmp/ccVkuUp1.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
To read data from a sequential file, open the file in input mode. Then use the Line Input # statement, the Input # statement, or the Input function, to read data from the file into variables. Line Input # reads one line of text from a file, up to an end-of-line. The end-of-line is not returned in the string.
A sequential file is one that is processed by reading the first record, operating on it, then reading the second record, operating on it, and so on. If a file is organized for sequential processing, it must always be read in sequence.
Sequential-Access File. Streams provide communication channels between files and programs. Three files and their associated streams are automatically opened when program execution begins: the standard input to read data from the keyboard. the standard output to print data on the screen.
Either use:
g++ ReadClientFile.cpp
or
gcc ReadClientFile.cpp -lstdc++
I'm guessing gcc isn't linking vs. the standard c++ library by default. In general you should be using g++ to compile c++ programs.
The code compiled without any errors on GCC version 4.3. But it did not work on a file with more than one client record entry. It will also have problems with client names that contain spaces. So I made some simple modifications to the code, so that it will work with a CSV.
// ReadClientFile.cpp
// Lendo e imprimindo um arquivo sequêncial.
#include <iostream>
#include <fstream> // Fluxo de arquivo
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
static void outputLine(int, const string,
double); // Protótipo
int main()
{
// Construtor ifstream, abre o arquivo
ifstream inClientFile("Clients.dat", ios::in);
// Fecha o programa se ifstream não pode abrir o arquivo
if (!inClientFile)
{
cerr << "File could not be opened" << endl;
return 1;
} // Fim do if
int account;
string name;
double balance;
const int buffSize = 80;
char* buffer = new char[buffSize + 1];
cout << left << setw( 10 ) << "Account" << setw( 13 )
<< "Name" << "Balance" << endl << fixed << showpoint;
// Exibe cada registro do arquivo
while (!inClientFile.eof())
{
inClientFile.getline(buffer, buffSize, ',');
account = atoi(buffer);
inClientFile.getline(buffer, buffSize, ',');
name = buffer;
inClientFile.getline(buffer, buffSize, ',');
balance = atof(buffer);
outputLine(account, name, balance);
}
return 0; // Destrutor ifstream fecha o arquivo
} // Fim de main
// Exibe um registro do arquivo
void outputLine(int account, const string name, double balance)
{
cout << left << setw( 10 ) << account << setw( 13 ) << name
<< setw( 7 ) << setprecision( 2 ) << right << balance << endl;
} // Fim da funcão outputLine
This is the contents of the data file I used for testing:
A1080,Dave Thomas,5600,A1081,Nancy George,9000,A1082,Jacob B,1900
The code is still not robust as we don't take care of exceptions, a corrupt data file etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With