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

Posted

技术标签:

【中文标题】在 C++ 中读取顺序文件时出错【英文标题】:Error When Reading a Sequencial File In C++ 【发布时间】:2009-07-17 16:09:27 【问题描述】:

当我尝试编译我的示例 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

【问题讨论】:

【参考方案1】:

任意使用:

g++ ReadClientFile.cpp

gcc ReadClientFile.cpp -lstdc++

我猜 gcc 默认情况下不会与标准 c++ 库进行链接。一般来说,你应该使用 g++ 来编译 c++ 程序。

【讨论】:

不要使用 gcc(使用 g++)。还有一些其他的不一致(我不记得我的头顶了)。【参考方案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

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

【讨论】:

以上是关于在 C++ 中读取顺序文件时出错的主要内容,如果未能解决你的问题,请参考以下文章

C ++:从文本文件中读取单行,按字母顺序对单词进行排序

读取有序文件时,Spark 是不是保留记录顺序?

linux基础--用户登录时读取配置文件顺序

ets和读取文件出错

在 Visual C++ MFC 中启动窗口时的事件顺序

C++ 静态初始化顺序