2013-06-04 51 views
0

我有一段代码不会编译。C++排序二进制文件

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <Windows.h> 
#include <iomanip> 
using namespace std; 


int average (int n); 

int main() 
{ 

string filename; 

int i,j; 
int n = 0; 
int sum = 0; 
int size = 4; 
const int nameLength = 19; 
const int maxRecords = 100; 
int index[100]; 


int scores[maxRecords]; 
char names[maxRecords][nameLength]; 

int count = 0; 

cout << "Enter binary data directory and file name: "; 
getline (cin, filename); 
// Open file for binary read-write access. 
    ifstream fin(filename.c_str(), ios::binary); 

if (!fin) { 
cout << "Could not open " << filename << endl; 
system("PAUSE"); 
return -1; 
} 

// Read data from the file. 
while (
fin.read(names[count], sizeof(names[0])) 
    && fin.read((char *) &scores[count], sizeof(scores[0])) 
) 
{ 

    count++; 
    fin.ignore(); // skip the padding byte 
} 


// Display the data and close. 
cout << "Your file's data unsorted: " << endl; 
cout << endl; 
    cout << setw(10) << "Name" << setw(20) << "Test Score" << endl; 

    for (i=0;i<n;i++){ 
      cout << setw(10) << names[i] << setw(20) << scores[i] << endl; 
    } 

      for (i=0;i<n;i++) 
    { 
      index[i]=i; 
    } 

    for (i=0;i<n;i++) 
    { 

      for (j=i+1;j<n;j++) 
      { 
        int temp; 
        if (scores[index[i]] > scores[index[j]]) 
        { 
          temp = index[i]; 
          index[i] = index[j]; 
          index[j] = temp; 
        } 
      } 
    } 

    cout << "The average of the test scores in your file is: " << average   (sum); 

sum=sum+scores[i]; 


fin.close(); 
system("PAUSE"); 
return 0; 
} 

int average (int sum, int size) 
{ 
return sum/size; 
} 

我收到说编译错误:fatal error LNK1120: 1 unresolved externals,我想不通为什么。另一个问题是如何格式化它,以便从原始二进制文件中读取的数据不会被篡改,然后输出并保存在新的编辑后的二进制文件中?

+0

什么是确切的错误信息? –

+0

为什么“”包含在内,但不包含“”?我在这里没有看到前者的任何内容,但'system'来自后者,而不是'system(“PAUSE”)'代码应该有的东西。 – chris

+0

错误消息说,“”致命错误LNK1120:1个未解决的外部事件“”,并且我早先在那里有因为我在那里有一个MAX_PATH事物,我拿出来了。 – user2444400

回答

0

你向前声明average像这样:

int average (int n); 

,但要实现平均是这样的:如果你更新你的向前声明

int average (int n, int size); 

,你在这里添加第二个参数:

cout << "The average of the test scores in your file is: " << average(sum,size); 

应该解决它。

+0

好了感谢的是固定的,但现在这是我的输出:输入的二进制数据的目录和文件名:C:\用户\张学友\桌面\ project6.dat 你的文件的数据未排序: 名称试验得分 的平均文件中的测试分数为:0按任意键继续。 。 。 – user2444400

+2

现在你必须修复你的程序。 :) – Joe

+0

是的,但它可能是我的排序,这是拧输出了? – user2444400