在C ++中读取顺序文件时出错

时间:2009-07-17 16:09:27

标签: c++ gcc file-io

当我尝试编译我的示例C ++项目时,我遇到了一些问题,我正在尝试读取一个序列文件,但是当我编译时我遇到了一些错误,这里是代码:

// 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

这是编译的日志:

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

2 个答案:

答案 0 :(得分:14)

使用:

g++ ReadClientFile.cpp

gcc ReadClientFile.cpp -lstdc++

我猜测默认情况下gcc没有链接到标准c ++库。通常,您应该使用g ++来编译c ++程序。

答案 1 :(得分:2)

在GCC版本4.3上编译时没有任何错误的代码。但它不适用于具有多个客户端记录条目的文件。它也会出现包含空格的客户端名称的问题。所以我对代码进行了一些简单的修改,以便它可以使用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 

这是我用于测试的数据文件的内容:

A1080,Dave Thomas,5600,A1081,Nancy George,9000,A1082,Jacob B,1900

代码仍然不健全,因为我们不处理异常,损坏的数据文件等。