微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 为什么源文件的名称影响编译?

当我在C中写了一个叫做“lotto.cpp”的一个乐透程序时,我遇到了一些很奇怪的事情.一切都很好,直到我为我的程序写入写入文件.当我编译时,它显示以下错误
ld: can't open output file for writing: lotto,errno=21 for architecture x86_64
collect2: ld returned 1 exit status

巧合的是,我将程序的名称改为“1.cpp”,突然间编译没有问题.当我将名称更改为“test.cpp”时,它也工作.

我真的好奇为什么会发生这种情况.有任何想法吗?

这发生在MacBook Pro上.

如果你想要代码,只要让我知道!

我知道有些人要求代码.这里是:

#include <iostream>
#include <fstream>

using namespace std;

const int NED  = 10;            
const int VIKING =  6;
const int norMAL =  7;
const int MAX  = 10;

void quickSort(int arr[],int left,int right);
int checkDuplicates(int arr[],int length);

int main (int argc,const char *argv[])
{
    int i,j,k,ans;
    char ans2;
    int lottoNumbers[MAX];

    ofstream out("Lotto.txt",ios::out | ios::app);

    srand((unsigned)time(NULL));    

    do
    {
        do
        {
            cout << "\n\nDo you want to play Viking Lotto (press 6),or normal Lotto (press 7): ";
            cin >> ans;
        }while(ans != VIKING && ans != normal);

        (ans == VIKING) ? cout << "\nViking Lotto:\n" : cout << "\n\nnormal Lotto:\n";
        (ans == VIKING) ? out << "\nViking Lotto:\n" : out << "\n\nnormal Lotto:\n";


        for (i = 0; i < NED; i++)       //10 rows
        {       
            for (j = 0; j < ans; j++)  //6 or 7 columns
            {
                (ans == VIKING) ? lottoNumbers[j] = (rand() % 48) + 1 : lottoNumbers[j] = (rand() % 34) + 1;
            }
            if(checkDuplicates(lottoNumbers,ans) != -1)        
            {
                for(k = 0; k < ans; k++)
                {
                    while(checkDuplicates(lottoNumbers,ans) == lottoNumbers[k])    
                    {
                        (ans == VIKING) ? lottoNumbers[k] = (rand() % 48) + 1 : lottoNumbers[k] = (rand() % 34) + 1;
                    }
                }
            }
            quickSort(lottoNumbers,ans - 1);  
            cout << '\n';

            for(j = 0; j < ans; j++)
            {
                cout << lottoNumbers[j] << '\t'; 
                out << lottoNumbers[j] << '\t';
            }
            out << '\n';

        }   
        cout << "\n\n";


        cout <<"Another lottery ticket (Y/N) ";
        cin >> ans2;
    }while(ans2 == 'j' || ans2 == 'J');

    cout << "\n\nLottO NUMBERS WAS WRITTEN TO FILE...\n\n";

    return 0;
}

void quickSort(int arr[],int right) 
{
    int i = left,j = right;
    int tmp;
    int mid = arr[(left + right) / 2];

    while (i <= j) 
    {
        while (arr[i] < mid)    i++;
        while (arr[j] > mid)    j--;
        if (i <= j) 
        {
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    };
    if (left < j)   quickSort(arr,left,j);
    if (i < right)  quickSort(arr,i,right);
}

int checkDuplicates(int arr[],int length)  
{                                       
    for(int i = 0; i < length; i++)
    {
        for(int j = i + 1; j < length; j++)
        {   
            if(arr[i] == arr[j])    return arr[j];
        }
    }
    return -1;  
}

解决方法

错误号21(在MacOS X 10.7.2上)是EISDIR:是一个目录.

名称乐透似乎是一个目录,而不是一个文件.

原文地址:https://www.jb51.cc/c/112039.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐